2017-11-07 21:09:13 +00:00
|
|
|
#!/usr/bin/env python3.6
|
2017-07-03 16:46:00 +00:00
|
|
|
"""The main module that brings everything together."""
|
2017-04-28 07:46:39 +00:00
|
|
|
|
2017-08-10 20:16:51 +00:00
|
|
|
import os
|
2017-06-24 04:58:03 +00:00
|
|
|
import random
|
2017-04-28 23:17:44 +00:00
|
|
|
import sys
|
|
|
|
import time
|
2017-08-13 00:46:26 +00:00
|
|
|
from multiprocessing import Process
|
2017-04-28 07:46:39 +00:00
|
|
|
|
2017-10-09 01:35:59 +00:00
|
|
|
from . import scripter
|
|
|
|
from pokemonterminal.command_flags import parser, is_slideshow
|
2017-10-06 17:48:05 +00:00
|
|
|
from pokemonterminal.database import Database
|
|
|
|
from pokemonterminal.filters import Filter
|
2017-04-28 07:46:39 +00:00
|
|
|
|
2017-08-13 00:46:26 +00:00
|
|
|
PIPE_PATH = os.environ["HOME"] + "/.pokemon-terminal-pipe"
|
|
|
|
if not os.path.exists(PIPE_PATH):
|
|
|
|
os.mkfifo(PIPE_PATH)
|
2017-04-28 07:46:39 +00:00
|
|
|
|
|
|
|
|
2017-08-13 00:46:26 +00:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def daemon(time_stamp, pkmn_list):
|
|
|
|
# TODO: Implement messaging, like status and curr pokemon
|
|
|
|
pip = open(PIPE_PATH, 'r')
|
|
|
|
while True:
|
|
|
|
for msg in pip:
|
|
|
|
msg = msg.strip()
|
|
|
|
if msg == 'quit':
|
|
|
|
print("Stopping the slideshow")
|
|
|
|
sys.exit(0)
|
|
|
|
pip = open(PIPE_PATH, 'r')
|
2017-04-28 07:46:39 +00:00
|
|
|
|
|
|
|
|
2017-08-13 00:46:26 +00:00
|
|
|
def slideshow(filtered, delay, changer_func):
|
2017-08-10 20:16:51 +00:00
|
|
|
pid = os.fork()
|
|
|
|
if pid > 0:
|
|
|
|
print(f"Starting slideshow with {len(filtered)}, pokemon " +
|
|
|
|
f"and a delay of {delay} minutes between pokemon")
|
2017-08-13 00:46:26 +00:00
|
|
|
print("Forked process to background with pid", pid,
|
2017-08-14 12:56:04 +00:00
|
|
|
"you can stop it with -c")
|
2017-08-10 20:16:51 +00:00
|
|
|
os.environ["POKEMON_TERMINAL_PID"] = str(pid)
|
|
|
|
sys.exit(0)
|
2017-08-13 00:46:26 +00:00
|
|
|
p = Process(target=daemon, args=(time.time(), filtered,))
|
|
|
|
p.daemon = True
|
|
|
|
p.start()
|
2017-08-10 20:16:51 +00:00
|
|
|
random.shuffle(filtered)
|
|
|
|
queque = iter(filtered)
|
2017-08-13 00:46:26 +00:00
|
|
|
while p.is_alive():
|
2017-08-10 20:16:51 +00:00
|
|
|
next_pkmn = next(queque, None)
|
|
|
|
if next_pkmn is None:
|
|
|
|
random.shuffle(filtered)
|
|
|
|
queque = iter(filtered)
|
|
|
|
continue
|
2017-08-13 00:46:26 +00:00
|
|
|
changer_func(next_pkmn.get_path())
|
|
|
|
p.join(delay * 60)
|
2017-04-28 23:17:44 +00:00
|
|
|
|
|
|
|
|
2017-06-29 07:00:39 +00:00
|
|
|
def main(argv):
|
2017-07-03 16:46:00 +00:00
|
|
|
"""Entrance to the program."""
|
2017-07-07 19:03:00 +00:00
|
|
|
if __name__ != "__main__":
|
|
|
|
Filter.filtered_list = [pok for pok in Filter.POKEMON_LIST]
|
2017-10-07 13:25:15 +00:00
|
|
|
# TODO Lower main() complexity with factory functions or something
|
2017-10-09 01:35:59 +00:00
|
|
|
options = parser.parse_args(argv) # Parser is imported at top of file.
|
2017-08-14 12:56:04 +00:00
|
|
|
try:
|
|
|
|
options.id = int(options.id)
|
2017-10-07 13:25:15 +00:00
|
|
|
except ValueError:
|
2017-08-14 12:56:04 +00:00
|
|
|
options.name = options.id.lower()
|
|
|
|
options.id = 0
|
|
|
|
Filter.filtered_list = [
|
2017-08-14 18:05:50 +00:00
|
|
|
x for x in Filter.filtered_list if options.name == x.get_name()
|
2017-08-14 12:56:04 +00:00
|
|
|
]
|
2017-07-04 18:33:23 +00:00
|
|
|
|
2017-07-05 23:35:46 +00:00
|
|
|
size = len(Filter.filtered_list)
|
2017-07-04 18:33:23 +00:00
|
|
|
if size == 0:
|
|
|
|
print("No pokemon matches the specified filters")
|
2017-07-02 23:46:35 +00:00
|
|
|
return
|
2017-06-29 18:17:16 +00:00
|
|
|
|
2017-07-05 23:35:46 +00:00
|
|
|
if options.id <= 0:
|
2017-10-07 13:25:15 +00:00
|
|
|
# TODO this doesn't account for the current set pokemon and might try
|
|
|
|
# TODO to set the same pokemon again (essentially not doing anything)
|
2017-07-05 23:35:46 +00:00
|
|
|
target = random.choice(Filter.filtered_list)
|
|
|
|
else:
|
|
|
|
options.id -= 1
|
|
|
|
if len(Filter.POKEMON_LIST) > options.id:
|
|
|
|
if len(sys.argv) > 2:
|
|
|
|
print("ID has been specified, ignoring all filters.")
|
|
|
|
size = 1
|
|
|
|
target = Filter.POKEMON_LIST[options.id]
|
|
|
|
Filter.filtered_list = [target]
|
|
|
|
else:
|
|
|
|
print("Invalid id specified")
|
|
|
|
return
|
2017-08-10 20:16:51 +00:00
|
|
|
if size == 1:
|
|
|
|
print('A single pokemon matches the specified criteria: ')
|
2017-07-07 19:03:00 +00:00
|
|
|
|
2017-07-04 19:59:19 +00:00
|
|
|
if options.dry_run:
|
|
|
|
options.verbose = True
|
2017-07-04 18:33:23 +00:00
|
|
|
if options.verbose:
|
|
|
|
if size > Database.MAX_ID:
|
2017-08-10 20:16:51 +00:00
|
|
|
print('No pokemon has been filtered...')
|
2017-07-04 18:33:23 +00:00
|
|
|
else:
|
|
|
|
# Print the list of filtered pokemon
|
2017-08-10 20:16:51 +00:00
|
|
|
[
|
|
|
|
print("#%s - %s" % (pkmn.get_id(), pkmn.get_name().title()))
|
|
|
|
for pkmn in Filter.filtered_list
|
|
|
|
]
|
2017-07-04 18:33:23 +00:00
|
|
|
print("Total of %d pokemon matched the filters. Chose %s" %
|
|
|
|
(size, target.get_name().title()))
|
|
|
|
|
2017-07-04 19:59:19 +00:00
|
|
|
if options.dry_run:
|
2017-07-06 01:39:28 +00:00
|
|
|
print("Dry run, exiting.")
|
2017-07-04 19:59:19 +00:00
|
|
|
return
|
|
|
|
|
2017-08-13 00:46:26 +00:00
|
|
|
if options.clear:
|
|
|
|
pipe_out = os.open(PIPE_PATH, os.O_WRONLY)
|
|
|
|
os.write(pipe_out, b"quit\n")
|
|
|
|
os.close(pipe_out)
|
|
|
|
scripter.clear_terminal()
|
|
|
|
return
|
|
|
|
|
|
|
|
if is_slideshow and options.id <= 0 and size > 1:
|
|
|
|
if options.slideshow <= 0:
|
2017-10-06 19:25:14 +00:00
|
|
|
print("Time has to be greater then 0. You can use decimal values.")
|
2017-08-13 00:46:26 +00:00
|
|
|
return
|
|
|
|
target_func = scripter.change_wallpaper if options.wallpaper else \
|
|
|
|
scripter.change_terminal
|
|
|
|
slideshow(Filter.filtered_list, options.slideshow, target_func)
|
|
|
|
return
|
2017-08-10 20:16:51 +00:00
|
|
|
|
2017-07-04 18:33:23 +00:00
|
|
|
if options.wallpaper:
|
|
|
|
scripter.change_wallpaper(target.get_path())
|
2017-04-28 07:46:39 +00:00
|
|
|
else:
|
2017-07-04 18:33:23 +00:00
|
|
|
scripter.change_terminal(target.get_path())
|
2017-06-29 07:00:39 +00:00
|
|
|
|
2017-07-02 23:46:35 +00:00
|
|
|
|
2017-06-29 07:00:39 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
# Entrance to the program.
|
2017-07-06 01:39:28 +00:00
|
|
|
main(sys.argv[1:])
|