From 61c63f9340f7f0f60573f00826b3bd6e45e59524 Mon Sep 17 00:00:00 2001 From: Devisha Padmaperuma Date: Thu, 11 Nov 2021 09:31:38 +0530 Subject: [PATCH 1/3] Added a set aliases section --- functions/configuration.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/functions/configuration.py b/functions/configuration.py index 66e0225..8c3615d 100644 --- a/functions/configuration.py +++ b/functions/configuration.py @@ -1,4 +1,4 @@ -from config import print, input, warning +from config import info, input, warning from pathlib import Path from json import loads, dumps @@ -18,6 +18,19 @@ def write_config(self) -> None: choice = input("\tDefault Directory (goes here if no args are given)", override="orange1") config.__add__("default_dir", choice) + info("Add Your Directory Aliases Below (Ctrl+C when Done)") + aliases = [] + while True: + try: + temp_dict = { + "Name": input("\tName", override="pale_turquoise1"), + "Directory": input("\tDirectory", override="gold3") + } + aliases.append(temp_dict) + except KeyboardInterrupt: + break + config.__add__("aliases", aliases) + config_file = open(self.file, "w+") json_dump = dumps(config.__get__(), indent=4) config_file.write(json_dump) From 68167abf05deff149d1b20f1c91acad44bd587a4 Mon Sep 17 00:00:00 2001 From: Devisha Padmaperuma Date: Thu, 11 Nov 2021 09:36:33 +0530 Subject: [PATCH 2/3] Fixed parse_config() - Made it a while loop that returns the object --- functions/configuration.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/functions/configuration.py b/functions/configuration.py index 8c3615d..d740259 100644 --- a/functions/configuration.py +++ b/functions/configuration.py @@ -38,12 +38,13 @@ def write_config(self) -> None: config_file.close() def parse_config(self) -> dict: - try: - config_file = open(self.file, "r") - return loads(config_file.read()) - except FileNotFoundError: - warning("No Config File Found, Making One") - self.write_config() + while True: + try: + config_file = open(self.file, "r") + return loads(config_file.read()) + except FileNotFoundError: + warning("No Config File Found, Making One") + self.write_config() class WriteJSON(object): From d89eed465f3ce7a572b8112831b04bf3f982758c Mon Sep 17 00:00:00 2001 From: Devisha Padmaperuma Date: Thu, 11 Nov 2021 09:52:59 +0530 Subject: [PATCH 3/3] Completed directory aliases - Made config() return default_dir and aliases - if there were arguments, it would search for the argument in the aliases and if there's a match, chdir there. Otherwise it'll chdir to the argument. --- __main__.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/__main__.py b/__main__.py index fa1189d..1cdfcbc 100644 --- a/__main__.py +++ b/__main__.py @@ -15,7 +15,7 @@ def first_time_check(): setup() -def config() -> str: +def config() -> [str, tuple]: def update(): number = ar['update_check_frequency'] @@ -52,21 +52,26 @@ def write(file: Path, contents: str): try: if ar['update_check_frequency'] != 0: update() - return ar['default_dir'] + return ar['default_dir'], ar['aliases'] except KeyError: error("There's been an unsatisfied option, rebuilding config") conf.write_config() if __name__ == "__main__": - default = config() + default, aliases = config() directory = "" try: if len(argv) < 2: directory = default chdir(directory) else: - directory = argv[1] + for entry in aliases: + if entry['Name'] == argv[1]: + directory = entry['Directory'] + break + else: + directory = argv[1] chdir(directory) except FileNotFoundError: error(f"Directory '{directory}' Not Found!")