-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlsh.py
688 lines (638 loc) · 26.1 KB
/
xmlsh.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/usr/bin/python
# Script created by Isox 18.11.2011
# Update 21.11.2011 : Added etree lib for preserver attributes order.
# Update 22.11.2011 : Removed minidom. All parsing made on etree. Added "add", "settext" commands. Improved "cd".
#
# This XMLsh utility will help you to agregate state.db file
# With best regards.
from subprocess import call
try:
import xml.etree.ElementTree as etree
except:
import elementtree.ElementTree as etree
import sys
ver=list(sys.version.split())[0]
try:
from thread import get_ident as _get_ident
except:
from collections import OrderedDict as odict
else:
class _Nil(object):
def __repr__(self):
return "nil"
def __eq__(self, other):
if (isinstance(other, _Nil)):
return True
else:
return NotImplemented
def __ne__(self, other):
if (isinstance(other, _Nil)):
return False
else:
return NotImplemented
_nil = _Nil()
class _odict(object):
def _dict_impl(self):
return None
def __init__(self, data=(), **kwds):
if kwds:
raise TypeError(" ")
self._dict_impl().__init__(self)
# If you give a normal dict, then the order of elements is undefined
if hasattr(data, "iteritems"):
for key, val in data.iteritems():
self[key] = val
else:
for key, val in data:
self[key] = val
# Double-linked list header
def _get_lh(self):
dict_impl = self._dict_impl()
if not hasattr(self, '_lh'):
dict_impl.__setattr__(self, '_lh', _nil)
return dict_impl.__getattribute__(self, '_lh')
def _set_lh(self, val):
self._dict_impl().__setattr__(self, '_lh', val)
lh = property(_get_lh, _set_lh)
# Double-linked list tail
def _get_lt(self):
dict_impl = self._dict_impl()
if not hasattr(self, '_lt'):
dict_impl.__setattr__(self, '_lt', _nil)
return dict_impl.__getattribute__(self, '_lt')
def _set_lt(self, val):
self._dict_impl().__setattr__(self, '_lt', val)
lt = property(_get_lt, _set_lt)
def __getitem__(self, key):
return self._dict_impl().__getitem__(self, key)[1]
def __setitem__(self, key, val):
dict_impl = self._dict_impl()
try:
dict_impl.__getitem__(self, key)[1] = val
except KeyError:
new = [dict_impl.__getattribute__(self, 'lt'), val, _nil]
dict_impl.__setitem__(self, key, new)
if dict_impl.__getattribute__(self, 'lt') == _nil:
dict_impl.__setattr__(self, 'lh', key)
else:
dict_impl.__getitem__(
self, dict_impl.__getattribute__(self, 'lt'))[2] = key
dict_impl.__setattr__(self, 'lt', key)
def __delitem__(self, key):
dict_impl = self._dict_impl()
pred, _ ,succ= self._dict_impl().__getitem__(self, key)
if pred == _nil:
dict_impl.__setattr__(self, 'lh', succ)
else:
dict_impl.__getitem__(self, pred)[2] = succ
if succ == _nil:
dict_impl.__setattr__(self, 'lt', pred)
else:
dict_impl.__getitem__(self, succ)[0] = pred
dict_impl.__delitem__(self, key)
def __contains__(self, key):
# XXX: try: self[key] ...
return key in self.keys()
def has_key(self, key):
return key in self
def __len__(self):
return len(self.keys())
def __str__(self):
pairs = ("%r: %r" % (k, v) for k, v in self.iteritems())
return "{%s}" % ", ".join(pairs)
def __repr__(self):
if self:
pairs = ("(%r, %r)" % (k, v) for k, v in self.iteritems())
return "odict([%s])" % ", ".join(pairs)
else:
return "odict()"
def get(self, k, x=None):
if k in self:
return self._dict_impl().__getitem__(self, k)[1]
else:
return x
def __iter__(self):
dict_impl = self._dict_impl()
curr_key = dict_impl.__getattribute__(self, 'lh')
while curr_key != _nil:
yield curr_key
curr_key = dict_impl.__getitem__(self, curr_key)[2]
iterkeys = __iter__
def keys(self):
return list(self.iterkeys())
def itervalues(self):
dict_impl = self._dict_impl()
curr_key = dict_impl.__getattribute__(self, 'lh')
while curr_key != _nil:
_, val, curr_key = dict_impl.__getitem__(self, curr_key)
yield val
def values(self):
return list(self.itervalues())
def iteritems(self):
dict_impl = self._dict_impl()
curr_key = dict_impl.__getattribute__(self, 'lh')
while curr_key != _nil:
_, val, next_key = dict_impl.__getitem__(self, curr_key)
yield curr_key, val
curr_key = next_key
def items(self):
return list(self.iteritems())
def sort(self, cmp=None, key=None, reverse=False):
items = [(k, v) for k,v in self.items()]
if cmp is not None:
items = sorted(items, cmp=cmp)
elif key is not None:
items = sorted(items, key=key)
else:
items = sorted(items, key=lambda x: x[1])
if reverse:
items.reverse()
self.clear()
self.__init__(items)
def clear(self):
dict_impl = self._dict_impl()
dict_impl.clear(self)
dict_impl.__setattr__(self, 'lh', _nil)
dict_impl.__setattr__(self, 'lt', _nil)
def copy(self):
return self.__class__(self)
def update(self, data=(), **kwds):
if kwds:
raise TypeError("update() of ordered dict takes no keyword "
"arguments to avoid an ordering trap.")
if hasattr(data, "iteritems"):
data = data.iteritems()
for key, val in data:
self[key] = val
def setdefault(self, k, x=None):
try:
return self[k]
except KeyError:
self[k] = x
return x
def pop(self, k, x=_nil):
try:
val = self[k]
del self[k]
return val
except KeyError:
if x == _nil:
raise
return x
def popitem(self):
try:
dict_impl = self._dict_impl()
key = dict_impl.__getattribute__(self, 'lt')
return key, self.pop(key)
except KeyError:
raise KeyError("'popitem(): ordered dictionary is empty'")
def riterkeys(self):
"""To iterate on keys in reversed order.
"""
dict_impl = self._dict_impl()
curr_key = dict_impl.__getattribute__(self, 'lt')
while curr_key != _nil:
yield curr_key
curr_key = dict_impl.__getitem__(self, curr_key)[0]
__reversed__ = riterkeys
def rkeys(self):
"""List of the keys in reversed order.
"""
return list(self.riterkeys())
def ritervalues(self):
"""To iterate on values in reversed order.
"""
dict_impl = self._dict_impl()
curr_key = dict_impl.__getattribute__(self, 'lt')
while curr_key != _nil:
curr_key, val, _ = dict_impl.__getitem__(self, curr_key)
yield val
def rvalues(self):
"""List of the values in reversed order.
"""
return list(self.ritervalues())
def riteritems(self):
"""To iterate on (key, value) in reversed order.
"""
dict_impl = self._dict_impl()
curr_key = dict_impl.__getattribute__(self, 'lt')
while curr_key != _nil:
pred_key, val, _ = dict_impl.__getitem__(self, curr_key)
yield curr_key, val
curr_key = pred_key
def ritems(self):
"""List of the (key, value) in reversed order.
"""
return list(self.riteritems())
def firstkey(self):
if self:
return self._dict_impl().__getattribute__(self, 'lh')
else:
raise KeyError("'firstkey(): ordered dictionary is empty'")
def lastkey(self):
if self:
return self._dict_impl().__getattribute__(self, 'lt')
else:
raise KeyError("'lastkey(): ordered dictionary is empty'")
def as_dict(self):
return self._dict_impl()(self.items())
def _repr(self):
"""_repr(): low level repr of the whole data contained in the odict.
Useful for debugging.
"""
dict_impl = self._dict_impl()
form = "odict low level repr lh,lt,data: %r, %r, %s"
return form % (dict_impl.__getattribute__(self, 'lh'),
dict_impl.__getattribute__(self, 'lt'),
dict_impl.__repr__(self))
class odict(_odict, dict):
def _dict_impl(self):
return dict
if(ver[0]=="3"):
#Ordered atributes tree building for Python 3
def _start_list(self, tag, attrib_in):
fixname = self._fixname
tag = fixname(tag)
attrib=odict()
if attrib_in:
for i in range(0, len(attrib_in), 2):
attrib[fixname(attrib_in[i])] = attrib_in[i+1]
return self.target.start(tag, attrib)
#Replace base function with mine
etree.XMLTreeBuilder._start_list = _start_list
if(ver[0]=="2"):
#Ordered atributes tree building for Python 2.4
def _start(self, tag, attrib_in):
fixname = self._fixname
tag = fixname(tag)
attrib = odict()
for key, value in attrib_in.items():
attrib[fixname(key)] = self._fixtext(value)
return self._target.start(tag, attrib)
def _start_list(self, tag, attrib_in):
fixname = self._fixname
tag = fixname(tag)
attrib = odict()
if attrib_in:
for i in range(0, len(attrib_in), 2):
attrib[fixname(attrib_in[i])] = attrib_in[i+1]
return self._target.start(tag, attrib)
#Replace base function with mine
etree.XMLTreeBuilder._start_list = _start_list
etree.XMLTreeBuilder._strt = _start
if(ver[0]=="3"):
#Ordered serializer....let's try =)
def _serialize_xml(write, elem, qnames, namespaces):
tag = elem.tag
text = elem.text
if tag is etree.Comment:
write("<!--%s-->" % text)
elif tag is etree.ProcessingInstruction:
write("<?%s?>" % text)
else:
tag = qnames[tag]
if tag is None:
if text:
write(etree._escape_cdata(text))
for e in elem:
etree._serialize_xml(write, e, qnames, None)
else:
write("<" + tag)
items = list(elem.items())
if items or namespaces:
if namespaces:
for v, k in sorted(namespaces.items(),
key=lambda x: x[1]): # sort on prefix
if k:
k = ":" + k
write(" xmlns%s=\"%s\"" % (
k,
etree._escape_attrib(v)
))
for k, v in list(items): # no !!!! lexical order
if isinstance(k, etree.QName):
k = k.text
if isinstance(v, etree.QName):
v = qnames[v.text]
else:
v = etree._escape_attrib(v)
write(" %s=\"%s\"" % (qnames[k], v))
if text or len(elem):
write(">")
if text:
write(etree._escape_cdata(text))
for e in elem:
etree._serialize_xml(write, e, qnames, None)
write("</" + tag + ">")
else:
write(" />")
if elem.tail:
write(etree._escape_cdata(elem.tail))
#Redirecting
etree._serialize_xml = _serialize_xml
if(ver[0]=="2.4"):
#For Python 2.4 there is no serializer. Changing to my method.
def _escape_attrib(text, encoding=None, replace=etree.string.replace):
# escape attribute value
try:
if encoding:
try:
text = etree._encode(text, encoding)
except UnicodeError:
return etree._encode_entity(text)
text = replace(text, "&", "&")
text = replace(text, "\"", """)
text = replace(text, "<", "<")
text = replace(text, ">", ">")
return text
except (TypeError, AttributeError):
etree._raise_serialization_error(text)
etree._escape_attrib = _escape_attrib
def _write(self, file, node, encoding, namespaces):
# write XML to file
tag = node.tag
if tag is etree.Comment:
file.write("<!-- %s -->" % etree._escape_cdata(node.text, encoding))
elif tag is etree.ProcessingInstruction:
file.write("<?%s?>" % etree._escape_cdata(node.text, encoding))
else:
items = node.items()
xmlns_items = [] # new namespaces in this scope
try:
if isinstance(tag, etree.QName) or tag[:1] == "{":
tag, xmlns = fixtag(tag, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
etree._raise_serialization_error(tag)
file.write("<" + etree._encode(tag, encoding))
if items or xmlns_items:
# NO !!!!lexical order
for k, v in items:
try:
if isinstance(k, etree.QName) or k[:1] == "{":
k, xmlns = fixtag(k, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
etree._raise_serialization_error(k)
try:
if isinstance(v, etree.QName):
v, xmlns = fixtag(v, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
etree._raise_serialization_error(v)
file.write(" %s=\"%s\"" % (etree._encode(k, encoding),
etree._escape_attrib(v, encoding)))
for k, v in xmlns_items:
file.write(" %s=\"%s\"" % (etree._encode(k, encoding),
etree._escape_attrib(v, encoding)))
if node.text or len(node):
file.write(">")
if node.text:
file.write(etree._escape_cdata(node.text, encoding))
for n in node:
self._write(file, n, encoding, namespaces)
file.write("</" + etree._encode(tag, encoding) + ">")
else:
file.write(" />")
for k, v in xmlns_items:
del namespaces[v]
if node.tail:
file.write(etree._escape_cdata(node.tail, encoding))
#Redirecting...
etree.ElementTree._write=_write
#Location founder function
def loctree(current,root):
if current == root:
return("/"+root.tag)
else:
c_node=current
location=""
if current.get('name'):
location=current.tag+" "+current.get('name')
elif len(list(current.keys()))!=0 and current.get(list(current.keys())[0]):
location=current.tag+" "+list(current.keys())[0]+"="+current.get(list(current.keys())[0])
else:
location=current.tag
build=0
c_element=root
while(c_node!=root):
nlist=list(root)
nlist.insert(0,root)
for node in nlist:
if list(node):
nlist+=list(node)
for child in list(node):
if child==c_node:
c_node=node
if node.get('name'):
location=node.tag+" "+node.get('name')+"/"+location
elif len(list(node.keys()))!=0 and node.get(list(node.keys())[0]):
location=node.tag+" "+list(node.keys())[0]+"="+node.get(list(node.keys())[0])+"/"+location
else:
location=node.tag+"/"+location
if node == root:
location="/"+location
break
return(location)
#MAIN SH FUNCTION
def XMLsh():
tui=0
print("Python version: "+ver[0])
if(ver[0]=="3"):
fname=input('Enter filename to work with. Default is /var/xapi/state.db: ')
elif(ver[0]=="2"):
fname=raw_input('Enter filename to work with. Default is /var/xapi/state.db: ')
else:
fname=0
print("Something with Python version ?")
return 0
if fname=="":
fname="/var/xapi/state.db"
try:
tree=etree.parse(fname)
root=tree.getroot()
except:
print("Something goes wrong. Check file? Exiting..")
return 0
current=root
parent=None
while(tui!=-1):
if(ver[0]=="3"):
tui=input('#:')
elif(ver[0]=="2"):
tui=raw_input('#:')
if tui != '':
tui=list(tui.split())
# Here goes commands
# Command list / ls
if tui[0]=="list" or tui[0]=="ls":
for node in list(current):
if node.get('name'):
print(node.tag+" "+node.get('name'))
elif len(list(node.keys()))!=0 and node.get(list(node.keys())[0]):
print(node.tag+" "+list(node.keys())[0]+"="+node.get(list(node.keys())[0]))
else:
print(node.tag)
for key in list(current.keys()):
print(key+"="+current.get(key))
if current.text != None:
print("Tag text:")
print(current.text)
# Command la (list attributes)
elif tui[0] == "la":
for key in list(current.keys()):
print(key+"="+current.get(key))
if current.text != None:
print("Tag text:")
print(current.text)
# Command exit
elif tui[0] == "exit":
tui=-1
# Command who
elif tui[0] == "who":
print(loctree(current,root))
# Command cd
elif tui[0] == "cd" and len(tui) > 1:
if tui[1] != ".." and tui[1] != "/":
tui.pop(0)
tui=" ".join(tui).strip()
tui=tui.split("/")
iferror=current
for move in tui:
if move=='' and len(tui)>2 and tui[1]!=root.tag:
current=root
if move==root.tag:
current=root
parent=None
for node in list(current):
if node.get('name') and node.get('name')==move:
parent=current
current=node
elif node.get('name') and move==node.tag+" "+node.get('name'):
parent=current
current=node
elif len(list(node.keys()))!=0 and move==node.tag+" "+list(node.keys())[0]+"="+node.get(list(node.keys())[0]):
parent=current
current=node
elif str(node.tag)==move:
parent=current
current=node
if iferror==current:
print("No such instance.")
elif tui[1] == "/":
current=root
parent=None
elif tui[1] == "..":
if parent != None:
current=parent
c_element=root
nlist=list(root)
nlist.insert(0,root)
for node in nlist:
if list(node):
nlist+=list(node)
for child in list(node):
if child==current:
parent=node
break
# Command set
elif tui[0]=="set":
tui.pop(0)
target=tui.pop(0)
tui=" ".join(tui).strip()
if target in list(current.keys()):
current.set(target,tui)
else:
print("No such attribute")
# Command add
elif tui[0]=="add":
tui.pop(0)
target=tui.pop(0)
tui=" ".join(tui).strip()
if target in list(current.keys()):
print("Can't add attribute. Already exist.")
else:
current.set(target,tui)
# Command settext
elif tui[0]=="settext":
tui.pop(0)
tui=" ".join(tui).strip()
current.text=tui
# Command locate, find
elif tui[0]=="locate" or tui[0]=="find":
result=""
mode=tui.pop(0)
tui=" ".join(tui).strip()
c_element=root
nlist=list(root)
nlist.insert(len(nlist)-1,root)
for node in nlist:
if list(node):
nlist+=list(node)
for child in list(node):
if tui==child.tag or (str(child.tag).find(tui)!=-1 and mode=="find"):
print(loctree(child,root))
for key in list(child.keys()):
if str(key)==tui:
print(loctree(child,root)+"/"+tui+"="+str(child.get(tui)))
if str(child.get(key))==tui:
print(loctree(child,root)+"/"+key+"="+child.get(key))
if str(child.text)==tui:
print(loctree(child,root)+"/"+tui)
if mode=="find":
if str(child.text).find(tui)!=-1:
print(loctree(child,root)+"/"+child.text)
for key in list(child.keys()):
if str(key).find(tui)!=-1:
print(loctree(child,root)+"/"+tui+"="+str(child.get(tui)))
if str(child.get(key)).find(tui)!=-1:
print(loctree(child,root)+"/"+key+"="+child.get(key))
# Command xapi
elif tui[0]=="xapi":
tui.pop(0)
tui=" ".join(tui).strip()
if(tui=="start"):
print(call("/etc/init.d/xapi start", shell=True))
elif(tui=="stop"):
print(call("/etc/init.d/xapi stop", shell=True))
elif(tui=="restart"):
print(call("/etc/init.d/xapi restart", shell=True))
else:
print("Usage: xapi <start,stop,restart>")
# Command save
elif tui[0]=="save":
if(ver[0]=="3"):
loc=input("Enter filename to save. Default is /var/xapi/state.db: ")
elif(ver[0]=="2"):
loc=raw_input("Enter filename to save. Default is /var/xapi/state.db: ")
if loc=="":
tree.write("/var/xapi/state.db")
else:
tree.write(loc)
# Command ?/help
elif tui[0]=="?" or tui[0]=="help":
print("XML shell parser commands:")
print("ls/list : List childs in current tag")
print("la : List attributes in current tag")
print("who : Show current location")
print("cd : Change directory to <child_name> or <..> to parent node. For enumerated row select: cd row <row_number>")
print("set : Change attribute value. Usage: set <attribute> <value>")
print("add : Add attribute. Usage: add <attribute> <value>")
print("settext : Change tag text block. Usage: settext <text>")
print("locate : Locate exact key/value. Usage: locate <value>")
print("find : Find all ocurrences of value. Usage: find <value>")
print("xapi : Manage XAPI. Usage: xapi <start/stop/restart>")
print("save : Save file.")
print("?/help : Show this message")
print("! : Debug mode. Direct python command execution. Usage: ! <command>")
# Debug mode for direct python commands input
elif tui[0]=="!":
try:
print(eval(tui[1]))
except:
print ("Something goes wrong..")
# Incorrect command
else:
tui=" ".join(tui).strip()
print("No such command "+tui)
XMLsh()