-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
241 lines (189 loc) · 8.34 KB
/
parser.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
""" Compact OwnCloud/NextCloud WebDAV client parse classes
Author: hevp
"""
import re
import copy
from lxml import etree
from dateutil.parser import parse as dateparse
from common import makeHuman, relativePath
class ParserFactory():
@staticmethod
def getParser(p, data, options):
if p.get('scope', '') == 'headers':
parser = HeadersParser(p, options)
elif p.get('scope', '') == 'response':
if type(data) is etree._Element:
if options["operation"] == 'list':
parser = ListXMLResponseParser(p, options)
else:
parser = XMLResponseParser(p, options)
elif type(data) is dict:
parser = JSONResponseParser(p, options)
else:
parser = ResponseParser(p, options)
else:
parser = Parser(p, options)
return parser.run(data)
class Parser(dict):
def __init__(self, defs, options={}, resultInit=None):
self.update(defs)
self.options = options
self.result = resultInit
def _pre(self, data):
pass
def _parse(self, data):
pass
def _post(self, data):
pass
def run(self, data):
self._pre(data)
self._parse(data)
self._post(data)
return self
def result(self):
return self.result
def filter(self):
pass
def format(self):
return copy.deepcopy(self.result)
class HeadersParser(Parser):
pass
class ResponseParser(Parser):
pass
class XMLResponseParser(ResponseParser):
def __init__(self, data, options={}):
super().__init__(data, options, [])
def _parse(self, data):
super()._parse(data)
# get XML namespace map, excluding default namespace
nsmap = {k: v for k, v in data.nsmap.items() if k}
# get main namespace from root element
ns = data.prefix
# process result elements
for child in data.findall(f".//{ns}:{self['items']}", nsmap):
variables = {}
for var, varv in self["variables"].items():
for paths in varv["xpath"].split('|'):
if var in variables:
break
p = f".//{ns}:" + f"/{ns}:".join(paths.split('/'))
v = child.find(p, nsmap)
# note: booleans are stored invertedly due to sorting algorithm
if v is not None:
if "type" in varv and varv["type"] == "bool":
variables[var] = "0"
elif "type" in varv and varv["type"] == "enum":
if "values" in varv and "present" in varv["values"]:
variables[var] = varv["values"]["present"]
else:
variables[var] = v.text
elif v.text is not None:
if "type" in varv and varv["type"] == "int":
variables[var] = int(v.text)
# treat as string
else:
variables[var] = v.text
elif "type" in varv and varv["type"] == "bool":
variables[var] = "1"
elif "type" in varv and varv["type"] == "enum":
if "values" in varv and "absent" in varv["values"]:
variables[var] = varv["values"]["absent"]
else:
variables[var] = v.text
# add to results
self.result.append(variables)
def _post(self, data):
# apply sorting etc
if self.options['sort'] and self.options['dirs-first']:
self.result.sort(key=lambda x: x['type'] + x['path'].lower(), reverse=self.options['reverse'])
elif self.options['sort']:
self.result.sort(key=lambda x: x['path'].lower(), reverse=self.options['reverse'])
elif self.options['dirs-first']:
self.result.sort(key=lambda x: x['type'], reverse=self.options['reverse'])
if self.options['hide-root'] and len(self.result):
self.result = self.result[1:]
class ListXMLResponseParser(XMLResponseParser):
def _post(self, data):
super()._post(data)
# filtering
if self.options['list-empty']:
self.result = list(filter(lambda x: x['type'] == 'd' and x['size'] == 0, self.result))
# filter dirs (wins) or files
if self.options['dirs-only']:
self.result = list(filter(lambda x: x['type'] == 'd', self.result))
elif self.options['files-only']:
self.result = list(filter(lambda x: x['type'] == 'f', self.result))
def format(self):
printResult = ""
printf = self.options.get("printf", "")
# find {<varname>:<length>}
matching = re.findall(r'{([^}:]+):?([^}]+)?}', printf)
if not matching:
return printf
# loop through result list
results = []
for item in self.result:
if self.options['recursive'] and item['type'] == 'd':
continue
# determine and possibly update all found variables
result = copy.deepcopy(item)
for var in matching:
# if found variable exists
if var[0] in result:
# get the original value
val = result[var[0]]
# special treatment per variable
if var[0] == "path":
val = relativePath(result, var[0], self.options["root"], self.options["credentials"]["endpoint"])
elif var[0] == "date":
val = dateparse(val).strftime("%Y-%m-%d %H:%M:%S")
elif var[0] == "size":
val = makeHuman(val)
else:
val = None
# update temporary result array with sanity check
result[var[0]] = val if val else ""
results.append(result)
# determine maximum lengths of each field when necessary
maxs = {}
for var in matching:
if var[1] > '' and not var[1].isdigit():
# determine maximum length of all elements for this variable
# store as string
lengths = list(map(lambda x: len(x[var[0]]), results))
maxs[var[0]] = str(max(lengths)) if len(lengths) > 0 else '0'
# list all elements
for result in results:
text = printf
# determine all variables
for var in matching:
val = result[var[0]]
# justification
if var[1] > '':
if var[1].isdigit():
val = ("%" + var[1] + "s") % val
elif var[1] == 'l':
val = ("%-" + maxs[var[0]] + "s") % val
elif var[1] == 'r':
val = ("%" + maxs[var[0]] + "s") % val
# replace variable with value
text = text.replace("{%s}" % ":".join(filter(lambda x: x > '', list(var))), val)
# print resulting string
printResult += "%s\n" % text
return printResult
def format_summary(self):
# filter out any directory if recursive
res = copy.deepcopy(self.result) if not self.options['recursive'] else list(filter(lambda x: x['type'] != "d", self.result))
# get total size, directory and file counts
lsum = sum(map(lambda x: x['size'], res))
dcount = len(list(filter(lambda x: x['type'] == 'd', res)))
fcount = len(res) - dcount
# print total size, file count if > 0, directory count if > 0
return "%s %s%s%s%s%s\n" % ("\n" if len(self.result) > 0 else "",
makeHuman(lsum, len(self.result)),
" in " if len(self.result) > 0 else "",
"%d file%s" % (fcount, "s" if fcount != 1 else "") if fcount > 0 else "",
" and " if (dcount > 0 and fcount > 0) else "",
"%d director%s" % (dcount, "ies" if dcount != 1 else "y") if dcount > 0 else "")
class JSONResponseParser(ResponseParser):
pass