2017-06-06 17:13:26 +00:00
|
|
|
#!/usr/bin/env python3
|
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-13 00:46:26 +00:00
|
|
|
import argparse
|
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-06 17:48:05 +00:00
|
|
|
from . import filters, scripter
|
|
|
|
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-07-04 18:33:23 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Set a pokemon to the current terminal background or '
|
2017-08-13 00:46:26 +00:00
|
|
|
'wallpaper',
|
|
|
|
epilog='Not setting any filters will get a completely random pokemon')
|
|
|
|
filters_group = parser.add_argument_group(
|
2017-07-04 18:33:23 +00:00
|
|
|
'Filters', 'Arguments used to filter the list of pokemons with '
|
2017-10-07 13:25:15 +00:00
|
|
|
'various conditions that then will be picked')
|
2017-08-13 00:46:26 +00:00
|
|
|
filters_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-n',
|
|
|
|
'--name',
|
|
|
|
help='Filter by pokemon which name contains NAME',
|
|
|
|
action=filters.NameFilter,
|
|
|
|
type=str.lower)
|
2017-08-13 00:46:26 +00:00
|
|
|
filters_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-r',
|
|
|
|
'--region',
|
|
|
|
help='Filter the pokemons by region',
|
|
|
|
action=filters.RegionFilter,
|
|
|
|
choices=Database.REGIONS,
|
|
|
|
type=str.lower)
|
2017-08-13 00:46:26 +00:00
|
|
|
filters_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-l',
|
|
|
|
'--light',
|
2017-10-07 13:25:15 +00:00
|
|
|
help='Filter out the pokemons darker (ligthness treshold lower) ' +
|
|
|
|
'then 0.xx (default is 0.7)',
|
2017-08-10 20:16:51 +00:00
|
|
|
default=0.7,
|
|
|
|
const=0.7,
|
|
|
|
metavar='0.xx',
|
|
|
|
nargs='?',
|
|
|
|
type=float,
|
|
|
|
action=filters.LightFilter)
|
2017-08-13 00:46:26 +00:00
|
|
|
filters_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-d',
|
|
|
|
'--dark',
|
2017-10-07 13:25:15 +00:00
|
|
|
help='Filter out the pokemons lighter (ligthness treshold higher) ' +
|
|
|
|
'then 0.xx (defualt is 0.42)',
|
2017-08-10 20:16:51 +00:00
|
|
|
default=0.42,
|
|
|
|
const=0.42,
|
|
|
|
metavar='0.xx',
|
|
|
|
nargs='?',
|
|
|
|
type=float,
|
|
|
|
action=filters.DarkFilter)
|
2017-08-13 00:46:26 +00:00
|
|
|
filters_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-t',
|
|
|
|
'--type',
|
|
|
|
help='Filter the pokemons by type.',
|
|
|
|
action=filters.TypeFilter,
|
|
|
|
choices=Database.POKEMON_TYPES,
|
|
|
|
type=str.lower)
|
2017-08-13 00:46:26 +00:00
|
|
|
filters_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-ne',
|
|
|
|
'--no-extras',
|
2017-10-07 13:25:15 +00:00
|
|
|
help='Excludes extra pokemons (from the extras folder)',
|
2017-08-10 20:16:51 +00:00
|
|
|
nargs=0,
|
|
|
|
action=filters.NonExtrasFilter)
|
2017-08-13 00:46:26 +00:00
|
|
|
filters_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-e',
|
|
|
|
'--extras',
|
|
|
|
help='Excludes all non-extra pokemons',
|
|
|
|
nargs=0,
|
|
|
|
action=filters.ExtrasFilter)
|
2017-07-04 18:33:23 +00:00
|
|
|
|
2017-08-13 00:46:26 +00:00
|
|
|
misc_group = parser.add_argument_group("Misc")
|
|
|
|
misc_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-ss',
|
|
|
|
'--slideshow',
|
|
|
|
help='Instead of simply choosing a random pokemon ' +
|
|
|
|
'from the filtered list, starts a slideshow (with X minutes ' +
|
|
|
|
'of delay between pokemon) in the background with the ' +
|
|
|
|
'pokemon that matched the filters',
|
2017-08-13 00:46:26 +00:00
|
|
|
const=10.0, nargs='?', type=float, metavar='X')
|
|
|
|
is_slideshow = '-ss' in sys.argv or '--slideshow' in sys.argv
|
|
|
|
misc_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-w',
|
|
|
|
'--wallpaper',
|
2017-08-13 00:46:26 +00:00
|
|
|
help='Changes the desktop wallpaper instead of the terminal '
|
|
|
|
'background',
|
2017-08-10 20:16:51 +00:00
|
|
|
action='store_true')
|
2017-08-13 00:46:26 +00:00
|
|
|
misc_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-v', '--verbose', help='Enables verbose output', action='store_true')
|
2017-08-13 00:46:26 +00:00
|
|
|
misc_group.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-dr',
|
|
|
|
'--dry-run',
|
2017-08-13 00:46:26 +00:00
|
|
|
help='Implies -v and doesnt actually changes either wallpaper '
|
|
|
|
'or background after the pokemon has been chosen',
|
2017-08-10 20:16:51 +00:00
|
|
|
action='store_true')
|
2017-07-04 19:59:19 +00:00
|
|
|
either = parser.add_mutually_exclusive_group()
|
|
|
|
either.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'-c',
|
|
|
|
'--clear',
|
|
|
|
help='Clears the current pokemon from terminal '
|
2017-08-13 00:46:26 +00:00
|
|
|
'background and quits.',
|
2017-08-10 20:16:51 +00:00
|
|
|
action='store_true')
|
2017-07-05 23:35:46 +00:00
|
|
|
either.add_argument(
|
2017-08-10 20:16:51 +00:00
|
|
|
'id',
|
2017-10-06 19:25:14 +00:00
|
|
|
help='Specify the wanted pokemon ID or the exact (case insensitive)' +
|
|
|
|
' name',
|
2017-08-10 20:16:51 +00:00
|
|
|
nargs='?',
|
2017-08-14 12:56:04 +00:00
|
|
|
default=0, const=0)
|
2017-07-06 01:39:28 +00:00
|
|
|
options = parser.parse_args(argv)
|
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:])
|