|
2 | 2 | import argparse, os.path |
3 | 3 | from utils.data_handlers import open_typo_file, save_typo_data |
4 | 4 |
|
5 | | -# Parse argument |
6 | | -parser = argparse.ArgumentParser(description='add new terms!') |
7 | | - |
8 | | -parser.add_argument('-wrong', dest="wrong", type=str, required=True) |
9 | | -parser.add_argument('-right', dest="right", type=str, required=True) |
10 | | -parser.add_argument('-lang', dest="lang", type=str, required=True) |
11 | | -args = parser.parse_args() |
12 | 5 |
|
13 | | -script_path = os.path.dirname(os.path.realpath(__file__)) |
14 | | -lang_path = script_path + '/words/' + args.lang + '.json' |
| 6 | +def parse_argument(_parser_): |
| 7 | + _parser_.add_argument('-wrong', dest="wrong", type=str, required=True) |
| 8 | + _parser_.add_argument('-right', dest="right", type=str, required=True) |
| 9 | + _parser_.add_argument('-lang', dest="lang", type=str, required=True) |
| 10 | + args = _parser_.parse_args() |
| 11 | + return args |
| 12 | + |
| 13 | + |
| 14 | +def store_new_argument(_args_): |
| 15 | + try: |
| 16 | + lang_path = script_path + '/words/' + _args_.lang + '.json' |
| 17 | + typo_data = open_typo_file(lang_path) |
| 18 | + typo_data[args.right].add(_args_.wrong) |
| 19 | + save_typo_data(lang_path, typo_data) |
| 20 | + except FileNotFoundError: |
| 21 | + raise ValueError('SyntaxAlert only supports en, es, fr, it at the moment.') |
15 | 22 |
|
16 | | -typo_data = open_typo_file(lang_path) |
17 | | -typo_data[args.right].add(args.wrong) |
| 23 | +# Parse argument |
| 24 | +parser = argparse.ArgumentParser(description='add new terms!') |
18 | 25 |
|
19 | | -save_typo_data(lang_path, typo_data) |
| 26 | +# Check argument is not circular |
| 27 | +if args.right == args.wrong: |
| 28 | + raise ValueError('You can’t replace a word with itself. It will create a loop.') |
| 29 | +else: |
| 30 | + # Store argument |
| 31 | + script_path = os.path.dirname(os.path.realpath(__file__)) |
| 32 | + store_new_argument(args) |
| 33 | + |
0 commit comments