Merge pull request #84 from cclauss/patch-2

Scripter accepts an image_file_path, not a pokemon
This commit is contained in:
Lazo 2017-07-03 20:49:38 -04:00 committed by GitHub
commit eee1106466
7 changed files with 62 additions and 67 deletions

View file

@ -7,11 +7,11 @@ class TerminalAdapterInterface(object):
""" """
raise NotImplementedError() raise NotImplementedError()
def set_pokemon(self, pokemon): def set_image_file_path(self, image_file_path):
""" """
Set the background image of the terminal. Set the background image of the terminal.
:param pokemon: Information about a Pokémon. :param image_file_path: Path to an image file.
:type pokemon: dict :rtype str
""" """
raise NotImplementedError() raise NotImplementedError()

View file

@ -22,8 +22,8 @@ class ITerm(TerminalAdapterInterface):
p.communicate() p.communicate()
p.stdin.close() p.stdin.close()
def set_pokemon(self, pokemon): def set_image_file_path(self, image_file_path):
stdin = osa_script_fmt.format(pokemon.get_path()) stdin = osa_script_fmt.format(image_file_path)
self.__run_osascript(str.encode(stdin)) self.__run_osascript(str.encode(stdin))
def clear(self): def clear(self):

View file

@ -8,7 +8,7 @@ class NullAdapter(TerminalAdapterInterface):
def is_available(): def is_available():
return True return True
def set_pokemon(self, pokemon): def set_image_file_path(self, image_file_path):
print(self.err) print(self.err)
def clear(self): def clear(self):

View file

@ -8,8 +8,8 @@ class Terminology(TerminalAdapterInterface):
def is_available(): def is_available():
return os.environ.get("TERMINOLOGY") == '1' return os.environ.get("TERMINOLOGY") == '1'
def set_pokemon(self, pokemon): def set_image_file_path(self, image_file_path):
os.system('tybg "{}"'.format(pokemon.get_path())) os.system('tybg "{}"'.format(image_file_path))
def clear(self): def clear(self):
os.system("tybg") os.system("tybg")

View file

@ -11,11 +11,11 @@ class Tilix(TerminalAdapterInterface):
def is_available(): def is_available():
return "TILIX_ID" in os.environ return "TILIX_ID" in os.environ
def set_pokemon(self, pokemon): def set_image_file_path(self, image_file_path):
command = 'gsettings set {} {} "{}"' command = 'gsettings set {} {} "{}"'
os.system(command.format(self.setting_key, os.system(command.format(self.setting_key,
self.setting_field, self.setting_field,
pokemon.get_path())) image_file_path))
def clear(self): def clear(self):
command = 'gsettings set {} {}' command = 'gsettings set {} {}'

67
main.py
View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# The main module that brings everything together. """The main module that brings everything together."""
from database import Database from database import Database
import random import random
@ -10,17 +10,17 @@ import time
def print_list(list_of_items): def print_list(list_of_items):
# Print all the items in a list. Used for printing each Pokemon from a particular region. """Print all the items in a list. Used for printing each Pokemon from a particular region."""
print("\n".join(str(item) for item in list_of_items)) print("\n".join(str(item) for item in list_of_items))
def print_columns(items): def print_columns(items):
# Print a list as multiple columns instead of just one. """Print a list as multiple columns instead of just one."""
rows = [] rows = []
items_per_column = int(len(items) / 4) + 1 items_per_column = int(len(items) / 4) + 1
for index, pokemon in enumerate(items): for index, pokemon in enumerate(items):
if not pokemon.is_extra(): if not pokemon.is_extra():
name = pokemon.get_id() + " " + str(pokemon.get_name()).capitalize() name = pokemon.get_id() + " " + str(pokemon.get_name()).title()
else: else:
name = "--- " + pokemon.get_name() name = "--- " + pokemon.get_name()
name = name.ljust(20) name = name.ljust(20)
@ -37,7 +37,7 @@ def print_types(db):
print('\b\b\b ') print('\b\b\b ')
def prefix_search(db, arg): def prefix_search(db, arg):
# Find all Pokemon in database, db, with the prefix, arg. """Find all Pokemon in database, db, with the prefix, arg."""
result = db.names_with_prefix(arg) result = db.names_with_prefix(arg)
if len(result) == 0: if len(result) == 0:
print("No Pokemon found with prefix '" + arg + "'.") print("No Pokemon found with prefix '" + arg + "'.")
@ -46,7 +46,7 @@ def prefix_search(db, arg):
def print_extra(db): def print_extra(db):
# Print all the 'Extra' Pokemon from the 'Extra' folder. """Print all the 'Extra' Pokemon from the 'Extra' folder."""
result = db.get_extra() result = db.get_extra()
if len(result) == 0: if len(result) == 0:
print("No Pokemon were found in Images/Extra.") print("No Pokemon were found in Images/Extra.")
@ -55,7 +55,7 @@ def print_extra(db):
def print_usage(): def print_usage():
# Print the instructions of usage. """Print the instructions of usage."""
print( print(
''' '''
Usage: Usage:
@ -109,52 +109,47 @@ def slideshow(db, start, end, seconds="0.25", rand=False):
try: try:
for x in r: for x in r:
pokemon = db.get_pokemon(x) pokemon = db.get_pokemon(x)
scripter.change_terminal(pokemon) scripter.change_terminal(pokemon.get_path())
time.sleep(delay) time.sleep(delay)
except KeyboardInterrupt: except KeyboardInterrupt:
print("Program was terminated.") print("Program was terminated.")
sys.exit() sys.exit()
def change_terminal_background(db, arg): def change_terminal_background(db, arg): # arg is a pokemon_name
# Change the terminal background to the specified Pokemon, if it exists. """Change the terminal background to the specified Pokemon, if it exists."""
if arg in db: if arg in db:
pokemon = db.get_pokemon(arg) pokemon = db.get_pokemon(arg)
scripter.change_terminal(pokemon) scripter.change_terminal(pokemon.get_path())
else: # If not found in the database, try to give suggestions. else: # If not found in the database, try to give suggestions.
suggestions = db.names_with_infix(arg) suggestions = db.names_with_infix(arg)
if len(suggestions) == 0: if len(suggestions) == 0:
print("No such Pokemon was found and no suggestions are available.") print("No such Pokemon was found and no suggestions are available.")
elif len(suggestions) == 1:
scripter.change_terminal(suggestions[0])
print("Did you mean " + suggestions[0].get_name().capitalize() + "?")
scripter.change_terminal(suggestions[0])
else: else:
print("Did you mean " + suggestions[0].get_name().capitalize() + "?") pokemon = suggestions[0]
print("Other suggestions:") scripter.change_terminal(pokemon.get_path())
print_columns(suggestions[1:]) print("Did you mean {}?".format(pokemon.get_name().title()))
scripter.change_terminal(suggestions[0]) if suggestions[1:]:
print("Other suggestions:")
print_columns(suggestions[1:])
def change_wallpaper(db, arg): # arg is a pokemon_name
def change_wallpaper(db, arg): """Change the wallpaper to the specified Pokemon, if it exists."""
# Change the wallpaper to the specified Pokemon, if it exists.
if arg in db: if arg in db:
pokemon = db.get_pokemon(arg) pokemon = db.get_pokemon(arg)
scripter.change_wallpaper(pokemon) scripter.change_wallpaper(pokemon.get_path())
else: # If not found in the database, try to give suggestions. else: # If not found in the database, try to give suggestions.
suggestions = db.names_with_infix(arg) suggestions = db.names_with_infix(arg)
if len(suggestions) == 0: if len(suggestions) == 0:
print("No such Pokemon was found and no suggestions are available.") print("No such Pokemon was found and no suggestions are available.")
elif len(suggestions) == 1:
scripter.change_wallpaper(suggestions[0])
print("Did you mean " + suggestions[0].get_name().capitalize() + "?")
scripter.change_wallpaper(suggestions[0])
else: else:
print("Result: " + arg) pokemon = suggestions[0]
print("Did you mean " + suggestions[0].get_name().capitalize() + "?") scripter.change_wallpaper(pokemon.get_path())
print("Other suggestions:") print("Did you mean {}?".format(pokemon.get_name().title()))
print_columns(suggestions[1:]) if suggestions[1:]: # if there are other suggestions
scripter.change_wallpaper(suggestions[0]) print("Other suggestions:")
print_columns(suggestions[1:])
def multiple_argument_handler(arg, arg2, escape_code): def multiple_argument_handler(arg, arg2, escape_code):
@ -194,7 +189,7 @@ def multiple_argument_handler(arg, arg2, escape_code):
def single_argument_handler(arg, escape_code): def single_argument_handler(arg, escape_code):
# Handle the logic for when there is only one command line parameter inputted. """Handle the logic for when there is only one command line parameter inputted."""
db = Database() db = Database()
if len(arg) < 3 and arg.isalpha(): if len(arg) < 3 and arg.isalpha():
@ -242,9 +237,9 @@ def single_argument_handler(arg, escape_code):
elif arg == "dark" and escape_code: elif arg == "dark" and escape_code:
change_wallpaper(db, db.get_dark().get_name()) change_wallpaper(db, db.get_dark().get_name())
elif arg == "light": elif arg == "light":
change_terminal_background(db, db.get_light()) change_terminal_background(db, db.get_light().get_name())
elif arg == "dark": elif arg == "dark":
change_terminal_background(db, db.get_dark()) change_terminal_background(db, db.get_dark().get_name())
elif arg in ("type", "types"): elif arg in ("type", "types"):
print_types(db) print_types(db)
elif arg == "slideshow": elif arg == "slideshow":
@ -276,7 +271,7 @@ def single_argument_handler(arg, escape_code):
def main(argv): def main(argv):
# Entrance to the program. """Entrance to the program."""
if len(argv) == 1: if len(argv) == 1:
print('No command line arguments specified.' print('No command line arguments specified.'
'\nTry typing in a Pokemon name or number.' '\nTry typing in a Pokemon name or number.'

View file

@ -13,16 +13,6 @@ osa_script_fmt = """tell application "System Events"
end tell""" end tell"""
def clear_terminal():
adapter = identify()
adapter.clear()
def change_terminal(pokemon):
adapter = identify()
adapter.set_pokemon(pokemon)
def __run_osascript(stream): def __run_osascript(stream):
p = subprocess.Popen(['osascript'], stdout=subprocess.PIPE, stdin=subprocess.PIPE) p = subprocess.Popen(['osascript'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(stream) p.stdin.write(stream)
@ -30,20 +20,30 @@ def __run_osascript(stream):
p.stdin.close() p.stdin.close()
def change_wallpaper(pokemon): def __linux_create_wallpaper_script(image_file_path):
if sys.platform == "darwin":
script = osa_script_fmt.format(pokemon.get_path())
__run_osascript(str.encode(script))
elif sys.platform == "linux":
os.system(__linux_create_wallpaper_script(pokemon))
def __linux_create_wallpaper_script(pokemon):
# If its gnome... aka GDMSESSION=gnome-xorg, etc. # If its gnome... aka GDMSESSION=gnome-xorg, etc.
if "gnome" in os.environ.get("GDMSESSION"): if "gnome" in os.environ.get("GDMSESSION"):
fmt = 'gsettings set org.gnome.desktop.background picture-uri "file://{}"' fmt = 'gsettings set org.gnome.desktop.background picture-uri "file://{}"'
return fmt.format(pokemon.get_path()) return fmt.format(image_file_path)
#elif condition of KDE... # elif condition of KDE...
else: else:
print("Window manager not supported ") print("Window manager not supported ")
exit(1) exit(1)
def clear_terminal():
adapter = identify()
adapter.clear()
def change_terminal(image_file_path):
adapter = identify()
adapter.set_pokemon(image_file_path)
def change_wallpaper(image_file_path):
if sys.platform == "darwin":
script = osa_script_fmt.format(image_file_path)
__run_osascript(str.encode(script))
elif sys.platform == "linux":
os.system(__linux_create_wallpaper_script(image_file_path))