1
1
import sys
2
+ import importlib .util
2
3
import os .path
3
4
import json
4
5
import zschema .registry
5
6
import argparse
6
7
7
- from imp import load_source
8
8
from importlib import import_module
9
9
from site import addsitedir
10
10
11
- from leaves import *
12
- from keys import *
13
- from compounds import *
11
+ from . leaves import *
12
+ from . keys import *
13
+ from . compounds import *
14
14
15
15
commands = [
16
16
"bigquery" ,
20
20
"docs-es" ,
21
21
"validate" ,
22
22
"flat" ,
23
- "json"
23
+ "json" ,
24
24
]
25
25
26
26
cmdList = ", " .join (commands )
27
27
28
28
parser = argparse .ArgumentParser (
29
29
prog = "zschema" ,
30
- description = "Process a zschema definition. "
31
- "VERSION: %s" % zschema .__version__ )
32
-
33
- parser .add_argument ("command" ,
34
- metavar = "command" , choices = commands ,
35
- help = "The command to execute; one of [ %s ]" % cmdList )
36
-
37
- parser .add_argument ("schema" ,
38
- help = "The name of the schema in the zschema.registry. "
39
- "For backwards compatibility, a filename can be "
40
- "prefixed with a colon, as in 'schema.py:my-type'." )
41
-
42
- parser .add_argument ("target" , nargs = "?" ,
43
- help = "Only used for the validate command. "
44
- "The input JSON file that will be checked against "
45
- "the schema." )
30
+ description = "Process a zschema definition. " "VERSION: %s" % zschema .__version__ ,
31
+ )
32
+
33
+ parser .add_argument (
34
+ "command" ,
35
+ metavar = "command" ,
36
+ choices = commands ,
37
+ help = "The command to execute; one of [ %s ]" % cmdList ,
38
+ )
39
+
40
+ parser .add_argument (
41
+ "schema" ,
42
+ help = "The name of the schema in the zschema.registry. "
43
+ "For backwards compatibility, a filename can be "
44
+ "prefixed with a colon, as in 'schema.py:my-type'." ,
45
+ )
46
+
47
+ parser .add_argument (
48
+ "target" ,
49
+ nargs = "?" ,
50
+ help = "Only used for the validate command. "
51
+ "The input JSON file that will be checked against "
52
+ "the schema." ,
53
+ )
46
54
47
55
parser .add_argument ("--module" , help = "The name of a module to import." )
48
56
49
- parser .add_argument ("--validation-policy" , help = "What to do when a validation "
50
- "error occurs. This only overrides the top-level Record. It does not "
51
- "override subrecords. Default: error." , choices = ["ignore" , "warn" , "error" ],
52
- default = None )
53
-
54
- parser .add_argument ("--validation-policy-override" , help = "Override validation "
55
- "policy for all levels of the schema." , choices = ["ignore" , "warn" , "error" ],
56
- default = None )
57
-
58
- parser .add_argument ("--path" , nargs = "*" ,
59
- help = "Additional PYTHONPATH directories to include." )
57
+ parser .add_argument (
58
+ "--validation-policy" ,
59
+ help = "What to do when a validation "
60
+ "error occurs. This only overrides the top-level Record. It does not "
61
+ "override subrecords. Default: error." ,
62
+ choices = ["ignore" , "warn" , "error" ],
63
+ default = None ,
64
+ )
65
+
66
+ parser .add_argument (
67
+ "--validation-policy-override" ,
68
+ help = "Override validation " "policy for all levels of the schema." ,
69
+ choices = ["ignore" , "warn" , "error" ],
70
+ default = None ,
71
+ )
72
+
73
+ parser .add_argument (
74
+ "--path" , nargs = "*" , help = "Additional PYTHONPATH directories to include."
75
+ )
60
76
61
77
args = parser .parse_args ()
62
78
@@ -71,7 +87,7 @@ def main():
71
87
# Backwards compatibility: given "file.py:schema", load file.py.
72
88
if ":" in schema :
73
89
path , recname = schema .split (":" )
74
- load_source (' module' , path )
90
+ load_source (" module" , path )
75
91
schema = recname
76
92
77
93
if args .module :
@@ -82,31 +98,39 @@ def main():
82
98
record .set ("validation_policy" , args .validation_policy )
83
99
command = args .command
84
100
if command == "bigquery" :
85
- print json .dumps (record .to_bigquery ())
101
+ print ( json .dumps (record .to_bigquery () ))
86
102
elif command == "elasticsearch" :
87
- print json .dumps (record .to_es (recname ))
103
+ print ( json .dumps (record .to_es (recname ) ))
88
104
elif command == "proto" :
89
- print record .to_proto (recname )
105
+ print ( record .to_proto (recname ) )
90
106
elif command == "docs-es" :
91
- print json .dumps (record .docs_es (recname ))
107
+ print ( json .dumps (record .docs_es (recname ) ))
92
108
elif command == "docs-bq" :
93
- print json .dumps (record .docs_bq (recname ))
109
+ print ( json .dumps (record .docs_bq (recname ) ))
94
110
elif command == "json" :
95
- print record .to_json ()
111
+ print ( record .to_json () )
96
112
elif command == "flat" :
97
113
for r in record .to_flat ():
98
- print json .dumps (r )
114
+ print ( json .dumps (r ) )
99
115
elif command == "validate" :
100
116
if not os .path .exists (args .target ):
101
117
sys .stderr .write ("Invalid test file. %s does not exist.\n " % args .target )
102
118
sys .exit (1 )
103
119
with open (args .target ) as fd :
104
120
for line in fd :
105
- record .validate (json .loads (line .strip ()),
106
- args .validation_policy_override )
121
+ record .validate (
122
+ json .loads (line .strip ()), args .validation_policy_override
123
+ )
107
124
else :
108
125
usage ()
109
126
110
127
128
+ def load_source (name , path ):
129
+ spec = importlib .util .spec_from_file_location (name , path )
130
+ module = importlib .util .module_from_spec (spec )
131
+ spec .loader .exec_module (module )
132
+ return module
133
+
134
+
111
135
if __name__ == "__main__" :
112
136
main ()
0 commit comments