forked from smooth80/defold
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript_doc.py
282 lines (245 loc) · 8.87 KB
/
script_doc.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
import re
import logging
import sys
import StringIO
from optparse import OptionParser
from markdown import Markdown
from markdown import Extension
from markdown.util import etree
from markdown.inlinepatterns import Pattern
import script_doc_ddf_pb2
#
# This extension allows the use of [ref:go.animate] or [ref:animate] reference tags in source
#
class RefExtensionException(Exception):
pass
class RefExtension(Extension):
def extendMarkdown(self, md, md_globals):
pattern = r'\[(?P<prefix>ref:)(?P<type>.+?)\]'
tp = RefPattern(pattern)
tp.md = md
tp.ext = self
# Add inline pattern before "reference" pattern
md.inlinePatterns.add("ref", tp, "<reference")
class RefPattern(Pattern):
def getCompiledRegExp(self):
return re.compile("^(.*?)%s(.*)$" % self.pattern, re.DOTALL | re.UNICODE | re.IGNORECASE)
def handleMatch(self, m):
ref = m.group(3)
s = ref.split('.')
if len(s) == 2:
refurl = ('/ref/%s#%s') % (s[0], ref)
else:
refurl = ('#%s') % (s[0])
el = etree.Element('a')
el.text = ref
el.set('href', refurl)
return el
#
# This extension allows the use of [icon:icon_type] tags in source
#
class IconExtensionException(Exception):
pass
class IconExtension(Extension):
def extendMarkdown(self, md, md_globals):
pattern = r'\[(?P<prefix>icon:)(?P<type>[\w| ]+)\]'
tp = IconPattern(pattern)
tp.md = md
tp.ext = self
# Add inline pattern before "reference" pattern
md.inlinePatterns.add("icon", tp, "<reference")
class IconPattern(Pattern):
def getCompiledRegExp(self):
return re.compile("^(.*?)%s(.*)$" % self.pattern, re.DOTALL | re.UNICODE | re.IGNORECASE)
def handleMatch(self, m):
el = etree.Element('span')
el.set('class', 'icon-' + m.group(3).lower())
return el
#
# This extension allows the use of [type:some_type] tags in source.
# Expands to <code class="type">some_type</code>
#
class TypeExtensionException(Exception):
pass
class TypeExtension(Extension):
def extendMarkdown(self, md, md_globals):
pattern = r'\[(?P<prefix>type:)\s*(?P<type>.+?)\]'
tp = TypePattern(pattern)
tp.md = md
tp.ext = self
# Add inline pattern before "reference" pattern
md.inlinePatterns.add("type", tp, "<reference")
class TypePattern(Pattern):
def getCompiledRegExp(self):
return re.compile("^(.*?)%s(.*)$" % self.pattern, re.DOTALL | re.UNICODE | re.IGNORECASE)
def handleMatch(self, m):
el = etree.Element('code')
el.set('class', 'type')
types = m.group(3)
# Make sure types are shown as type1 | type2
types = re.sub(' or ', ' | ', types)
types = re.sub(r'(?<=\w)[|](?=\w)', ' | ', types)
el.text = types
return el
#
# Instance a markdown converter with some useful extensions
#
md = Markdown(extensions=['markdown.extensions.fenced_code','markdown.extensions.def_list',
'markdown.extensions.codehilite','markdown.extensions.tables',
TypeExtension(), IconExtension(), RefExtension()])
def _strip_comment_stars(str):
lines = str.split('\n')
ret = []
for line in lines:
line = line.strip()
if line.startswith('*'):
line = line[1:]
if line.startswith(' '):
line = line[1:]
ret.append(line)
return '\n'.join(ret)
def _parse_comment(str):
str = _strip_comment_stars(str)
# The regexp means match all strings that:
# * begins with line start, possible whitespace and an @
# * followed by non-white-space (the tag)
# * followed by possible spaces
# * followed by every character that is not an @ or is an @ but not preceded by a new line (the value)
lst = re.findall('^\s*@(\S+) *((?:[^@]|(?<!\n)@)*)', str, re.MULTILINE)
name_found = False
document_comment = False
element_type = script_doc_ddf_pb2.FUNCTION
for (tag, value) in lst:
tag = tag.strip()
value = value.strip()
if tag == 'name':
name_found = True
elif tag == 'variable':
element_type = script_doc_ddf_pb2.VARIABLE
elif tag == 'message':
element_type = script_doc_ddf_pb2.MESSAGE
elif tag == 'property':
element_type = script_doc_ddf_pb2.PROPERTY
elif tag == 'document':
document_comment = True
if not name_found:
logging.warn('Missing tag @name in "%s"' % str)
return None
desc_start = min(len(str), str.find('\n'))
brief = str[0:desc_start]
desc_end = min(len(str), str.find('\n@'))
description = str[desc_start:desc_end].strip()
element = None
if document_comment:
element = script_doc_ddf_pb2.Info()
element.brief = md.convert(brief)
element.description = md.convert(description)
else:
element = script_doc_ddf_pb2.Element()
element.type = element_type
element.brief = md.convert(brief)
element.description = md.convert(description)
namespace_found = False
for (tag, value) in lst:
value = value.strip()
md.reset()
if tag == 'name':
element.name = value
elif tag == 'return':
tmp = value.split(' ', 1)
if len(tmp) < 2:
tmp = [tmp[0], '']
ret = element.returnvalues.add()
ret.name = tmp[0]
ret.doc = md.convert(tmp[1])
elif tag == 'param':
tmp = value.split(' ', 1)
if len(tmp) < 2:
tmp = [tmp[0], '']
param = element.parameters.add()
param.name = tmp[0]
param.doc = md.convert(tmp[1])
elif tag == 'note':
element.note = md.convert(value)
elif tag == 'examples':
element.examples = md.convert(value)
elif tag == 'deprecated':
element.deprecated = md.convert(value)
elif tag == 'replaces':
element.replaces = md.convert(value)
elif tag == 'error':
element.error = md.convert(value)
elif tag == 'namespace' and document_comment:
# Do not set namespace unless this is a document_comment
element.namespace = value
namespace_found = True
if document_comment and not namespace_found:
logging.warn('Missing tag @namespace in "%s"' % str)
return None
return element
def parse_document(doc_str):
doc = script_doc_ddf_pb2.Document()
lst = re.findall('/\*#(.*?)\*/', doc_str, re.DOTALL)
element_list = []
doc_info = None
for comment_str in lst:
element = _parse_comment(comment_str)
if type(element) is script_doc_ddf_pb2.Element:
element_list.append(element)
if type(element) is script_doc_ddf_pb2.Info:
doc_info = element
if doc_info:
doc.info.CopyFrom(doc_info)
element_list.sort(cmp = lambda x,y: cmp(x.name, y.name))
for i, e in enumerate(element_list):
doc.elements.add().MergeFrom(e)
return doc
from google.protobuf.descriptor import FieldDescriptor
from google.protobuf.message import Message
import json
def message_to_dict(message):
ret = {}
for field in message.DESCRIPTOR.fields:
value = getattr(message, field.name)
if field.label == FieldDescriptor.LABEL_REPEATED:
lst = []
for element in value:
if isinstance(element, Message):
lst.append(message_to_dict(element))
else:
lst.append(str(element))
ret[field.name] = lst
elif field.type == FieldDescriptor.TYPE_MESSAGE:
ret[field.name] = message_to_dict(value)
elif field.type == FieldDescriptor.TYPE_ENUM:
ret[field.name] = field.enum_type.values_by_number[value].name
else:
ret[field.name] = str(value)
return ret
if __name__ == '__main__':
usage = "usage: %prog [options] INFILE(s) OUTFILE"
parser = OptionParser(usage = usage)
parser.add_option("-t", "--type", dest="type",
help="Supported formats: protobuf and json. default is protobuf", metavar="TYPE", default='protobuf')
(options, args) = parser.parse_args()
if len(args) < 2:
parser.error('At least two arguments required')
doc_str = ''
for name in args[:-1]:
f = open(name, 'rb')
doc_str += f.read()
f.close()
doc = parse_document(doc_str)
if doc:
output_file = args[-1]
f = open(output_file, "wb")
if options.type == 'protobuf':
f.write(doc.SerializeToString())
elif options.type == 'json':
doc_dict = message_to_dict(doc)
json.dump(doc_dict, f, indent = 2)
else:
print 'Unknown type: %s' % options.type
sys.exit(5)
f.close()