-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmltool.py
executable file
·78 lines (61 loc) · 2.14 KB
/
xmltool.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
#!/usr/bin/env python
import os
import pdb
import re
import sys
import toolframe
import xml.etree.ElementTree as ET
from optparse import *
# ---------------------------------------------------------------------------
def xml_attredit(argv):
"""attredit - edit attribute values in XML files
usage: xmltool attredit [-d] -a attrib -e ... <filename> <filename> ...
-a which attribute to edit
-d run under python debugger
-e edits to make
Result is written to stdout.
"""
p = OptionParser()
p.add_option('-d', '--debug',
action='store_true', default=False, dest='debug',
help='run the debugger')
p.add_option('-a', '--attr',
action='store', default=None, dest='attrib',
help='attribute to edit')
p.add_option('-e', '--edit',
action='append', default=None, dest='edits',
help='edit(s) to make on the input')
p.add_option('-o', '--output',
action='store', default=None, dest='output',
help='write to file OUTPUT')
(o, a) = p.parse_args(argv)
if o.debug: pdb.set_trace()
ehash = {}
for e in o.edits:
q = e.split(e[1])
ehash[q[1]] = q[2]
for filename in a:
tagl = []
X = ET.parse(filename)
for el in X.iter():
if o.attrib in el.attrib:
# -- do the edit
# tagl.append(el.tag)
# cp = os.path.commonprefix(tagl)
# print cp
# for el in X.iter('%sservice' % cp):
# print '---------------------------------'
# print el.attrib
# attr = el.attrib
# if 'serviceType' in attr.keys() and attr['serviceType'] == 'SRM':
# editable = el
# print editable
# print editable.attrib
base = editable.attrib['base']
# print base
newbase = base.replace('esg2-sdnl1.ccs.ornl.gov', 'esg.ccs.ornl.gov')
editable.attrib['base'] = newbase
X.write(sys.stdout)
pass
# ---------------------------------------------------------------------------
toolframe.tf_launch("xml")