test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
bookConverter.py
Go to the documentation of this file.
1 import xml.dom.minidom as dom
2 import sys, os, optparse
3 
4 class OptionParser(optparse.OptionParser):
5  """
6  OptionParser is main class to parse options.
7  """
8  def __init__(self):
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")
15 
16 def read_data():
17  print "Reading histogram file"
18  n = 0
19  histos = srcdoc.getElementsByTagName("Histogram")
20  for histo in histos:
21  h = []
22  for key in histo.childNodes:
23  if key.nodeType == key.ELEMENT_NODE:
24  name = key.localName
25  value = key.childNodes[0].nodeValue
26  found = 0
27 
28  if not elements.has_key(name):
29  elements[name] = {'type': '', 'count': 0}
30  elements[name]['count'] = elements[name]['count'] + 1
31 
32  try:
33  i = int(value)
34  if elements[name]['type'] == '':
35  elements[name]['type'] = 'xs:integer'
36  except ValueError:
37  try:
38  i = float(value)
39  if elements[name]['type'] in ('', 'xs:integer'):
40  elements[name]['type'] = 'xs:double'
41  except ValueError:
42  elements[name]['type'] = 'xs:string'
43 
44  for k in keys.keys():
45  if keys[k]['name'] == name and keys[k]['value'] == value:
46  keys[k]['count'] = keys[k]['count'] + 1
47  h.append(k)
48  found = 1
49  break
50  if found == 0:
51  keys[n] = {'name': name, 'value': value, 'count': 1}
52  h.append(n)
53  n += 1
54  h.sort()
55  histograms.append(h)
56 
57 def create_xsd():
58  for k in keys.keys():
59  name = keys[k]['name']
60 
61  root = resdoc.createElement("xs:complexType")
62  root.setAttribute("name", "HistogramType")
63  resdoc.appendChild(root)
64  seq = resdoc.createElement("xs:all")
65  root.appendChild(seq)
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')
73  seq.appendChild(el)
74 
76  co = comb[cid]
77  print "Declaration to apply:", co
78  for k in comb[cid]:
79  print keys[k]['name'], '=', keys[k]['value']
80 
81 def cexists(s, c):
82  d = len(c)
83  for v1 in s:
84  for v2 in c:
85  if v1 == v2:
86  d = d - 1
87  return (d == 0)
88 
89 def ccopy(a):
90  r = []
91  for v in a:
92  r.append(v)
93  return r
94 
95 def kpermutation(vfrom, vto, min, max):
96  vto = vto + 1
97  queue = []
98  for i in range(vfrom, vto):
99  for j in range(i, vto):
100  queue.append(j)
101  if len(queue) >= min and len(queue) <= max:
102  yield queue
103  queue = []
104 
105 def compute(min, max):
106  print "Computing permutations"
107  for v in kpermutation(0, len(keys), min, max):
108  ci = -1
109  for h in histograms:
110  if cexists(h, v):
111  if ci == -1:
112  ci = len(comb)
113  comb[ci] = ccopy(v)
114  results[ci] = [h]
115  else:
116  results[ci].append(h)
117 
119  for ci in comb.keys():
120  l = len(results[ci])
121  if l == 1:
122  continue
123  if not prior.has_key(l):
124  prior[l] = [ci]
125  else:
126  prior[l].append(ci)
127 
128 if __name__ == "__main__":
129 
130  optManager = OptionParser()
131  (opts, args) = optManager.parse_args()
132  opts = opts.__dict__
133 
134  if opts['src'] in ('', None):
135  print "You must specify a valid source xml file"
136  sys.exit(0)
137 
138  resdoc = dom.Document()
139  srcdoc = dom.parse(opts['src'])
140 
141  histograms = []
142  keys = {}
143  results = {}
144  comb = {}
145  prior = {}
146  elements = {}
147  len_min = 1000000
148  len_max = 0
149 
150  read_data()
151 
152  if opts['xsd'] != None:
153 
154  create_xsd()
155  print resdoc.toprettyxml()
156 
157  else:
158 
159  for h in histograms:
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
163 
164  min = 2
165  if opts['min'] not in (0, None): min = opts['min']
166  max = len_max
167  if opts['max'] not in (0, None): max = opts['max']
168  print "Computing lens from", min, " to ", max
169 
170  compute(min, max)
171  priorities()
172 
173  for pi in sorted(prior.keys()):
174  print pi, "=", prior[pi]
175 
176  if opts['cid'] != None:
177  create_declaration(opts['cid'])
def create_declaration