~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to plugins/wb.admin/gen-opt/genopt.py

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import sys
 
3
import xml.dom.minidom
 
4
import re
 
5
 
 
6
# table_type is a synonym to storage_engine
 
7
OPTIONS_TO_IGNORE = ["default-table-type", "new"]
 
8
 
 
9
sdoc = xml.dom.minidom.parse("structure.xml")
 
10
 
 
11
tabs = {}
 
12
tabs_list = []
 
13
variables = {}
 
14
 
 
15
#===============================================================================
 
16
# Create structure first
 
17
def parse_configuration(sec):
 
18
  for node in sec.childNodes:
 
19
    if xml.dom.Node.ELEMENT_NODE == node.nodeType:
 
20
      if node.tagName == "tab":
 
21
        tab = (node.getAttribute("caption"), node.getAttribute("description"), [], {})
 
22
        groups = tab[2]
 
23
        groups_map = tab[3]
 
24
        tabs[node.getAttribute("caption")] = tab
 
25
        tabs_list.append(tab)
 
26
        for group in node.childNodes:
 
27
          if xml.dom.Node.ELEMENT_NODE == group.nodeType:
 
28
            if group.tagName == "group":
 
29
              grp = []
 
30
              groups.append((group.getAttribute('caption'), grp))
 
31
              groups_map[group.getAttribute('caption')] = grp
 
32
 
 
33
for sec in sdoc.documentElement.getElementsByTagName('section'):
 
34
  if sec.getAttribute("class") == "mycnf":
 
35
    parse_configuration(sec)
 
36
 
 
37
misc_grp = []
 
38
misc_tab = ('Misc', 'Uncategorized', [('Misc', misc_grp)], {'Misc':misc_grp})
 
39
tabs['Misc'] = misc_tab
 
40
tabs_list.append(misc_tab)
 
41
 
 
42
#-------------------------------------------------------------------------------
 
43
def get_group(collection, group):
 
44
  coll = None
 
45
  grp = None
 
46
  try:
 
47
    coll = tabs[collection]
 
48
    grp = coll[3][group]
 
49
  except:
 
50
    print collection, group
 
51
    raise
 
52
  return grp
 
53
 
 
54
#===============================================================================
 
55
# read controls/options
 
56
doc = xml.dom.minidom.parse('mysqld_new.xml')
 
57
 
 
58
#-------------------------------------------------------------------------------
 
59
def parse_ui_section(name, sec):
 
60
  uis = {}
 
61
 
 
62
  if sec.getAttribute('class') == "mycnf":
 
63
    uis['class'] = "mycnf"
 
64
    uis['collection'] = sec.getAttribute('collection')
 
65
    uis['group'] = sec.getAttribute('group')
 
66
    for node in sec.childNodes:
 
67
      if xml.dom.Node.ELEMENT_NODE == node.nodeType:
 
68
        if node.tagName == "caption":
 
69
          uis['caption'] = node.getAttribute('value')
 
70
        elif node.tagName == "description":
 
71
          uis['description'] = node.getAttribute('value')
 
72
        elif node.tagName == "type":
 
73
          utype = node.getAttribute('value')
 
74
          unitcontrol = node.getAttribute('unitcontrol')
 
75
          if unitcontrol != "":
 
76
            uis['unitcontrol'] = unitcontrol
 
77
          udefault = node.getAttribute('default')
 
78
          uis['default'] = udefault
 
79
          uis['type'] = utype
 
80
 
 
81
          for n in node.getElementsByTagName('value'):
 
82
            if n.nodeType == xml.dom.Node.ELEMENT_NODE:
 
83
              for i in range(len(n.attributes)):
 
84
                attr = n.attributes.item(i)
 
85
                uis[attr.name] = attr.value
 
86
 
 
87
          if utype == 'dropdownbox':
 
88
            style = node.getAttribute('style')
 
89
            if style == 'edit':
 
90
              uis['type'] = 'dropdownboxentry'
 
91
            choices = []
 
92
            pysrc = node.getElementsByTagName('pysource')
 
93
            if len(pysrc) > 0:
 
94
              srcid = pysrc[0].getAttribute('sourceid')
 
95
              uis['choices'] = srcid
 
96
            else:
 
97
              for n in node.childNodes:
 
98
                if xml.dom.Node.ELEMENT_NODE == n.nodeType:
 
99
                  if n.tagName == 'choice':
 
100
                    choices.append((n.getAttribute('caption'), n.getAttribute('value')))
 
101
              uis['choices'] = choices
 
102
          #else:
 
103
          #  sys.stderr.write("Fix script: unhandled overriden type " + utype + "\n")
 
104
          #  uis = None
 
105
 
 
106
  return uis
 
107
 
 
108
#-------------------------------------------------------------------------------
 
109
def parse_ui(name, ui_node):
 
110
  ui = {}
 
111
  for node in ui_node.childNodes:
 
112
    if xml.dom.Node.ELEMENT_NODE == node.nodeType:
 
113
      if node.tagName == "section":
 
114
        uis = parse_ui_section(name, node)
 
115
        ui[uis['class']] = uis
 
116
 
 
117
  return ui
 
118
 
 
119
#-------------------------------------------------------------------------------
 
120
def add_variable(var, oclass, section, scope, versions, description):
 
121
  #if scope == 'session':
 
122
  #  return
 
123
 
 
124
  # Prepare description
 
125
  description = description.replace('\n', '')
 
126
  while True:
 
127
    new_description = description.replace('  ', ' ')
 
128
    if new_description == description:
 
129
      break
 
130
    description = new_description
 
131
 
 
132
  description = description.replace('\'', '\\\'')
 
133
  res = re.search('<shortdescription>(.+)</shortdescription>', description)
 
134
  if res is not None:
 
135
    description = res.groups()[0].strip(' ')
 
136
 
 
137
  # Get class
 
138
  if oclass not in variables:
 
139
    variables[oclass] = {}
 
140
  cls = variables[oclass]
 
141
 
 
142
  if section not in cls:
 
143
    cls[section] = []
 
144
 
 
145
  sec = cls[section]
 
146
 
 
147
  values = vars()
 
148
  names  = ('var', 'versions', 'description')
 
149
  lines = []
 
150
  for name in names:
 
151
    value = values[name]
 
152
    if value is not None:
 
153
      if type(value) is not tuple:
 
154
        lines.append(name + "='" + str(value) + "'")
 
155
      else:
 
156
        lines.append(name + "=" + str(value))
 
157
    else:
 
158
      lines.append(name + "=None")
 
159
  sec.append("Option(" + ' ,'.join(lines) + ")")
 
160
 
 
161
#-------------------------------------------------------------------------------
 
162
def parse_versions(versions_node):
 
163
  vers = []
 
164
  for node in versions_node.childNodes:
 
165
    if xml.dom.Node.ELEMENT_NODE == node.nodeType:
 
166
      if node.tagName == "manual" or node.tagName == 'introduced':
 
167
        version_str = node.getAttribute('version')
 
168
        version = None
 
169
        try:
 
170
          tokens = re.match("([0-9]+)\.([0-9]+)\.([0-9]+)|([0-9]+)\.([0-9]+)", version_str).groups()
 
171
          if tokens[0] is not None:
 
172
            version = (int(tokens[0]), int(tokens[1]), int(tokens[2]))
 
173
          else:
 
174
            version = (int(tokens[3]), int(tokens[4]))
 
175
          vers.append(version)
 
176
        except ValueError, e:
 
177
          print "ERROR! incorrect version attribute value '" + version_str + "', ", type(version_str)
 
178
 
 
179
  vers.sort()
 
180
  vers.append((0,0))
 
181
  res = []
 
182
  skip = False
 
183
  for i in range(0, len(vers)-1):
 
184
 
 
185
    if vers[i][0:2] == vers[i+1][0:2]:
 
186
      res.append(max(vers[i], vers[i+1]))
 
187
      skip = True
 
188
    else:
 
189
      if not skip:
 
190
        res.append(vers[i])
 
191
      else:
 
192
        skip = False
 
193
 
 
194
  return tuple(res)
 
195
 
 
196
 
 
197
#-------------------------------------------------------------------------------
 
198
def parse_opt_values(name, node):
 
199
  opt={}
 
200
 
 
201
  for i in range(len(node.attributes)):
 
202
    attr = node.attributes.item(i)
 
203
    aname = attr.name
 
204
    if aname == 'vartype':
 
205
      aname = 'type'
 
206
    avalue = attr.value
 
207
    if avalue == 'enumeration':
 
208
      avalue = 'dropdownbox'
 
209
    opt[aname] = avalue
 
210
 
 
211
 
 
212
  optype = None
 
213
  if 'type' in opt:
 
214
    optype = opt['type']
 
215
 
 
216
  if optype == 'numeric' or optype == 'string' or optype == 'boolean' or optype == 'filename':
 
217
    for n in node.childNodes:
 
218
      if xml.dom.Node.ELEMENT_NODE == n.nodeType:
 
219
        for i in range(len(n.attributes)):
 
220
          attr = n.attributes.item(i)
 
221
          opt[attr.name] = attr.value
 
222
  elif optype == 'dropdownbox':
 
223
    choices = []
 
224
    for n in node.childNodes:
 
225
      if xml.dom.Node.ELEMENT_NODE == n.nodeType:
 
226
        for i in range(len(n.attributes)):
 
227
          attr = n.attributes.item(i)
 
228
          if attr.name == 'value':
 
229
            choices.append((attr.value, attr.value))
 
230
          elif attr.name == 'default':
 
231
            opt['default'] = attr.value
 
232
    opt['choices'] = choices
 
233
  else:
 
234
    sys.stderr.write("Unhandled " + optype + " in " + name + "\n")
 
235
    opt = None
 
236
 
 
237
  return opt
 
238
 
 
239
 
 
240
#-------------------------------------------------------------------------------
 
241
for option in doc.documentElement.getElementsByTagName('mysqloption'):
 
242
  name = ""
 
243
  optype = None
 
244
  ui = None
 
245
  versions = None
 
246
  variable_name = None
 
247
  varcls = None
 
248
  varscope = None
 
249
  shortdesc = ""
 
250
 
 
251
  optid = option.getAttribute('id')
 
252
  optsection = option.getAttribute('section')
 
253
 
 
254
  for node in option.getElementsByTagName('shortdescription'):
 
255
    shortdesc += node.firstChild.data.encode('ascii')
 
256
 
 
257
  is_opt = False
 
258
  is_var = False
 
259
  for node in option.getElementsByTagName('optype'):
 
260
    if node.getAttribute('class') == 'mycnf':
 
261
      name = node.getAttribute('format') # Try to pick name from optype tag's attribute
 
262
      split_name = name.split("=")
 
263
      if len(split_name) > 0:
 
264
        name = split_name[0].strip(" \r\t\n")
 
265
      is_opt = True
 
266
 
 
267
  for node in option.getElementsByTagName('vartype'):
 
268
    varcls = node.getAttribute('class')
 
269
    varscope = node.getAttribute('scope')
 
270
    nm = node.getAttribute('format') # Try to pick name from optype tag's attribute
 
271
    nm = nm.strip(' \r\n\t')
 
272
    if nm != '' or nm is not None:
 
273
      variable_name = nm
 
274
    is_var = True
 
275
 
 
276
  if name == "":
 
277
    name = optid
 
278
 
 
279
  if variable_name is None or variable_name == '':
 
280
    variable_name = optid
 
281
 
 
282
  if name in OPTIONS_TO_IGNORE:
 
283
    continue
 
284
 
 
285
  for node in option.childNodes:
 
286
    if xml.dom.Node.ELEMENT_NODE == node.nodeType:
 
287
      if node.tagName == "name":
 
288
        nm = node.firstChild.data.encode('ascii')
 
289
        if name == "":
 
290
          name = nm
 
291
        if variable_name == "":
 
292
          variable_name = nm
 
293
      elif node.tagName == "values":
 
294
        if optype is None:
 
295
          optype = parse_opt_values(name, node)
 
296
      elif node.tagName == "versions":
 
297
        versions = parse_versions(node)
 
298
      elif node.tagName == "ui":
 
299
        ui = parse_ui(name, node)
 
300
 
 
301
  if is_var:
 
302
    add_variable(variable_name, varcls, optsection, varscope, versions, shortdesc)
 
303
 
 
304
#  if is_opt and optype is None and ui is None:
 
305
#    print name
 
306
  if is_opt:
 
307
    if optype is None:
 
308
      optype = {}
 
309
 
 
310
    if 'type' not in optype:
 
311
      optype['type'] = 'string'
 
312
 
 
313
    optype['name'] = name
 
314
    optype['versions'] = versions
 
315
 
 
316
    if ui is not None:
 
317
      uiopt = ui['mycnf']
 
318
      for k,v in uiopt.iteritems():
 
319
        if 'collection' != k and 'class' != k and 'group' != k and 'platform' != k:
 
320
          optype[k] = v
 
321
 
 
322
      grp = get_group(uiopt['collection'], uiopt['group'])
 
323
      grp.append(optype)
 
324
    else:
 
325
      optype['caption'] = name
 
326
      optype['description'] = " "
 
327
 
 
328
      grp = get_group('Misc', 'Misc')
 
329
      grp.append(optype)
 
330
 
 
331
 
 
332
#===============================================================================
 
333
ind = "  "
 
334
tab_text = []
 
335
 
 
336
f = open('opts.py', 'w')
 
337
 
 
338
def build_group(name, group, ind):
 
339
  grp = "{'caption' : \"" + name + "\",\n"
 
340
  grp += ind + "'controls':\n" + ind + "  (\n"
 
341
  ctrls = []
 
342
  single = len(group) == 1
 
343
  for optype in group:
 
344
    ctrl = ind + "    {\n"
 
345
    lines = []
 
346
 
 
347
    for k,v in optype.iteritems():
 
348
      if k != 'platform':
 
349
        if type(v) is list:
 
350
          if optype['type'] == 'dropdownbox' or optype['type'] == 'dropdownboxentry':
 
351
            line = ind + "      'items' : {"
 
352
            items = []
 
353
            for caption, value in v:
 
354
              items.append("\"" + caption + "\":\"" + value + "\"")
 
355
            line += ", ".join(items) + "}"
 
356
            lines.append(line)
 
357
        else:
 
358
          if type(v) is not tuple:
 
359
            lines.append(ind + "      '" + k + "' : \"" + str(v) + "\"")
 
360
          else:
 
361
            lines.append(ind + "      '" + k + "' : " + str(v))
 
362
 
 
363
    ctrl += ",\n".join(lines)
 
364
    ctrl += "\n"
 
365
    ctrl += ind + "    }"
 
366
    ctrls.append(ctrl)
 
367
  grp += (",\n").join(ctrls)
 
368
  grp += "\n"
 
369
  if single:
 
370
    grp += ind + "   ,\n"
 
371
  grp += ind + "  ) #End of controls list in group " + name + "\n"
 
372
  grp += ind + "} #End of group " + name + "\n"
 
373
  return grp
 
374
 
 
375
f.write("opts_list = {\n")
 
376
 
 
377
for (pos, (tab_name,tab_desc, groups, groups_map)) in enumerate(tabs_list):
 
378
  text = ind + "'" + tab_name + "' : { '"
 
379
  indent = " "*len(text)
 
380
  text += "description' : \"" + tab_desc + "\",\n"
 
381
  text += indent + "  'position' : " + str(pos+1) + ",\n"
 
382
  group_text_list = []
 
383
  single_group = len(groups) == 1
 
384
  for group in groups:
 
385
    group_text_list.append(build_group(group[0], group[1], indent + "               "))
 
386
  text += indent + "  'groups':(  "
 
387
  text += (indent + "             ,").join(group_text_list)
 
388
  if single_group:
 
389
    text += indent + "             ,"
 
390
  text += indent + "           ) # end of groups in tab " + tab_name + "\n"
 
391
  text += indent + "} # end of tab " + tab_name +"\n"
 
392
  tab_text.append(text)
 
393
 
 
394
f.write((ind + ",").join(tab_text) + "\n")
 
395
f.write("} #End of opts_list\n")
 
396
f.close()
 
397
 
 
398
# Write out variables
 
399
f = open('wb_admin_variable_list.py', 'w')
 
400
 
 
401
def generate_Option_class(fields):
 
402
  cls = 'class Option:\n'
 
403
  cls += '  def __init__(self, ' + ', '.join(fields) + "):\n"
 
404
  for field in fields:
 
405
    cls += '    self.' + field + " = " + field + "\n"
 
406
  cls += '\n'
 
407
  cls += '  def __repr__(self):\n'
 
408
  cls += '    s = []\n'
 
409
  cls += '    for attr in dir(self):\n'
 
410
  cls += '      if attr[0] == \'_\':\n'
 
411
  cls += '        continue\n'
 
412
  cls += '      else:\n'
 
413
  cls += '        s.append("\'" + attr + "\':" + str(getattr(self, attr)))\n'
 
414
  cls += '    return \'(\' + (\',\'.join(s)) + \')\'\n'
 
415
  cls += '\n\n\n'
 
416
  return cls
 
417
 
 
418
f.write(generate_Option_class(('var', 'versions', 'description')))
 
419
 
 
420
f.write('vars_list = {\n')
 
421
classes = []
 
422
for cn, cv in variables.iteritems():
 
423
  cls = "'" + cn + "' : {\n"
 
424
  sections = []
 
425
  for sn, sv in cv.iteritems():
 
426
    if sn is None or sn == '':
 
427
      sn = "General"
 
428
    sn = sn[0].upper() + sn[1:]
 
429
    section =  "'" + sn + "' : (\n"
 
430
    sv.sort()
 
431
    vs = []
 
432
    for v in sv:
 
433
      vs.append(v + "\n")
 
434
    section += '        '
 
435
    section += '       ,'.join(vs)
 
436
    if len(vs) == 1:
 
437
      section += "        ,"
 
438
    section += "     ) # End of section " + sn + "\n"
 
439
    sections.append(section)
 
440
  cls += "     " + ("     ,".join(sections))
 
441
  cls += "   } # End of class " + cn + "\n"
 
442
  classes.append(cls)
 
443
f.write(" ,".join(classes))
 
444
f.write("\n")
 
445
f.write('} # End of \n')