Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit a8e422f

Browse files
bertilhattMte90
authored andcommitted
Error handling for circular references (#36)
- error handling for: - circular references; - use of non-supported languages; - some modularity.
1 parent e193886 commit a8e422f

1 file changed

Lines changed: 26 additions & 12 deletions

File tree

manageterms.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@
22
import argparse, os.path
33
from utils.data_handlers import open_typo_file, save_typo_data
44

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()
125

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.')
1522

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!')
1825

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

Comments
 (0)