From 30fcba54eae5934b769c98b047f30e1a80815804 Mon Sep 17 00:00:00 2001 From: arcanearronax Date: Tue, 28 Jul 2020 01:42:07 -0400 Subject: [PATCH 1/3] Rebuilt the Cli.cli method to rely on Click to process command line arguments. This includes modifying the flags used to call various processes and how file extensions are provided. The to_clean dict in Cli.cli was modified to rely on more pythonic comprehension, rather than for loops. The requirements file was updated to include the Click module as well. --- organise_desktop/Cli.py | 127 +++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 67 deletions(-) mode change 100644 => 100755 organise_desktop/Cli.py diff --git a/organise_desktop/Cli.py b/organise_desktop/Cli.py old mode 100644 new mode 100755 index c40927f..f0fcaee --- a/organise_desktop/Cli.py +++ b/organise_desktop/Cli.py @@ -4,6 +4,7 @@ import os import sys import json +import click pwd = os.path.dirname(os.path.abspath(__file__)) @@ -11,80 +12,72 @@ folders = [x for x in Extensions] -def print_usage(): +@click.command() +@click.option( + "--undosched", + "-u", + is_flag=True, + help="Undo changes that were made.", +) +@click.option( + "--sched", + "-s", + is_flag=True, + help="Schedule an organization process to run." +) +@click.option( + "--desched", + "-d", + is_flag=True, + help="Remove a scheduled organization process." +) +@click.option( + "--ignore", + "-i", + type=str, + multiple=True, + help="Identify which file extension to exclude from organization. This should be passed for each file extension." +) +def cli( + undosched, + sched, + desched, + ignore +): """ - Prints usage of the Command Line interface + This is used to call a command line interface. """ - print("Usage: " + sys.argv[0] + " ") - print("-h -- Display help message.") - print("-u -- Undo") - print("-c --all -- Clean. If given --all then cleans all otherwise prompts") - print("-s --r -- start a schedule. removes a schedule if --r given") + if undosched: + undo() -if __name__ == '__main__': - - if len(sys.argv) <= 1: - print_usage() + elif sched: + schedule_start(folders) - elif sys.argv[1] == '-h': - print_usage() + elif desched: + schedule_end() - elif sys.argv[1] == '-u': - undo() - sys.exit() + elif ignore: + + tmp_ext_list = [] + for i in ignore: # Create a tuple with all the given extensions + tmp_ext_list.extend(i.split(',')) - elif sys.argv[1] == '-c': - - if len(sys.argv) == 3: - - if sys.argv[2] == '--all': - organise_desktop(Extensions) - sys.exit() - - else: - print("Invalid input") - sys.exit() - - elif len(sys.argv) == 2: - for x in enumerate(folders): - print('{} - {}'.format(x[0], x[1])) - - try: - exclude = input("Enter indexes of types to EXCLUDE\nEnter multiple by separating by a ,:\n") - except KeyboardInterrupt: - sys.exit() + extension_tuple = tuple([ # add periods to the start of extensions if not already there + x if x[0] == '.' else f".{x}" for x in tmp_ext_list + ]) - exclude_list = [int(i) for i in exclude.split(',')] - to_clean = {} - - for i in range(len(folders)): - if i not in exclude_list: - to_clean[folders[i]] = Extensions[folders[i]] - - - organise_desktop(to_clean) - - sys.exit() - - else: - print("Invalid Input") - sys.exit() + to_clean = { # create a dict that doesn't include the provided extensions, based on the extensions in the json file + category: [ + extension for extension in Extensions[category] if extension not in extension_tuple + ] for category in Extensions + } - elif sys.argv[1] == "-s": - - if len(sys.argv) == 2: - print("Starting schedule..") - schedule_start(folders) - - elif sys.argv[2] == '--r': - print("Removing schedule..") - schedule_end() - - else: - print("Invalid Input") - sys.exit() + organise_desktop(to_clean) else: - print("Invalid Input") - sys.exit() - + organise_desktop(Extensions) + + sys.exit() + +if __name__ == '__main__': + cli() From 03776803a423a42b43151b57c1999b93f88d4791 Mon Sep 17 00:00:00 2001 From: arcanearronax Date: Tue, 28 Jul 2020 01:42:59 -0400 Subject: [PATCH 2/3] Adding the requirements file since that was missed in the previous commit. --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 91a2e07..440875e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ requests==2.18.4 six==1.11.0 urllib3==1.22 urwid==2.0.1 +Click>=7.0 From 775fd3f0f07d4507d3c4184dde04781675a4bfa9 Mon Sep 17 00:00:00 2001 From: arcanearronax Date: Tue, 28 Jul 2020 23:04:10 -0400 Subject: [PATCH 3/3] Modified the cli method to rely on Click rather than parse arguments passed via sys.argv. The flags that can be used are --undosort/-u, --sched/-s, --desched/-d, --ignore/-i, and -h. The --ignore/-i flag is used to identify which file extensions to ignore when sorting. The --ignore/-i flag can be passed multiple times, once for each extension, or parse a comma delimited string of extensions. The extensions can be passed with or without the preceding period. --- organise_desktop/Cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/organise_desktop/Cli.py b/organise_desktop/Cli.py index f0fcaee..031da93 100755 --- a/organise_desktop/Cli.py +++ b/organise_desktop/Cli.py @@ -14,7 +14,7 @@ @click.command() @click.option( - "--undosched", + "--undosort", "-u", is_flag=True, help="Undo changes that were made.", @@ -47,7 +47,7 @@ def cli( """ This is used to call a command line interface. """ - if undosched: + if undosort: undo() elif sched: