-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_tables.py
More file actions
104 lines (83 loc) · 2.59 KB
/
export_tables.py
File metadata and controls
104 lines (83 loc) · 2.59 KB
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
100
101
102
103
104
"""[summary]
Raises:
Exception -- [description]
Exception -- [description]
Returns:
[type] -- [description]
"""
import argparse
import glob
import json
import os
from processors.utils.export import export_tables as export
ALLOWED_EXTENSIONS = ["json"]
def get_files(src):
"""[summary]
Arguments:
src {[type]} -- [description]
Returns:
[type] -- [description]
"""
files = []
for extension in ALLOWED_EXTENSIONS:
ext_files = glob.glob(os.path.join(
src, "**/*." + extension), recursive=True)
files += ext_files
return files
def process_files(files, dst, fmt):
"""[summary]
Arguments:
files {[type]} -- [description]
dst {[type]} -- [description]
fmt {[type]} -- [description]
"""
for file in files:
document = json.load(open(file))
_, file_name = os.path.split(file)
name, _ = os.path.splitext(file_name)
dst_dir = os.path.join(dst, name)
os.makedirs(dst_dir, exist_ok=True)
export(document, dst_dir, name, fmt)
def run(src, dst, fmt):
"""[summary]
Arguments:
src {[type]} -- [description]
dst {[type]} -- [description]
fmt {[type]} -- [description]
Raises:
Exception -- [description]
Exception -- [description]
"""
src = os.path.abspath(src)
dst = os.path.abspath(dst)
if not os.path.exists(src):
raise Exception("Directory/File (%s) does not exist." % (src))
files = get_files(src)
if not files:
raise Exception("Found 0 files in %s" % (src))
os.makedirs(dst, exist_ok=True)
process_files(files, dst, fmt)
if __name__ == '__main__':
PARSER = argparse.ArgumentParser(
"Command line arguments for Table Export from JSON Document(s)")
PARSER.add_argument("-s",
"--src",
type=str,
required=True,
dest="src",
help="Source directory of files/Single JSON file")
PARSER.add_argument("-d",
"--dst",
type=str,
required=True,
dest="dst",
help="Destination directory")
PARSER.add_argument("-f",
"--fmt",
type=str,
required=True,
choices=["txt", "csv", "xlsx", "df"],
dest="fmt",
help="Format to write Tables.")
FLAGS = PARSER.parse_args()
run(**vars(FLAGS))