From a8635120849d5dc3bd1cd308f7e00402595a58e6 Mon Sep 17 00:00:00 2001 From: LazoCoder Date: Fri, 28 Apr 2017 03:46:39 -0400 Subject: [PATCH] Added script creator and a bunch of print methods --- README.md | 2 +- Source/database.py => database.py | 104 +++++++++++++++++---------- main.py | 113 ++++++++++++++++++++++++++++++ scripter.py | 72 +++++++++++++++++++ 4 files changed, 253 insertions(+), 38 deletions(-) rename Source/database.py => database.py (63%) create mode 100644 main.py create mode 100644 scripter.py diff --git a/README.md b/README.md index 17d8b57..2442cef 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ I highly suggest making the font colors black and the terminal window transparen # Adding Custom Images -The folder Images/Extra is for adding custom images. You can manually add backgrounds to this folder and they will be visible to the program. To see a list of all the custom backgrounds type: +The folder Images/Extra is for adding custom images. You can manually add backgrounds to this folder and they will be visible to the program. Only PNG format is supported. To see a list of all the custom backgrounds type: ``` pokemon extra ``` diff --git a/Source/database.py b/database.py similarity index 63% rename from Source/database.py rename to database.py index 80a15cb..b5592e1 100644 --- a/Source/database.py +++ b/database.py @@ -1,28 +1,19 @@ -# The Database object is a container for all the supported Pokemon. It loads up all the Pokemons' -# IDs and names from a text file. It also analyzes all the images available in the 'Extra' folder -# and stores them in a separate data structure. The Pokemon in the text file are already assumed -# to exist in the other folders, so there is no reason to traverse them. -# -# The Pokemon are stored as Pokemon objects in a list. They contain an ID, name, region, and -# folder location. The 'Extra' Pokemon images are stored as tuples in a separate list. The -# reason for this is because 'Extra' images don't have IDs. The tuple contains the name of -# the image and the location of the image. +# The Database object is a container for all the supported Pokemon. import os -from sys import argv class Pokemon: __id = "" # ID is stored as a string because it must maintain "003" format, not "3". __name = "" __region = "" - __folder = "" # The location of the image. + __path = "" # The location of the image. - def __init__(self, identifier, name, region, folder): + def __init__(self, identifier, name, region, path): self.__id = identifier self.__name = name self.__region = region - self.__folder = folder + self.__path = path def get_id(self): return self.__id @@ -33,19 +24,22 @@ class Pokemon: def get_region(self): return self.__region - def get_folder(self): - return self.__folder + def get_path(self): + return self.__path + + def is_extra(self): + return self.__id is None def __str__(self): - return self.get_id() + " " + self.get_name() + " at " + self.get_folder() + return self.get_id() + " " + self.get_name() + " at " + self.get_path() class Database: __pokemon_list = [] __pokemon_dictionary = {} - __extra_pokemon = {} # The Pokemon from the 'Extra' folder. __directory = "" # The global location of the code. __MAX_ID = 493 # Highest possible Pokemon ID. + __regions = ('kanto', 'johto', 'hoenn', 'sinnoh') def __init__(self): self.directory = os.get_exec_path()[0] @@ -56,11 +50,47 @@ class Database: string = "POKEMON:\n" for element in self.__pokemon_list: string += str(element) + "\n" - string += "EXTRA:\n" - for element in self.__extra_pokemon: - string += str(element) + " in " + str(self.__extra_pokemon[element] + "\n") return string[:-1] # Remove the final new line ("\n"). + def get_all(self): + # Get all the Pokemon. + result = [] + for pokemon in self.__pokemon_list: + result.append(pokemon) + return result + + def get_kanto(self): + # Get all the Pokemon from the Kanto region. + return self.__get_region("kanto") + + def get_johto(self): + # Get all the Pokemon from the Johto region. + return self.__get_region("kanto") + + def get_hoenn(self): + # Get all the Pokemon from the Hoenn region. + return self.__get_region("hoenn") + + def get_sinnoh(self): + # Get all the Pokemon from the Sinnoh region. + return self.__get_region("sinnoh") + + def get_extra(self): + # Get all the Extra Pokemon images available. + return self.__get_region(None) + + def get_regions(self): + # Get all the supported regions. + return self.__regions + + def __get_region(self, region): + # Helper method for getting all the Pokemon of a specified region. + result = [] + for pokemon in self.__pokemon_list: + if pokemon.get_region() == region: + result.append(pokemon) + return result + def pokemon_exists(self, pokemon): # Check for a Pokemon by ID or name. if type(pokemon) is int or str(pokemon).isdigit(): @@ -78,11 +108,14 @@ class Database: def name_exists(self, name): # Check for Pokemon by Name. - if name.lower() in self.__pokemon_dictionary: - return True - if name.lower() in self.__extra_pokemon: - return True - return False + return name.lower() in self.__pokemon_dictionary + + def names_starting_with(self, prefix): + result = [] + for pokemon in self.__pokemon_list: + if str(pokemon.get_name()).startswith(prefix): + result.append(pokemon) + return result def __load_data(self): # Load all the Pokemon data. This does not include the 'Extra' Pokemon. @@ -93,8 +126,8 @@ class Database: name = line[len(identifier)+1:-1].lower() # The rest is the name (minus the new line at the end). identifier = self.__add_zeroes(identifier) # This statement cannot occur before name has been created. region = self.__determine_region(identifier) - folder = self.__determine_folder(identifier) - pokemon = Pokemon(identifier, name, region, folder) + path = self.__determine_folder(identifier) + "/" + identifier + ".png" + pokemon = Pokemon(identifier, name, region, path) self.__pokemon_list.append(pokemon) self.__pokemon_dictionary[pokemon.get_name()] = pokemon @@ -103,8 +136,13 @@ class Database: for file in os.listdir(self.directory + "/./Images/Extra"): if file.endswith(".png"): name = os.path.join("/Images/Extra", file).split('/')[-1][0:-4].lower() - folder = self.directory + "/./Images/Extra" - self.__extra_pokemon[name] = folder + path = self.directory + "/./Images/Extra" + pokemon = Pokemon(None, name, None, path) + if name in self.__pokemon_dictionary: + raise Exception("Duplicate names detected. " + "The name of the file " + str(name) + ".png in the folder 'Extra' must be changed.") + self.__pokemon_list.append(pokemon) + self.__pokemon_dictionary[pokemon.get_name()] = pokemon @staticmethod def __add_zeroes(number): @@ -147,11 +185,3 @@ class Database: return self.directory + "/./Images/Generation IV - Sinnoh" else: raise Exception("Pokemon ID cannot be greater than 493.") - -# Method for debugging. -if __name__ == "__main__": - database = Database() - if len(argv) == 1: - print(database) - else: - print(database.pokemon_exists(argv[1])) diff --git a/main.py b/main.py new file mode 100644 index 0000000..6bdc25a --- /dev/null +++ b/main.py @@ -0,0 +1,113 @@ +#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 + +# The main module that brings everything together. + +from sys import argv +from database import Pokemon, Database + + +def print_list(list_of_items): + # Print all the items in a list. Used for printing each Pokemon from a particular region. + for item in list_of_items: + print(item) + + +def print_columns(items): + # Print a list as multiple columns instead of just one. + rows = [] + items_per_column = int(len(items) / 5) + 1 + + for index in range(0, len(items)): + pokemon = items[index] + + if not pokemon.is_extra(): + name = pokemon.get_id() + " " + pokemon.get_name() + else: + name = "--- " + pokemon.get_name() + + name = name.ljust(20) + + if len(rows) < items_per_column: + rows.append(name) + else: + rows[index % items_per_column] += name + + print_list(rows) + + +def prefix_search(db, arg): + # Find all Pokemon in database, db, with the prefix, arg. + result = db.names_starting_with(arg) + if len(result) == 0: + print("No Pokemon found with prefix '" + arg + "'.") + else: + print_columns(result) + + +def print_extra(db): + # Print all the 'Extra' Pokemon from the 'Extra' folder. + result = db.get_extra() + if len(result) == 0: + print("No Pokemon were found in Images/Extra.") + else: + print_columns(result) + + +def print_usage(): + # Print the instructions of usage. + print( + ''' +Usage: + pokemon [parameter] + +Parameters: + [name] - Change the terminal background to the specified Pokemon. + [index] - Change the terminal background to a Pokemon by its index. + [region] - List all the Pokemon of the specified region. + [letter] - List all Pokemon who's names begin with a particular letter. + +Other Parameters: + pokemon all - List all the Pokemon supported. + pokemon random - Pick a Pokemon at random. + pokemon ? - Identify the current Pokemon. + pokemon regions - List all the available regions. + pokemon slideshow - Iterate through each Pokemon. + pokemon slideshow-kanto - Iterate through each Pokemon in the specified reigon. + pokemon extra - List all the Pokemon from the 'Extra' folder. + pokemon help - Display this menu. +''') + + +def single_argument_handler(arg): + # Handle the logic for when there is only one command line parameter inputted. + arg = argv[1].lower() + db = Database() + if len(arg) < 3 and arg.isalpha(): + prefix_search(db, arg) + elif arg == "extra" or arg == "custom": + print_extra(db) + elif arg == "regions": + print_list(db.get_regions()) + elif arg == "help" or arg == "--help" or arg == "-h": + print_usage() + elif arg == "kanto": + print_columns(db.get_kanto()) + elif arg == "johto": + print_columns(db.get_johto()) + elif arg == "hoenn": + print_columns(db.get_hoenn()) + elif arg == "sinnoh": + print_columns(db.get_sinnoh()) + elif arg == "all" or arg == "pokemon" or arg == "list": + print_columns(db.get_all()) + + + +if __name__ == "__main__": + # Entrance to the program. + if len(argv) == 1: + print("No command line arguments specified. Try typing in a Pokemon name or number.") + elif len(argv) == 2: + single_argument_handler(argv[1]) + else: + print("Only one command line argument is supported.") diff --git a/scripter.py b/scripter.py new file mode 100644 index 0000000..0220d44 --- /dev/null +++ b/scripter.py @@ -0,0 +1,72 @@ +import os + + +def __terminal_script(pokemon): + # Create the content for script that will change the terminal background image. + content = "tell application \"iTerm\"\n" + content += "\ttell current session of current window\n" + content += "\t\tset background image to \"" + pokemon.get_path() + "\"\n" + content += "\tend tell\n" + content += "end tell" + return content + + +def __wallpaper_script(pokemon): + # Create the content for the script that will change the wallpaper. + path = pokemon.get_path() + pokemon.get_id() + content = "tell application \"System Events\"\n" + content += "\ttell current desktop\n" + content += "\t\tset picture to \"" + pokemon.get_path() + "\"\n" + content += "\tend tell\n" + content += "end tell" + return content + + +def __create_terminal_script(pokemon): + # Create and save the script for changing the terminal background image. + content = __terminal_script(pokemon) + file = open(os.get_exec_path()[0] + "/./Scripts/background.scpt", "wb") + file.write(bytes(content, 'UTF-8')) + file.close() + + +def __create_wallpaper_script(pokemon): + # Create and save the script for changing the wallpaper. + content = __wallpaper_script(pokemon) + file = open(os.get_exec_path()[0] + "/./Scripts/wallpaper.scpt", "wb") + file.write(bytes(content, 'UTF-8')) + file.close() + + +def __create_terminal_bash(): + # Create and save the run.sh that will execute the AppleScript if the correct run.sh doesn't already exist. + content = "#!/bin/bash\n" + "osascript " + os.get_exec_path()[0] + "/./Scripts/background.scpt" + if open(os.get_exec_path()[0] + "/./Scripts/run.sh", 'r').read() == content: + return + file = open(os.get_exec_path()[0] + "/./Scripts/run.sh", 'wb') + file.write(bytes(content, 'UTF-8')) + file.close() + + +def __create_wallpaper_bash(): + # Create and save the run.sh that will execute the AppleScript if the correct run.sh doesn't already exist. + content = "#!/bin/bash\n" + "osascript " + os.get_exec_path()[0] + "/./Scripts/wallpaper.scpt" + if open(os.get_exec_path()[0] + "/./Scripts/run.sh", 'r').read() == content: + return + file = open(os.get_exec_path()[0] + "/./Scripts/run.sh", 'wb') + file.write(bytes(content, 'UTF-8')) + file.close() + + +def change_terminal(pokemon): + # Create, save and run the bash script to change the terminal background. + __create_terminal_script(pokemon) + __create_terminal_bash() + os.system(os.get_exec_path()[0] + "/./Scripts/run.sh") + + +def change_wallpaper(pokemon): + # Create, save and run the bash script to change the wallpaper. + __create_wallpaper_script(pokemon) + __create_wallpaper_bash() + os.system(os.get_exec_path()[0] + "/./Scripts/run.sh")