1 | ### =========================================================================== |
---|
2 | ### |
---|
3 | ### Modifies or add an element in an XML file |
---|
4 | ### |
---|
5 | ### =========================================================================== |
---|
6 | ## |
---|
7 | ## Warning, to install, configure, run, use any of Olivier Marti's |
---|
8 | ## software or to read the associated documentation you'll need at least |
---|
9 | ## one (1) brain in a reasonably working order. Lack of this implement |
---|
10 | ## will void any warranties (either express or implied). |
---|
11 | ## O. Marti assumes no responsability for errors, omissions, |
---|
12 | ## data loss, or any other consequences caused directly or indirectly by |
---|
13 | ## the usage of his software by incorrectly or partially configured |
---|
14 | ## personal. |
---|
15 | ## |
---|
16 | ## SVN information |
---|
17 | __Author__ = "$Author$" |
---|
18 | __Date__ = "$Date$" |
---|
19 | __Revision__ = "$Revision$" |
---|
20 | __Id__ = "$Id$" |
---|
21 | __HeadURL = "$HeadURL$" |
---|
22 | |
---|
23 | # |
---|
24 | # Tested with python/2.7.12 and python/3.6.4 |
---|
25 | # |
---|
26 | import xml.etree.ElementTree |
---|
27 | import getopt, sys |
---|
28 | |
---|
29 | ## |
---|
30 | def usage () : |
---|
31 | texte = """%(prog)s usage : |
---|
32 | python %(prog)s [-d] [-i iodef.xml] [-o iodef_new.xml] -n <node in Xpath syntax> -k <key> -v <value> |
---|
33 | python %(prog)s [-d] [-i iodef.xml] [-o iodef_new.xml] -n <node in Xpath syntax> -t <value> |
---|
34 | -d | --debug : debug |
---|
35 | -i <file> | --input=<file> : input file (default: iodef.xml) |
---|
36 | -o <file> | --output=<file> : output file (default: overwrite input file) |
---|
37 | -n <node> | --node=<node> : node in Xpath syntax |
---|
38 | -f <field> | --field=<field> : xml field to update |
---|
39 | -v <value> | --value=<value> : new value for xml field |
---|
40 | -t <text> | --text=<text> : will replace the 'text' part of the Xpath by <text> |
---|
41 | examples : |
---|
42 | python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_T |
---|
43 | python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]' -t ${dstDomainType} |
---|
44 | """ |
---|
45 | #print ( texte % ( sys.argv[0], sys.argv[0], sys.argv[0], sys.argv[0] ) ) |
---|
46 | #print ( texte % ( 6*[sys.argv[0]] )) |
---|
47 | print ( texte % { 'prog':sys.argv[0] } ) |
---|
48 | |
---|
49 | # Check version of Python |
---|
50 | Version = sys.version_info |
---|
51 | if Version < (2,7,0) : |
---|
52 | sys.stderr.write ( "You need python 2.7 or later to run this script\n" ) |
---|
53 | sys.stderr.write ( "Present version is: " + str(Version[0]) + "." + str(Version[1]) + "." + str(Version[2]) + "\n" ) |
---|
54 | sys.exit (1) |
---|
55 | |
---|
56 | ## Default input parameters |
---|
57 | FileIn = 'iodef.xml' |
---|
58 | FileOut = None |
---|
59 | Node = None |
---|
60 | Key = None |
---|
61 | Text = None |
---|
62 | Value = None |
---|
63 | Debug = False |
---|
64 | |
---|
65 | ## Command line options |
---|
66 | try: |
---|
67 | myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:o:n:k:v:t:dh', [ 'input=', 'output=', 'node=', 'key=', 'value=', 'text=', 'debug=', '--help' ] ) |
---|
68 | except getopt.GetoptError as cmdle : |
---|
69 | print ( "Command line error : "+cmdle ) |
---|
70 | usage () |
---|
71 | sys.exit(1) |
---|
72 | |
---|
73 | for myopt, myval in myopts : |
---|
74 | if myopt in [ '-h', '--help' ] : usage () ; sys.exit (0) ; |
---|
75 | if myopt in [ '-i', '--input' ] : FileIn = myval |
---|
76 | if myopt in [ '-o', '--output' ] : FileOut = myval |
---|
77 | if myopt in [ '-n', '--node' ] : Node = myval |
---|
78 | if myopt in [ '-k', '--key' ] : Key = myval |
---|
79 | if myopt in [ '-t', '--text' ] : Text = myval |
---|
80 | if myopt in [ '-v', '--value' ] : Value = myval |
---|
81 | if myopt in [ '-d', '--debug' ] : Debug = True |
---|
82 | |
---|
83 | ## Some coherency checking of command line parameters |
---|
84 | ErrorCount = 0 |
---|
85 | |
---|
86 | if FileIn == None : |
---|
87 | print ( "Error : please specify input file by -i <file>" ) |
---|
88 | ErrorCount += 1 |
---|
89 | |
---|
90 | if Node == None : |
---|
91 | print ( "Error : please specify -n <node>" ) |
---|
92 | ErrorCount += 1 |
---|
93 | |
---|
94 | if Key == None and Text == None : |
---|
95 | print ( "Error : please specify either -t <text> or -k <key> -v <value>" ) |
---|
96 | ErrorCount += 1 |
---|
97 | |
---|
98 | if Key != None and Text != None : |
---|
99 | print ( "Error : please specify only one option between -t "+Text+" and -k "+Key ) |
---|
100 | ErrorCount += 1 |
---|
101 | |
---|
102 | if Key != None and Value == None : |
---|
103 | print ( "Error : please specify -v <value> when -k "+Key+" is given") |
---|
104 | ErrorCount += 1 |
---|
105 | |
---|
106 | if ErrorCount > 0 : |
---|
107 | usage () |
---|
108 | sys.exit (1) |
---|
109 | |
---|
110 | if FileOut == None : FileOut = FileIn |
---|
111 | |
---|
112 | ## Remove white spaces at beginning and end of line |
---|
113 | Node = Node.rstrip().lstrip() |
---|
114 | |
---|
115 | ## Get XML tree from input file |
---|
116 | iodef = xml.etree.ElementTree.parse ( FileIn ) |
---|
117 | |
---|
118 | ## Find node |
---|
119 | nodeList = iodef.findall ( Node ) |
---|
120 | |
---|
121 | ## Check that one and only one node is found |
---|
122 | if len(nodeList) == 0 : |
---|
123 | print ( "Error : node not found" ) |
---|
124 | print ( "Node : "+Node ) |
---|
125 | sys.exit (1) |
---|
126 | |
---|
127 | if len(nodeList) > 1 : |
---|
128 | print ( "Error : "+len(nodeList)+" occurences of node found" ) |
---|
129 | print ( "Node : "+Node ) |
---|
130 | sys.exit (1) |
---|
131 | |
---|
132 | ## Update element |
---|
133 | elem = nodeList[0] |
---|
134 | |
---|
135 | if Debug : |
---|
136 | print ( 'Node : '+Node ) |
---|
137 | print ( 'Key : '+Key ) |
---|
138 | print ( 'Value : '+Value ) |
---|
139 | |
---|
140 | if Text != None : |
---|
141 | if Debug : |
---|
142 | print ( 'Attributes of node: '+str(elem.attrib) ) |
---|
143 | print ( 'Text : '+str(elem.text) ) |
---|
144 | elem.text = Text |
---|
145 | |
---|
146 | if Key != None : |
---|
147 | # To do : check that Key exist (it is added if not : do we want that ?) |
---|
148 | if Debug : |
---|
149 | print ( 'Attributes of node: '+str(elem.attrib) ) |
---|
150 | elem.attrib.update ( { Key:Value } ) |
---|
151 | |
---|
152 | |
---|
153 | ## Writes XML tree to file |
---|
154 | iodef.write ( FileOut ) |
---|
155 | |
---|
156 | ## This is the end |
---|
157 | sys.exit (0) |
---|
158 | |
---|
159 | ### =========================================================================== |
---|
160 | ### |
---|
161 | ### That's all folk's !!! |
---|
162 | ### |
---|
163 | ### =========================================================================== |
---|
164 | |
---|