1 import xml.dom.minidom
as dom
2 import sys, os, optparse
6 OptionParser is main class to parse options.
9 optparse.OptionParser.__init__(self, usage=
"%prog --help or %prog [options] file", version=
"%prog 0.0.1", conflict_handler=
"resolve")
10 self.add_option(
"--src", action=
"store", type=
"string", dest=
"src", help=
"specify source XML file")
11 self.add_option(
"--min", action=
"store", type=
"int", dest=
"min", help=
"Minimum length to measure")
12 self.add_option(
"--max", action=
"store", type=
"int", dest=
"max", help=
"Maximum length to measure")
13 self.add_option(
"--cid", action=
"store", type=
"int", dest=
"cid", help=
"Apply combination ID")
14 self.add_option(
"--xsd", action=
"store_true", dest=
"xsd", help=
"Create XML Schema fragment")
17 print "Reading histogram file"
19 histos = srcdoc.getElementsByTagName(
"Histogram")
22 for key
in histo.childNodes:
23 if key.nodeType == key.ELEMENT_NODE:
25 value = key.childNodes[0].nodeValue
28 if not elements.has_key(name):
29 elements[name] = {
'type':
'',
'count': 0}
30 elements[name][
'count'] = elements[name][
'count'] + 1
34 if elements[name][
'type'] ==
'':
35 elements[name][
'type'] =
'xs:integer'
39 if elements[name][
'type']
in (
'',
'xs:integer'):
40 elements[name][
'type'] =
'xs:double'
42 elements[name][
'type'] =
'xs:string'
45 if keys[k][
'name'] == name
and keys[k][
'value'] == value:
46 keys[k][
'count'] = keys[k][
'count'] + 1
51 keys[n] = {
'name': name,
'value': value,
'count': 1}
59 name = keys[k][
'name']
61 root = resdoc.createElement(
"xs:complexType")
62 root.setAttribute(
"name",
"HistogramType")
63 resdoc.appendChild(root)
64 seq = resdoc.createElement(
"xs:all")
66 for e
in sorted(elements.keys()):
67 el = resdoc.createElement(
"xs:element")
68 el.setAttribute(
"name", e)
69 el.setAttribute(
"type", elements[e][
'type'])
70 if elements[e][
'count'] < len(histograms):
71 el.setAttribute(
"minOccurs",
'0')
72 el.setAttribute(
"maxOccurs",
'1')
77 print "Declaration to apply:", co
79 print keys[k][
'name'],
'=', keys[k][
'value']
98 for i
in range(vfrom, vto):
99 for j
in range(i, vto):
101 if len(queue) >= min
and len(queue) <= max:
106 print "Computing permutations"
119 for ci
in comb.keys():
123 if not prior.has_key(l):
128 if __name__ ==
"__main__":
131 (opts, args) = optManager.parse_args()
134 if opts[
'src']
in (
'',
None):
135 print "You must specify a valid source xml file"
138 resdoc = dom.Document()
139 srcdoc = dom.parse(opts[
'src'])
152 if opts[
'xsd'] !=
None:
155 print resdoc.toprettyxml()
160 if len(h) > len_max: len_max = len(h)
161 if len(h) < len_min: len_min = len(h)
162 print "Computed len: min = ", len_min,
", max = ", len_max
165 if opts[
'min']
not in (0,
None): min = opts[
'min']
167 if opts[
'max']
not in (0,
None): max = opts[
'max']
168 print "Computing lens from", min,
" to ", max
173 for pi
in sorted(prior.keys()):
174 print pi,
"=", prior[pi]
176 if opts[
'cid'] !=
None: