1
2
3
4
5
6 """
7 1. Synopsis:
8
9 Recusively process the include elements in an XML Schema file.
10 Produce a single file that contains all included content.
11 Input is read either from a file or from stdin.
12 Output is written either to a file or to stdout.
13
14 2. Usage:
15
16 python process_includes.py [options] [ infile [ outfile ] ]
17
18 3. Options:
19 -h, --help Display this help message.
20 -f, --force Force. If outfile exists, overwrite without asking.
21
22 4. Examples:
23
24 python process_includes.py infile.xsd
25 python process_includes.py infile.xsd outfile.xsd
26 python process_includes.py infile.xsd > outfile.xsd
27 cat infile.py | python process_includes.py > outfile.xsd
28
29 """
30
31
32
33
34 import sys
35 import os
36 import getopt
37 import re
38 import types
39
40 WhichElementTree = ''
41 try:
42 from xml.etree import ElementTree as etree
43 WhichElementTree = 'xml.etree'
44 except ImportError, e:
45 try:
46 from lxml import etree
47 WhichElementTree = 'lxml'
48 except ImportError, e:
49 try:
50 from elementtree import ElementTree as etree
51 WhichElementTree = 'elementtree'
52 except ImportError, e:
53 print '***'
54 print '*** Error: Must install >=Python-2.5 or ElementTree or lxml.'
55 print '***'
56 raise
57
58
59
60
61
62
63 FORCE = False
64 NAMESPACE_PAT = re.compile(r'\{.*\}')
65
66
67
68
69
70
71
72
73
74
75
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 if inpath:
95 doc = etree.parse(inpath)
96 root = doc.getroot()
97 process_include_tree(root)
98 else:
99 s1 = sys.stdin.read()
100 root = etree.fromstring(s1)
101 process_include_tree(root)
102 doc = etree.ElementTree(root)
103 if outpath:
104 outfile = make_file(outpath)
105 if outfile:
106 doc.write("tmp_" + outfile.name)
107 fTemp = open("tmp_" + outfile.name)
108 strContent = fTemp.read()
109 strContent = str.replace(strContent, "ns0", "xs", -1)
110 outfile.write(strContent)
111 outfile.close()
112 else:
113 doc.write(sys.stdout)
114
115
117 idx = 0
118 children = root.getchildren()
119 while idx < len(children):
120 child = children[idx]
121 tag = child.tag
122
123 if type(tag) == types.StringType:
124 tag = NAMESPACE_PAT.sub("", tag)
125 else:
126 tag = None
127 if tag == 'include' and 'schemaLocation' in child.attrib:
128 root.remove(child)
129 path = child.attrib['schemaLocation']
130 if os.path.exists(path):
131 doc = etree.parse(path)
132 node = doc.getroot()
133 process_include_tree(node)
134 children1 = node.getchildren()
135 for child1 in children1:
136 root.insert(idx, child1)
137 idx += 1
138 else:
139 process_include_tree(child)
140 idx += 1
141 children = root.getchildren()
142
143
145 global FORCE
146 outFile = None
147
148
149
150
151
152
153
154 outFile = open(outFileName, 'w')
155
156 return outFile
157
158
159 USAGE_TEXT = __doc__
160
164
165
167 global FORCE
168 args = sys.argv[1:]
169 try:
170 opts, args = getopt.getopt(args, 'hf', ['help', 'force', ])
171 except Exception:
172 usage()
173 name = 'nobody'
174 for opt, val in opts:
175 if opt in ('-h', '--help'):
176 usage()
177 elif opt in ('-f', '--force'):
178 FORCE = True
179 if len(args) == 2:
180 inpath = args[0]
181 outpath = args[1]
182 elif len(args) == 1:
183 inpath = args[0]
184 outpath = None
185 elif len(args) == 0:
186 inpath = None
187 outpath = None
188 else:
189 usage()
190 process_include(inpath, outpath)
191
192
193 if __name__ == '__main__':
194
195 main()
196