-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
99 lines (90 loc) · 3.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Run with:
./scripts/convert_schema.bat inputs/input_file.txt outputs/output_file.txt 1NF
or
python3 main.py --input_file=inputs/input.txt --output_file=outputs/output.txt --normal_form=0NF --output=SQL
Test scripts:
.\scripts\convert_schema.bat inputs/0NF_test_1.txt outputs/0NF_target_1.txt 0NF
.\scripts\convert_schema.bat inputs/0NF_test_1.txt outputs/1NF_target_1.txt 1NF
.\scripts\convert_schema.bat inputs/1NF_test_1.txt outputs/2NF_target_1.txt 2NF
"""
import argparse
from modules import preprocess, normalize
normal_forms = {
"0NF": normalize.zero_nf,
"1NF": normalize.first_nf,
"2NF": normalize.second_nf,
"3NF": normalize.third_nf,
"BCNF": normalize.bcnf,
"4NF": normalize.fourth_nf,
"5NF": normalize.fifth_nf,
}
def main(args: argparse.Namespace):
"""
Description:
Run the normalizer based on the inputted arguments
Input:
args - Namespace: Arguments from the command line
input_file: File path to the input file
output_file: File path to the output file
Set to stdio to print to the console instead
normal_form: Normal form to normalize the input file to - 0NF, 1NF, 2NF, 3NF, BCNF, 4NF, or 5NF
target: Target output format - set to SQL to output SQL commands instead of a textual schema
Output:
Sends the output to the desired output file, or the stdio console if specified
"""
if not args.normal_form in normal_forms.keys():
raise ValueError(
f"Invalid normal form {args.normal_form} - must be one of {normal_forms.keys()}"
)
relations = preprocess.process_input(args.input_file)
for normal_form, function in normal_forms.items():
relations = function(relations)
if normal_form == args.normal_form:
break
if args.output_file and args.output_file != "stdio":
output = ""
with open(args.output_file, "w") as file:
if args.target == "sql":
for current_relation in relations:
for command in current_relation.to_sql():
output += command + "\n"
output += "\n"
else:
for current_relation in relations:
output += str(current_relation)
file.write(output.removesuffix("\n\n"))
print(f"Wrote output to {args.output_file}")
else:
for current_relation in relations:
print(current_relation)
for command in current_relation.to_sql():
print(command)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Normalize the relations from the inputted .txt file"
)
parser.add_argument(
"--input_file",
type=str,
required=True,
help="Name of the .txt file to input, like 'inputs/1NF_test_1.txt'",
)
parser.add_argument(
"--output_file",
type=str,
required=False,
help="Name of the .txt file to output to, like 'outputs/1NF_output_1.txt'",
)
parser.add_argument(
"--normal_form",
type=str,
required=True,
help="Normal form to normalize the input file to - 1NF, 2NF, 3NF, BCNF, 4NF, or 5NF'",
)
parser.add_argument(
"--target",
type=str,
required=False,
)
main(parser.parse_args())