-
Notifications
You must be signed in to change notification settings - Fork 55
/
code.py
420 lines (349 loc) · 14.6 KB
/
code.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import os.path
import re
import typing
import mistune
from jinja2 import Environment, FileSystemLoader, select_autoescape
from opentelemetry.semconv.model.semantic_attribute import (
AttributeType,
EnumAttributeType,
EnumMember,
RequirementLevel,
SemanticAttribute,
StabilityLevel,
TextWithLinks,
)
from opentelemetry.semconv.model.semantic_convention import (
BaseSemanticConvention,
MetricSemanticConvention,
SemanticConventionSet,
)
from opentelemetry.semconv.model.utils import ID_RE
def render_markdown(
txt: str,
html=True,
link=None,
image=None,
emphasis=None,
strong=None,
inline_html=None,
paragraph=None,
heading=None,
block_code=None,
block_quote=None,
list=None, # pylint:disable=redefined-builtin
list_item=None,
code=None,
):
class CustomRender(mistune.HTMLRenderer):
def link(self, url, text=None, title=None): # pylint:disable=arguments-renamed
if link:
return link.format(url, text, title)
return super().link(url, text, title) if html else url
def image(self, src, alt="", title=None):
if image:
return image.format(src, alt, title)
return super().image(src, alt, title) if html else src
def emphasis(self, text):
if emphasis:
return emphasis.format(text)
return super().emphasis(text) if html else text
def strong(self, text):
if strong:
return strong.format(text)
return super().strong(text) if html else text
def inline_html(self, html_text): # pylint:disable=arguments-renamed
if inline_html:
return inline_html.format(html_text)
return super().inline_html(html_text) if html else html_text
def paragraph(self, text):
if paragraph:
return paragraph.format(text)
return super().paragraph(text) if html else text
def heading(self, text, level):
if heading:
return heading.format(text, level)
return super().heading(text, level) if html else text
def block_code(self, code, info=None):
if block_code:
return block_code.format(code)
return super().block_code(code, info) if html else code
def block_quote(self, text):
if block_quote:
return block_quote.format(text)
return super().block_quote(text)
def list(self, text, ordered, level, start=None):
if list:
return list.format(text)
return super().list(text, ordered, level, start) if html else text
def list_item(self, text, level):
if list_item:
return list_item.format(text)
return super().list_item(text, level) if html else text
def codespan(self, text):
if code:
return code.format(text)
return super().codespan(text) if html else text
markdown = mistune.create_markdown(renderer=CustomRender())
return markdown(txt)
def to_doc_brief(doc_string: typing.Optional[str]) -> str:
if doc_string is None:
return ""
doc_string = doc_string.strip()
if doc_string.endswith("."):
return doc_string[:-1]
return doc_string
def print_member_value(attr: SemanticAttribute, member: EnumMember) -> str:
if (
isinstance(attr.attr_type, EnumAttributeType)
and attr.attr_type.enum_type == "string"
):
return f'"{member.value}"'
return str(member.value)
def to_html_links(doc_string: typing.Optional[typing.Union[str, TextWithLinks]]) -> str:
if doc_string is None:
return ""
if isinstance(doc_string, TextWithLinks):
str_list = []
for elm in doc_string.parts:
if isinstance(elm, str):
str_list.append(elm)
else:
str_list.append(f'<a href="{elm.url}">{elm.text}</a>')
doc_string = "".join(str_list)
doc_string = doc_string.strip()
if doc_string.endswith("."):
return doc_string[:-1]
return doc_string
def regex_replace(text: str, pattern: str, replace: str):
# convert standard dollar notation to python
replace = re.sub(r"\$", r"\\", replace) # TODO This is *very* surprising behavior
return re.sub(pattern, replace, text, 0, re.U)
def merge(elems: typing.List, elm):
return elems.extend(elm)
def to_const_name(name: str) -> str:
return name.upper().replace(".", "_").replace("-", "_")
def to_camelcase(name: str, first_upper=False) -> str:
first, *rest = name.replace("_", ".").split(".")
if first_upper:
first = first.capitalize()
return first + "".join(word.capitalize() for word in rest)
def to_snake_case(name):
name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
name = re.sub("__([A-Z])", r"_\1", name)
name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", name)
return name.lower()
def first_up(name: str) -> str:
return name[0].upper() + name[1:]
def is_stable(obj: typing.Union[SemanticAttribute, BaseSemanticConvention]) -> bool:
return obj.stability == StabilityLevel.STABLE
def is_deprecated(obj: typing.Union[SemanticAttribute, BaseSemanticConvention]) -> bool:
return obj.stability == StabilityLevel.DEPRECATED
def is_experimental(
obj: typing.Union[SemanticAttribute, BaseSemanticConvention]
) -> bool:
return obj.stability is None or obj.stability == StabilityLevel.EXPERIMENTAL
def is_definition(attribute: SemanticAttribute) -> bool:
return attribute.is_local and attribute.ref is None
def is_template(attribute: SemanticAttribute) -> bool:
return AttributeType.is_template_type(str(attribute.attr_type))
def is_metric(semconv: BaseSemanticConvention) -> bool:
return isinstance(semconv, MetricSemanticConvention)
class CodeRenderer:
pattern = f"{{{ID_RE.pattern}}}"
parameters: typing.Dict[str, str]
trim_whitespace: bool
@staticmethod
def from_commandline_params(parameters=None, trim_whitespace=False):
if parameters is None:
parameters = []
params = {}
if parameters:
for elm in parameters:
pairs = elm.split(",")
for pair in pairs:
(k, v) = pair.split("=")
params[k] = v
return CodeRenderer(params, trim_whitespace)
def __init__(self, parameters: typing.Dict[str, str], trim_whitespace: bool):
self.parameters = parameters
self.trim_whitespace = trim_whitespace
def get_data_single_file(
self, semconvset: SemanticConventionSet, template_path: str
) -> dict:
"""Returns a dictionary that contains all SemanticConventions to fill the template."""
data = {
"template": template_path,
"semconvs": semconvset.models,
"attributes": semconvset.attributes(),
"attribute_templates": semconvset.attribute_templates(),
"attributes_and_templates": self._grouped_attribute_definitions(semconvset),
"metrics": self._all_metrics_definitions(semconvset),
}
data.update(self.parameters)
return data
def get_data_multiple_files(
self, semconv, template_path
) -> typing.Dict[str, typing.Any]:
"""Returns a dictionary with the data from a single SemanticConvention to fill the template."""
data = {"template": template_path, "semconv": semconv}
data.update(self.parameters)
return data
@staticmethod
def setup_environment(env: Environment, trim_whitespace: bool):
env.filters["to_doc_brief"] = to_doc_brief
env.filters["to_const_name"] = to_const_name
env.filters["merge"] = merge
env.filters["to_camelcase"] = to_camelcase
env.filters["first_up"] = first_up
env.filters["to_html_links"] = to_html_links
env.filters["regex_replace"] = regex_replace
env.filters["render_markdown"] = render_markdown
env.filters["print_member_value"] = print_member_value
env.filters["is_deprecated"] = is_deprecated
env.filters["is_definition"] = is_definition
env.filters["is_stable"] = is_stable
env.filters["is_experimental"] = is_experimental
env.filters["is_template"] = is_template
env.filters["is_metric"] = is_metric
env.tests["is_stable"] = is_stable
env.tests["is_experimental"] = is_experimental
env.tests["is_deprecated"] = is_deprecated
env.tests["is_definition"] = is_definition
env.tests["is_template"] = is_template
env.tests["is_metric"] = is_metric
env.trim_blocks = trim_whitespace
env.lstrip_blocks = trim_whitespace
@staticmethod
def prefix_output_file(env, file_name, prefix):
# We treat incoming file names as a pattern.
# We allow will give them access to the same jinja model as file creation
# and we'll make sure a few things are available there, specifically:
# pascal case, camel case and snake case
data = {
"prefix": prefix,
"pascal_prefix": to_camelcase(prefix, True),
"camel_prefix": to_camelcase(prefix, False),
"snake_prefix": to_snake_case(prefix),
}
template = env.from_string(file_name)
full_name = template.render(data)
dirname = os.path.dirname(full_name)
basename = os.path.basename(full_name)
return os.path.join(dirname, basename)
def render(
self,
semconvset: SemanticConventionSet,
template_path: str,
output_file,
pattern: str,
):
file_name = os.path.basename(template_path)
folder = os.path.dirname(template_path)
env = Environment(
loader=FileSystemLoader(searchpath=folder),
autoescape=select_autoescape([""]),
)
self.setup_environment(env, self.trim_whitespace)
if pattern == "root_namespace":
self._render_group_by_root_namespace(
semconvset, template_path, file_name, output_file, env
)
elif pattern is not None:
self._render_by_pattern(
semconvset, template_path, file_name, output_file, pattern, env
)
else:
data = self.get_data_single_file(semconvset, template_path)
template = env.get_template(file_name, globals=data)
self._write_template_to_file(template, data, output_file)
def _render_by_pattern(
self,
semconvset: SemanticConventionSet,
template_path: str,
file_name: str,
output_file: str,
pattern: str,
env: Environment,
):
for semconv in semconvset.models.values():
prefix = getattr(semconv, pattern)
output_name = self.prefix_output_file(env, output_file, prefix)
data = self.get_data_multiple_files(semconv, template_path)
template = env.get_template(file_name, globals=data)
self._write_template_to_file(template, data, output_name)
def _render_group_by_root_namespace(
self,
semconvset: SemanticConventionSet,
template_path: str,
file_name: str,
output_file: str,
env: Environment,
):
attribute_and_templates = self._grouped_attribute_definitions(semconvset)
metrics = self._grouped_metric_definitions(semconvset)
for ns, attribute_and_templates in attribute_and_templates.items():
sanitized_ns = ns if ns != "" else "other"
output_name = self.prefix_output_file(env, output_file, sanitized_ns)
data = {
"template": template_path,
"attributes_and_templates": attribute_and_templates,
"enum_attributes": [a for a in attribute_and_templates if a.is_enum],
"metrics": metrics.get(ns) or [],
"root_namespace": sanitized_ns,
}
data.update(self.parameters)
template = env.get_template(file_name, globals=data)
self._write_template_to_file(template, data, output_name)
def _grouped_attribute_definitions(self, semconvset):
grouped_attributes = {}
for semconv in semconvset.models.values():
for attr in semconv.attributes_and_templates:
if not is_definition(attr): # skip references
continue
if attr.root_namespace not in grouped_attributes:
grouped_attributes[attr.root_namespace] = []
grouped_attributes[attr.root_namespace].append(attr)
for ns in grouped_attributes:
grouped_attributes[ns] = sorted(grouped_attributes[ns], key=lambda a: a.fqn)
return grouped_attributes
def _grouped_metric_definitions(self, semconvset):
grouped_metrics = {}
for semconv in semconvset.models.values():
if not is_metric(semconv):
continue
if semconv.root_namespace not in grouped_metrics:
grouped_metrics[semconv.root_namespace] = []
grouped_metrics[semconv.root_namespace].append(semconv)
for ns in grouped_metrics:
grouped_metrics[ns] = sorted(
grouped_metrics[ns], key=lambda a: a.metric_name
)
return grouped_metrics
def _all_metrics_definitions(self, semconvset):
all_metrics = []
for semconv in semconvset.models.values():
if is_metric(semconv):
all_metrics.append(semconv)
return sorted(all_metrics, key=lambda a: a.metric_name)
def _write_template_to_file(self, template, data, output_name):
template.globals["now"] = datetime.datetime.utcnow()
template.globals["version"] = os.environ.get("ARTIFACT_VERSION", "dev")
template.globals["RequirementLevel"] = RequirementLevel
content = template.render(data)
if content != "":
with open(output_name, "w", encoding="utf-8") as f:
f.write(content)