-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgensql.py
87 lines (82 loc) · 3.25 KB
/
gensql.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
from pathlib import PurePath
from textwrap import dedent
import sys
from gensql.generator import Generator
from gensql.runner import Runner
from gensql.validator import Validator
from exceptions.exceptions import (
OutputFilePermissionError,
OverwriteFileError,
)
from utilities import utilities
if __name__ == "__main__":
warnings = []
utils = utilities.Utilities()
h = utilities.Help()
args = utilities.Args().make_args()
v = Validator(args)
g = Generator(args)
if not args.debug:
sys.tracebacklimit = 0
if args.extended_help:
h.extended_help()
elif args.validate:
if v.validate_schema(v.parse_schema()):
# \u2704 == green checkmark
print("\u2705 INFO: validated schema, no errors detected")
raise SystemExit(0)
elif args.generate:
# due to the multi-line string output, there's a leading newline to remove
skeleton = dedent(h.make_skeleton())[1:]
try:
try:
filename = PurePath(args.output).with_suffix(".json")
except TypeError:
filename = "skeleton.json"
with open(
f"schema_inputs/{filename}", f"{'w' if args.force else 'x'}"
) as f:
f.write(skeleton)
raise SystemExit(0)
except FileExistsError:
raise OverwriteFileError(filename) from None
except PermissionError:
raise OutputFilePermissionError(filename) from None
elif args.dates:
if args.output:
warnings.append(f"--output {args.output}")
if args.filetype:
warnings.append(f"--filetype {args.filetype}")
try:
filename = "content/dates.txt"
with open(filename, f"{'w' if args.force else 'x'}") as f:
dates = [x + "\n" for x in g.make_dates(args.num)]
f.writelines(dates)
if warnings:
for w in warnings:
print(f"INFO: ignoring option {w} for datetime creation")
print(f"INFO: {args.num} datetimes created at content/dates.txt")
raise SystemExit(0)
except FileExistsError:
raise OverwriteFileError(filename) from None
except PermissionError:
raise OutputFilePermissionError(filename) from None
try:
tbl_name = args.table or args.output or PurePath(args.input).stem
except TypeError:
tbl_name = "gensql"
schema_dict = v.parse_schema()
schema_dict = utils.lowercase_schema(schema_dict)
v.validate_schema(schema_dict)
tbl_create, tbl_cols = g.mysql(schema_dict, tbl_name, args.drop_table)
# generally, there isn't a good reason to insert values manually into an auto-incrementing col
auto_inc_cols = [x for x in tbl_cols.keys() if tbl_cols[x].get("auto_inc")]
# if the user has hinted that a column is empty, it should only be created, not given inserts
empty_cols = [x for x in tbl_cols.keys() if tbl_cols[x].get("is_empty")]
unique_cols = [x for x in tbl_cols.keys() if tbl_cols[x].get("unique")]
for x in auto_inc_cols:
del tbl_cols[x]
for x in empty_cols:
del tbl_cols[x]
r = Runner(args, schema_dict, tbl_name, tbl_cols, tbl_create, unique_cols)
r.run()