-
Notifications
You must be signed in to change notification settings - Fork 81
/
common.py
265 lines (227 loc) · 11.6 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
# copyright (c) 2010-2016, Christian Mayer and the CometVisu contributers.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
from docutils import statemachine, nodes
from docutils.parsers.rst import Directive
from helper.schema import *
import gettext
import sys
import re
import os
from settings import config, root_dir
kwargs = {}
if sys.version_info[0] < 3:
kwargs['unicode'] = True
gettext.install('messages', **kwargs)
schema = Schema(os.path.join(root_dir, config.get("DEFAULT", "schema-file")))
class BaseDirective(Directive):
locale = 'en'
type_mapping = {}
def init_type_mapping(self):
self.type_mapping = {
'boolean': "*true* %s *false*" % _('or'),
'string': _('string'),
'decimal': _('decimal'),
'uri': _('uri'),
'addr': _('addr'),
'nonNegativeInteger': _('nonNegativeInteger'),
'dimension': _('dimension')
}
def init_locale(self):
self.locale = self.state.document.settings.env.config.language
t = gettext.translation('messages', localedir=os.path.join(root_dir, config.get("DEFAULT", "locale")), languages=[self.locale])
t.install(**kwargs)
self.init_type_mapping()
class BaseXsdDirective(BaseDirective):
def get_cell_data(self, content):
return 0, 0, self.content_offset, statemachine.StringList(content.splitlines())
def normalize_values(self, values):
if len(values) <= 1:
res = "*%s*" % ("* %s *" % _("or")).join(values)
else:
res = " ".join(["*%s*" % "*, *".join(values[0:-1]), _("or"), "*%s*" % values[-1]])
return res
def normalize_type(self, type):
if type[0:4] == "xsd:":
type = type[4:]
res = self.type_mapping[type] if type in self.type_mapping else type
return res
def get_name(self, name):
name = ":ref:`%s`" % name
cnode = nodes.Element() # anonymous container for parsing
sl = statemachine.StringList([name], source='')
self.state.nested_parse(sl, self.content_offset, cnode)
return nodes.label(name, '', *cnode)
def generate_table(self, element_name, include_name=False, mandatory=False):
table_body = []
attributes = schema.get_widget_attributes(element_name)
if include_name:
rowspan = len(attributes)-1
line = 0
for attr in attributes:
if 'name' in attr.attrib:
name = attr.get('name')
atype, values = schema.get_attribute_type(attr)
description = schema.get_node_documentation(attr, self.locale)
if description is not None:
description = re.sub("\n\s+", " ", description.text).strip()
else:
description = ''
elif 'ref' in attr.attrib:
name = attr.get('ref')
type_def = schema.get_attribute(name)
atype, values = schema.get_attribute_type(type_def)
# check if there is some documentation here
description = schema.get_node_documentation(attr, self.locale)
if description is None:
# check the referenced definition for documentation as fallback
description = schema.get_node_documentation(type_def, self.locale)
if description is not None:
description = re.sub("\n\s+", " ", description.text).strip()
else:
description = ''
#name = ":ref:`%s <%s>`" % (name, name)
if attr.get('use', 'optional') == "required":
name = ":abbr:`%s(%s)`" % (name, _('mandatory'))
atype = self.normalize_type(atype) if len(values) == 0 else self.normalize_values(values)
if include_name:
if line == 0:
if mandatory:
element_name = ":abbr:`%s(%s)`" % (element_name, _('mandatory'))
row = [(rowspan, 0, 0, statemachine.StringList(element_name.splitlines())), self.get_cell_data(name), self.get_cell_data(atype), self.get_cell_data(description)]
else:
row = [ None, self.get_cell_data(name), self.get_cell_data(atype), self.get_cell_data(description)]
else:
row = [self.get_cell_data(name), self.get_cell_data(atype), self.get_cell_data(description)]
table_body.append(row)
line += 1
if len(table_body) == 0:
return None
if include_name:
table_head = [
[self.get_cell_data(_('Element')), (0, 3, 0, statemachine.StringList(_('Attribute').splitlines())), None, None],
[self.get_cell_data(''), self.get_cell_data(_('Name')), self.get_cell_data(_('Content')), self.get_cell_data(_('Description'))]
]
table = ([10, 15, 20, 55], table_head, table_body)
else:
table_head = [
[(0, 3, 0, statemachine.StringList(_('Attribute').splitlines())), None, None],
[self.get_cell_data(_('Name')), self.get_cell_data(_('Content')), self.get_cell_data(_('Description'))]
]
table = ([20, 20, 60], table_head, table_body)
table_node = self.state.build_table(table, self.content_offset)
table_node['classes'] += self.options.get('class', [])
return table_node
def generate_complex_table(self, element_name, include_name=False, mandatory=False,
table_body=None, sub_run=False, parent=None):
""" needs to be fixed """
if table_body is None:
table_body = []
if not element_name == "#text":
attributes = schema.get_widget_attributes(element_name)
elements = schema.get_widget_elements(element_name, locale=self.locale)
if include_name:
rowspan = len(attributes)-1
line = 0
for attr in attributes:
if 'name' in attr.attrib:
name = attr.get('name')
atype, values = schema.get_attribute_type(attr)
description = schema.get_node_documentation(attr, self.locale)
if description is not None:
description = description.text
else:
description = ''
elif 'ref' in attr.attrib:
name = attr.get('ref')
type_def = schema.get_attribute(name)
atype, values = schema.get_attribute_type(type_def)
description = schema.get_node_documentation(type_def, self.locale)
if description is not None:
description = description.text
else:
description = ''
#name = ":ref:`%s <%s>`" % (name, name)
if attr.get('use', 'optional') == "required":
name = ":abbr:`%s (%s)`" % (name, _('mandatory'))
atype = self.normalize_type(atype) if len(values) == 0 else self.normalize_values(values)
if include_name:
if line == 0:
element_title = element_name
if mandatory:
element_title = ":abbr:`%s (%s)`" % (element_title, _('mandatory'))
if parent:
element_title = "%s\n * %s" % (parent, element_title)
row = [(rowspan, 0, 0,
statemachine.StringList(element_title.splitlines())),
self.get_cell_data(name),
self.get_cell_data(atype),
self.get_cell_data(description)
]
else:
row = [ None, self.get_cell_data(name), self.get_cell_data(atype), self.get_cell_data(description)]
else:
row = [self.get_cell_data(name), self.get_cell_data(atype), self.get_cell_data(description)]
table_body.append(row)
line += 1
for sub_element in elements:
if not isinstance(sub_element, tuple):
name = sub_element.get("name")
mandatory = sub_element.get("minOccurs") is not None and int(sub_element.get("minOccurs")) > 0
if parent is not None:
sub_parent = "%s\n * %s" % (parent, element_name)
else:
sub_parent = element_name
#no recursions
if name != element_name:
self.generate_complex_table(name, include_name=include_name,
mandatory=mandatory, table_body=table_body,
sub_run=True, parent=sub_parent)
else:
(sub_element, atype, doc) = sub_element
indent = 2 if parent is not None else 1
element_title = "%s\n%s* %s" % (element_name, " " * indent, sub_element)
if parent:
element_title = "%s\n * %s" % (parent, element_title)
row = [ self.get_cell_data(element_title), self.get_cell_data(""), self.get_cell_data(self.normalize_type(atype)), self.get_cell_data(doc)]
table_body.append(row)
line += 1
else:
# text node
if include_name:
row = [self.get_cell_data(element_name), self.get_cell_data(""), self.get_cell_data(self.normalize_type("string")), self.get_cell_data("")]
else:
row = [self.get_cell_data(element_name), self.get_cell_data(self.normalize_type("string")), self.get_cell_data("")]
table_body.append(row)
if sub_run is False:
if len(table_body) == 0:
return None
if include_name:
table_head = [
[self.get_cell_data(_('Element')), (0, 3, 0, statemachine.StringList(_('Attribute').splitlines())), None, None],
[self.get_cell_data(_('Structure')), self.get_cell_data(_('Name')), self.get_cell_data(_('Content')), self.get_cell_data(_('Description'))]
]
table = ([15, 15, 15, 55], table_head, table_body)
else:
table_head = [
[(0, 3, 0, statemachine.StringList(_('Attribute').splitlines())), None, None],
[self.get_cell_data(_('Name')), self.get_cell_data(_('Content')), self.get_cell_data(_('Description'))]
]
table = ([20, 20, 60], table_head, table_body)
table_node = self.state.build_table(table, self.content_offset)
table_node['classes'] += self.options.get('class', [])
table_node['classes'] += ["schema-table"]
return table_node