-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.py
81 lines (77 loc) · 2.22 KB
/
cli.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
import argparse
import formatter
import json
import logging
logging.basicConfig(
level="INFO", format='%(name)s | %(levelname)s | %(message)s')
from sqlite.orm import create_all, insert_from_json
PARGS = argparse.ArgumentParser(
description="simple CLI for running and setting up the project.")
PARGS.add_argument(
'--mode',
'-m',
help=
"pick your mode. 'format' to normalize json from scrape. 'create' creates the tables, and inserts from your infile, cleanup normalizes the database.",
choices=['format', 'create'],
required=True,
type=str)
PARGS.add_argument(
'--themes',
'-t',
help="fix lyrical themes from scrape.",
action="store_true",
default=False)
PARGS.add_argument(
'--genres',
'-g',
help="fix genres from scrape.",
action="store_true",
default=False)
PARGS.add_argument(
'--infile',
'-i',
help="file to fix, only supports json.",
type=str,
default='./json/items.json')
PARGS.add_argument(
'--outfile',
'-o',
help="custom named output file, only supports json, specify path",
type=str,
default="./json/fixed_bands.json")
PARGS.add_argument(
'--pretty',
'-p',
help="pretty print the json file",
action="store_true",
default=False)
PARGS.add_argument(
'--verbose',
'-v',
help='sets logging level for debugging',
action='store_true',
default=False)
if __name__ == "__main__":
args = PARGS.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
logger = logging.getLogger('cli')
logger.debug(f'got args: {args}')
if args.infile:
with open(args.infile, "r") as f: # both format and
bandlist = json.load(f, encoding='utf-16')
if args.mode in "format".lower():
if args.themes:
bandlist = formatter.theme_formatter(bandlist)
if args.genres:
bandlist = formatter.genre_formatter(bandlist)
with open(args.outfile, 'w+') as of:
if args.pretty:
json.dump(bandlist, of, indent=4)
else:
json.dump(bandlist, of)
of.close()
if args.mode in "create".lower():
create_all()
insert_from_json(bandlist)
exit