From 3b5f96a0e012403e40a33e7a87d3dfac593b41aa Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Tue, 10 Jul 2018 22:36:44 +0200 Subject: [PATCH] move args --- args.py | 35 +++++++++++++++++++++++++++++++++++ project.py | 44 ++++++++------------------------------------ task.py | 2 +- 3 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 args.py diff --git a/args.py b/args.py new file mode 100644 index 0000000..b021d96 --- /dev/null +++ b/args.py @@ -0,0 +1,35 @@ +import sys +import logging + +def get_string_arg(args, required): + """ + Take an array of element, take first element and return it stripped. + If array is empty or None, print an error message with required and exit + in error. + + :param args: an array of arguments. Can be empty or None + :param required: the message to print in no args (or None) + :return: The arg stripped + """ + if not args: + print(f'Missing {required}') + sys.exit(1) + + logging.debug(args) + return args[0].strip() + +# TODO Move this function +def arg_string(arg, required = ''): + # TODO To meld with get_string_args + if required and arg.startswith('-'): + print('required ', required) + sys.exit('1') + else: + return arg.strip() + +def arg_number(arg, required = ''): + if not arg.isdigit(): + print('Invalid value') # TODO + sys.exit('1') + + return int(arg) \ No newline at end of file diff --git a/project.py b/project.py index 06a9430..14f8daf 100644 --- a/project.py +++ b/project.py @@ -9,6 +9,9 @@ import sys import action +from args import get_string_arg +from args import arg_number + class Project: def __init__(self, status = None): self.name = None @@ -60,7 +63,7 @@ def handle_project(args, last_action, conn): else: delete_project(last_action.project_id, conn) else: - print('Nothing to do... And this is not normalous') + print(f'Invalid project option: {args[0]}') def parse_project_args(project, args): if not args: @@ -81,39 +84,6 @@ def parse_project_args(project, args): sys.exit(1) i += 1 -def get_string_arg(args, required): - """ - Take an array of element, take first element and return it stripped. - If array is empty or None, print an error message with required and exit - in error. - - :param args: an array of arguments. Can be empty or None - :param required: the message to print in no args (or None) - :return: The arg stripped - """ - if not args: - print(f'Missing {required}') - sys.exit(1) - - logging.debug(args) - return args[0].strip() - -# TODO Move this function -def arg_string(arg, required = ''): - # TODO To meld with get_string_args - if required and arg.startswith('-'): - print('required ', required) - sys.exit('1') - else: - return arg.strip() - -def arg_number(arg, required = ''): - if not arg.isdigit(): - print('Invalid value') # TODO - sys.exit('1') - - return int(arg) - def create_project(project, conn): # TODO Project is same name is forbidden logging.info('>> Create project') @@ -139,7 +109,7 @@ def edit_project(project, project_id, conn): if not update_args: print("nothing to update") - print("Tips: if you want to set an active, just do '{} project '" + print("Tips: if you want to set an active project, just do '{} project '" .format(os.path.basename(sys.argv[0]))) sys.exit(1) @@ -153,8 +123,9 @@ def edit_project(project, project_id, conn): logging.debug('Do a project update') cursor.execute(query, (project_id,)) - log_args = ', '.join("%s: '%s'" % (k, v) for k, v in update_args) + log_args = ', '.join("%s: %s" % (k, v) for k, v in update_args) print('updated project {}: ({})'.format(project_id, log_args)) + # TODO Bad output action.create_action(cursor, project_id, message = 'update ' + str(update_args)) @@ -217,5 +188,6 @@ def view_project_set_active(project_id, act_task_id, conn): print(' {} {:d}: ({}) [{}] [{}] {} ({} tasks)'.format('*' if act_task_id == row[0] else ' ', task_id, username, status, priority, message, nb_note)) action.create_action(cursor, project_id=project_id) + # TODO Unset active task # TODO Add a -v option to see notes? \ No newline at end of file diff --git a/task.py b/task.py index 47591c4..96811a5 100644 --- a/task.py +++ b/task.py @@ -8,7 +8,7 @@ import logging import sys import action -import project +from args import get_string_arg class Task: def __init__(self):