forked from snychka/python-decoding-sensor-data
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
Latest commit
188 lines (144 loc) · 5.52 KB
/
Copy pathconftest.py
File metadata and controls
188 lines (144 loc) · 5.52 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
importast
importjson
importparso
importpytest
fromcollectionsimportOrderedDict
fromtypesimportGeneratorTypeasgenerator
fromitertoolsimportchain
frompathlibimportPath
fromobjectpathimportTree
frommongoqueryimportQuery
fromtests.nodesimportconvert_node, flatten
fromtests.templateimportTemplate
classParser:
def__init__(self, file_name, nodes):
sensor=Path.cwd() /"sensor"
# ext = sensor / "extensions"
self.data= {
"success": True,
"full_path": "",
"message": "",
"start_pos": 0,
"nodes": nodes,
}
iffile_nameisnotNone:
path=lambdaroot, fn: root/"{}.py".format(fn)
# if file_name == "menu" or file_name == "stats":
# full_path = path(ext, file_name)
iffile_name=="sensor":
full_path=path(Path.cwd(), file_name)
else:
full_path=path(sensor, file_name)
grammar=parso.load_grammar()
module=grammar.parse(path=full_path)
self.data["success"] =len(grammar.iter_errors(module)) ==0
ifself.data["success"]:
self.data["message"] ="Syntax: valid"
iffile_nameisnotNone:
self.data["nodes"] =convert_node(ast.parse(full_path.read_text()))
self.data["code"] =full_path.read_text()
else:
self.data["message"] =grammar.iter_errors(module)[0].message
self.data["start_pos"] =grammar.iter_errors(module)[0].start_pos[0]
@property
defnodes(self):
returnself.data["nodes"]
n=nodes
@property
defsuccess(self):
returnself.data["success"]
@property
defcode(self):
returnself.data["code"]
defquery(self, pattern):
nodes=Template(pattern).process(self.code)
ifisinstance(nodes, list) andlen(nodes) ==1:
nodes=nodes[0]
returnParser(None, nodes)
defquery_raw(self, pattern):
nodes=Template(pattern).process(self.code, True)
ifisinstance(nodes, list) andlen(nodes) ==1:
nodes= [flatten(convert_node(node)) fornodeinnodes[0].body]
returnParser(None, nodes)
deflast_line(self):
returnflatten(self.nodes["body"][-1])
@property
defmessage(self):
return"{} on or around line {} in `{}`.".format(
self.data["message"], self.data["start_pos"], self.data["full_path"]
)
defmatch(self, template):
returnParser(None, list(filter(Query(template).match, self.nodes)))
defexecute(self, expr):
result=Tree(self.nodes).execute(expr)
ifisinstance(result, (generator, chain, map)):
process=list(result)
return (
Parser(None, process[0]) iflen(process) ==1elseParser(None, process)
)
else:
returnParser(None, result)
ex=execute
defexists(self):
returnbool(self.nodes)
defcalls(self):
nodes=self.execute("$.body[@.type is 'Expr' and @.value.type is 'Call']").n
node_list= [nodes] ifisinstance(nodes, dict) elsenodes
returnParser(None, [flatten(node) fornodeinnode_list])
defassign_(self):
returnParser(None, [flatten(self.execute("$.body[@.type is 'Assign']").n)])
defdef_args_(self, name):
returnParser(None, [flatten(self.execute("$.body[@.type is 'FunctionDef' and @.name is '{}']".format(
name
)
).n)])
defassigns(self):
returnParser(
None,
[flatten(node) fornodeinself.execute("$.body[@.type is 'Assign']").n],
)
defglobals(self, name):
returnnameinself.execute("$.body[@.type is 'Global'].names").n
defdefines(self, name):
returnself.execute(
"$.body[@.type is 'FunctionDef' and @.name is '{}'].(name, args, body, decorator_list)".format(
name
)
)
defclass_(self, name):
returnself.execute(
"$.body[@.type is 'ClassDef' and @.name is '{}'].(name, args, body)".format(
name
)
)
defdecorators(self):
returnParser(None, [flatten(self.execute("$.decorator_list").n)])
defreturns(self, name):
returnname==self.execute("$.body[@.type is 'Return'].value.id").n
defreturns_call(self):
returnParser(None, [flatten(self.execute("$.body[@.type is 'Return']").n)])
defmethod(self, name):
returnself.execute(
"$..body[@.type is 'FunctionDef' and @.name is '{}']".format(name)
)
defhas_arg(self, name, pos=0):
nodes=self.execute("$.args.args.arg").n
returnnodes[pos] ifisinstance(nodes, list) elsenodes
defimports(self, name):
returnnameinself.execute("$.body[@.type is 'Import'].names..name").n
deffor_(self):
for_body=self.execute("$.body[@.type is 'For'].body").n
iterators=self.execute("$.body[@.type is 'For'].(target, iter)").n
returnParser(None, [flatten(for_body), flatten(iterators)])
deffrom_imports(self, mod, alias):
nodes=self.execute(
"$.body[@.type is 'ImportFrom' and @.module is '{}'].names..name".format(
mod
)
).n
returnaliasin (nodesifisinstance(nodes, list) else [nodes])
@pytest.fixture
defparse():
def_parse(file_name):
returnParser(file_name, {})
return_parse