1
|
|
|
# |
2
|
|
|
# Copyright (c) 2015 SUSE Linux GmbH |
3
|
|
|
# |
4
|
|
|
# This program is free software; you can redistribute it and/or |
5
|
|
|
# modify it under the terms of version 3 of the GNU General Public License as |
6
|
|
|
# published by the Free Software Foundation. |
7
|
|
|
# |
8
|
|
|
# This program is distributed in the hope that it will be useful, |
9
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11
|
|
|
# GNU General Public License for more details. |
12
|
|
|
# |
13
|
|
|
# You should have received a copy of the GNU General Public License |
14
|
|
|
# along with this program; if not, contact SUSE LLC. |
15
|
|
|
# |
16
|
|
|
# To contact SUSE about this file by physical or electronic mail, |
17
|
|
|
# you may find current contact information at www.suse.com |
18
|
|
|
|
19
|
|
|
import json |
20
|
|
|
from collections import OrderedDict |
21
|
|
|
from lxml import etree |
22
|
|
|
from prettytable import PrettyTable |
23
|
|
|
from docmanager.shellcolors import red |
24
|
|
|
|
25
|
|
|
def textrenderer(data, **kwargs): # pylint: disable=unused-argument |
26
|
|
|
"""Normal text output |
27
|
|
|
|
28
|
|
|
:param list data: Filename with properties |
29
|
|
|
syntax: [(FILENAME, {PROPERTIES}), ...] |
30
|
|
|
:param dict kwargs: for further customizations |
31
|
|
|
:return: rendered output |
32
|
|
|
:rtype: str |
33
|
|
|
""" |
34
|
|
|
|
35
|
|
|
if data is None: |
36
|
|
|
return |
37
|
|
|
|
38
|
|
|
args = kwargs["args"] |
39
|
|
|
|
40
|
|
|
if args.action == "get": |
41
|
|
|
# print only the value of the given property if only one property and |
42
|
|
|
# only one file are given |
43
|
|
|
errors = data['errors'] |
44
|
|
|
data = data['data'] |
45
|
|
|
|
46
|
|
|
if len(data) == 1: |
47
|
|
|
if len(data[0][1]) == 1: |
48
|
|
|
for v in data[0][1]: |
49
|
|
|
if data[0][1][v] is not None: |
50
|
|
|
print(data[0][1][v]) |
51
|
|
|
return |
52
|
|
|
|
53
|
|
|
# if there are more than one file or one property |
54
|
|
|
for d in data: |
55
|
|
|
props = d[1] |
56
|
|
|
props = " ".join(["%s=%s" % (key, value) \ |
57
|
|
|
for key, value in props.items()]) |
58
|
|
|
if len(props): |
59
|
|
|
print("{} -> {}".format(d[0], props)) |
60
|
|
|
|
61
|
|
|
# print all errors if -q/--quiet is not set |
62
|
|
|
if errors and not args.quiet: |
63
|
|
|
print("") |
64
|
|
|
|
65
|
|
|
for i in errors: |
66
|
|
|
print("[{}] {} -> {}".format(red(" error "), i[0], i[1])) |
67
|
|
|
elif args.action == "get_attr": |
68
|
|
|
errors = data['errors'] |
69
|
|
|
data = data['data'] |
70
|
|
|
|
71
|
|
|
if data: |
72
|
|
|
for i in data: |
73
|
|
|
if data[i]: |
74
|
|
|
for prop in data[i]: |
75
|
|
|
if data[i][prop]: |
76
|
|
|
print("{}[{}] -> {}".format(i, prop, ", ".join(["%s=%s" % (key, value) \ |
77
|
|
|
for key, value in data[i][prop].items()]))) |
78
|
|
|
|
79
|
|
|
def tablerenderer(data, **kwargs): # pylint: disable=unused-argument |
80
|
|
|
"""Output as table |
81
|
|
|
|
82
|
|
|
:param list data: Filename with properties |
83
|
|
|
syntax: [(FILENAME, {PROPERTIES}), ...] |
84
|
|
|
:param dict kwargs: for further customizations |
85
|
|
|
:return: rendered output |
86
|
|
|
:rtype: str |
87
|
|
|
""" |
88
|
|
|
if data is None: |
89
|
|
|
return |
90
|
|
|
|
91
|
|
|
args = kwargs["args"] |
92
|
|
|
|
93
|
|
|
if args.action == "alias": |
94
|
|
View Code Duplication |
if len(data['aliases']) == 0: |
|
|
|
|
95
|
|
|
print("There are no aliases in config file: {}".format(data["configfile"])) |
96
|
|
|
else: |
97
|
|
|
table = PrettyTable(["Alias", "Command"]) |
98
|
|
|
table.align["Alias"] = "l" # left align |
99
|
|
|
table.align["Command"] = "l" # left align |
100
|
|
|
|
101
|
|
|
for i in data['aliases']: |
102
|
|
|
table.add_row([i, data['aliases'][i]]) |
103
|
|
|
|
104
|
|
|
print(table) |
105
|
|
|
elif args.action == "get": |
106
|
|
|
index = 0 |
107
|
|
|
for i in data['data']: |
108
|
|
View Code Duplication |
if len(i[1]): |
|
|
|
|
109
|
|
|
filename = i[0] |
110
|
|
|
print("File: {}".format(filename)) |
111
|
|
|
table = PrettyTable(["Property", "Value"]) |
112
|
|
|
table.align["Property"] = "l" # left align |
113
|
|
|
table.align["Value"] = "l" # left align |
114
|
|
|
|
115
|
|
|
for prop in i[1]: |
116
|
|
|
value = i[1][prop] |
117
|
|
|
table.add_row([prop, value]) |
118
|
|
|
|
119
|
|
|
print(table) |
120
|
|
|
if (len(data)-1) is not index: |
121
|
|
|
print("") |
122
|
|
|
|
123
|
|
|
index += 1 |
124
|
|
|
elif args.action == "get_attr": |
125
|
|
|
if data['data']: |
126
|
|
|
# file names |
127
|
|
|
for f in data['data']: |
128
|
|
|
if data['data'][f]: |
129
|
|
|
print("File: {}".format(f)) |
130
|
|
|
|
131
|
|
|
table = PrettyTable(["Property", "Attribute", "Value"]) |
132
|
|
|
table.align["Property"] = "1" |
133
|
|
|
table.align["Attribute"] = "1" |
134
|
|
|
table.align["Value"] = "1" |
135
|
|
|
|
136
|
|
|
# properties |
137
|
|
|
for prop in data['data'][f]: |
138
|
|
|
# attributes |
139
|
|
|
for i in data['data'][f][prop]: |
140
|
|
|
table.add_row([prop, i, data['data'][f][prop][i]]) |
141
|
|
|
|
142
|
|
|
print(table) |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
def jsonrenderer(data, **kwargs): # pylint: disable=unused-argument |
146
|
|
|
"""Output as JSON |
147
|
|
|
|
148
|
|
|
:param list data: Filename with properties |
149
|
|
|
syntax: [(FILENAME, {PROPERTIES}), ...] |
150
|
|
|
:param dict kwargs: for further customizations |
151
|
|
|
:return: rendered output |
152
|
|
|
:rtype: str |
153
|
|
|
""" |
154
|
|
|
|
155
|
|
|
args = kwargs["args"] |
156
|
|
|
|
157
|
|
|
if args.action == "alias": |
158
|
|
|
json_out = OrderedDict() |
159
|
|
|
for i in data['aliases'].keys(): |
160
|
|
|
json_out[i] = {} |
161
|
|
|
json_out[i] = data['aliases'][i] |
162
|
|
|
|
163
|
|
|
print(json.dumps(json_out)) |
164
|
|
|
elif args.action == "get": |
165
|
|
|
json_out = OrderedDict() |
166
|
|
|
for i in data['data']: |
167
|
|
|
json_out[i[0]] = {} |
168
|
|
|
json_out[i[0]] = i[1] |
169
|
|
|
|
170
|
|
|
print(json.dumps(json_out)) |
171
|
|
|
elif args.action == "get_attr": |
172
|
|
|
json_out = OrderedDict() |
173
|
|
|
data = data['data'] |
174
|
|
|
|
175
|
|
|
print(json.dumps(data)) |
176
|
|
|
|
177
|
|
|
def xmlrenderer(data, **kwargs): # pylint: disable=unused-argument |
178
|
|
|
"""Output as XML |
179
|
|
|
|
180
|
|
|
:param list data: Filename with properties |
181
|
|
|
syntax: [(FILENAME, {PROPERTIES}), ...] |
182
|
|
|
:param dict kwargs: for further customizations |
183
|
|
|
:return: rendered output |
184
|
|
|
:rtype: str |
185
|
|
|
""" |
186
|
|
|
|
187
|
|
|
root = etree.Element("docmanager") |
188
|
|
|
tree = root.getroottree() |
189
|
|
|
|
190
|
|
|
args = kwargs["args"] |
191
|
|
|
index = 0 |
192
|
|
|
|
193
|
|
|
if args.action == "alias": |
194
|
|
|
aliaseselem = etree.Element("aliases") |
195
|
|
|
root.append(aliaseselem) |
196
|
|
|
|
197
|
|
|
for name in data['aliases'].keys(): |
198
|
|
|
value = data['aliases'][name] |
199
|
|
|
|
200
|
|
|
root[0].append(etree.Element("alias")) |
201
|
|
|
|
202
|
|
|
child = root[0][index] |
203
|
|
|
child.set("name", name) |
204
|
|
|
|
205
|
|
|
child.text = value |
206
|
|
|
|
207
|
|
|
index += 1 |
208
|
|
|
|
209
|
|
|
elif args.action == "get": |
210
|
|
|
fileselem = etree.Element("files") |
211
|
|
|
root.append(fileselem) |
212
|
|
|
|
213
|
|
|
for i in data['data']: |
214
|
|
|
if len(i[1]): |
215
|
|
|
filename = i[0] |
216
|
|
|
|
217
|
|
|
root[0].append(etree.Element("file")) |
218
|
|
|
|
219
|
|
|
child = root[0][index] |
220
|
|
|
child.set("name", filename) |
221
|
|
|
|
222
|
|
|
for x in i[1]: |
223
|
|
|
prop = x |
224
|
|
|
value = i[1][x] |
225
|
|
|
|
226
|
|
|
elem = etree.Element("property") |
227
|
|
|
elem.set("name", prop) |
228
|
|
|
elem.text = value |
229
|
|
|
|
230
|
|
|
child.append(elem) |
231
|
|
|
|
232
|
|
|
index += 1 |
233
|
|
|
|
234
|
|
|
elif args.action == "get_attr": |
235
|
|
|
""" |
236
|
|
|
Output structure: |
237
|
|
|
<!DOCTYPE docmanager> |
238
|
|
|
<docmanager> |
239
|
|
|
<files> |
240
|
|
|
<file name="example.xml"> |
241
|
|
|
<property name="priority/test/hello"> |
242
|
|
|
<attribute name="someattribute">Hello</attribute> |
243
|
|
|
<attribute name="attr2">Hallo</attribute> |
244
|
|
|
</property> |
245
|
|
|
<property name="status"> |
246
|
|
|
<attribute name="attr3">Hi</attribute> |
247
|
|
|
</property> |
248
|
|
|
</file> |
249
|
|
|
</files> |
250
|
|
|
</docmanager> |
251
|
|
|
""" |
252
|
|
|
|
253
|
|
|
fileselem = etree.Element("files") |
254
|
|
|
root.append(fileselem) |
255
|
|
|
|
256
|
|
|
if data['data']: |
257
|
|
|
index = 0 |
258
|
|
|
|
259
|
|
|
for i in data['data']: |
260
|
|
|
filename = i |
261
|
|
|
root[0].append(etree.Element("file")) |
262
|
|
|
|
263
|
|
|
child = root[0][index] |
264
|
|
|
child.set("name", filename) |
265
|
|
|
|
266
|
|
|
for prop in data['data'][i]: |
267
|
|
|
propelem = etree.Element("property") |
268
|
|
|
child.append(propelem) |
269
|
|
|
propelem.set("name", prop) |
270
|
|
|
|
271
|
|
|
for key, value in data['data'][i][prop].items(): |
272
|
|
|
elem = etree.Element("attribute") |
273
|
|
|
elem.set("name", key) |
274
|
|
|
elem.text = value |
275
|
|
|
|
276
|
|
|
propelem.append(elem) |
277
|
|
|
|
278
|
|
|
index += 1 |
279
|
|
|
|
280
|
|
|
print(etree.tostring(tree, |
281
|
|
|
encoding="unicode", |
282
|
|
|
pretty_print=True, |
283
|
|
|
doctype="<!DOCTYPE docmanager>")) |
284
|
|
|
|
285
|
|
|
|
286
|
|
|
DEFAULTRENDERER = textrenderer |
287
|
|
|
|
288
|
|
|
def getrenderer(fmt): |
289
|
|
|
"""Returns the renderer for a specific format |
290
|
|
|
|
291
|
|
|
:param str fmt: format ('text', 'table', 'json', or 'default') |
292
|
|
|
:return: function of renderer |
293
|
|
|
:rtype: function |
294
|
|
|
""" |
295
|
|
|
# Available renderers |
296
|
|
|
renderer = { |
297
|
|
|
'default': textrenderer, |
298
|
|
|
'text': textrenderer, |
299
|
|
|
'table': tablerenderer, |
300
|
|
|
'json': jsonrenderer, |
301
|
|
|
'xml': xmlrenderer, |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return renderer.get(fmt, DEFAULTRENDERER) |
305
|
|
|
|