From a8e422fbda565a2083152b3ec4f6c1398616af04 Mon Sep 17 00:00:00 2001 From: Bertil Hatt Date: Tue, 16 Oct 2018 20:33:57 +0100 Subject: [PATCH] Error handling for circular references (#36) - error handling for: - circular references; - use of non-supported languages; - some modularity. --- manageterms.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/manageterms.py b/manageterms.py index 44c93fd..fe8fed2 100755 --- a/manageterms.py +++ b/manageterms.py @@ -2,18 +2,32 @@ import argparse, os.path from utils.data_handlers import open_typo_file, save_typo_data -# Parse argument -parser = argparse.ArgumentParser(description='add new terms!') - -parser.add_argument('-wrong', dest="wrong", type=str, required=True) -parser.add_argument('-right', dest="right", type=str, required=True) -parser.add_argument('-lang', dest="lang", type=str, required=True) -args = parser.parse_args() -script_path = os.path.dirname(os.path.realpath(__file__)) -lang_path = script_path + '/words/' + args.lang + '.json' +def parse_argument(_parser_): + _parser_.add_argument('-wrong', dest="wrong", type=str, required=True) + _parser_.add_argument('-right', dest="right", type=str, required=True) + _parser_.add_argument('-lang', dest="lang", type=str, required=True) + args = _parser_.parse_args() + return args + + +def store_new_argument(_args_): + try: + lang_path = script_path + '/words/' + _args_.lang + '.json' + typo_data = open_typo_file(lang_path) + typo_data[args.right].add(_args_.wrong) + save_typo_data(lang_path, typo_data) + except FileNotFoundError: + raise ValueError('SyntaxAlert only supports en, es, fr, it at the moment.') -typo_data = open_typo_file(lang_path) -typo_data[args.right].add(args.wrong) +# Parse argument +parser = argparse.ArgumentParser(description='add new terms!') -save_typo_data(lang_path, typo_data) +# Check argument is not circular +if args.right == args.wrong: + raise ValueError('You can’t replace a word with itself. It will create a loop.') +else: + # Store argument + script_path = os.path.dirname(os.path.realpath(__file__)) + store_new_argument(args) +