]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/dev/archery/archery/utils/report.py
6c7587ddd8729f1a8f6dcec48832d90e65c99351
[ceph.git] / ceph / src / arrow / dev / archery / archery / utils / report.py
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18 from abc import ABCMeta, abstractmethod
19 import datetime
20
21 import jinja2
22
23
24 def markdown_escape(s):
25 for char in ('*', '#', '_', '~', '`', '>'):
26 s = s.replace(char, '\\' + char)
27 return s
28
29
30 class Report(metaclass=ABCMeta):
31
32 def __init__(self, **kwargs):
33 for field in self.fields:
34 if field not in kwargs:
35 raise ValueError('Missing keyword argument {}'.format(field))
36 self._data = kwargs
37
38 def __getattr__(self, key):
39 return self._data[key]
40
41 @abstractmethod
42 def fields(self):
43 pass
44
45 @property
46 @abstractmethod
47 def templates(self):
48 pass
49
50
51 class JinjaReport(Report):
52
53 def __init__(self, **kwargs):
54 self.env = jinja2.Environment(
55 loader=jinja2.PackageLoader('archery', 'templates')
56 )
57 self.env.filters['md'] = markdown_escape
58 self.env.globals['today'] = datetime.date.today
59 super().__init__(**kwargs)
60
61 def render(self, template_name):
62 template_path = self.templates[template_name]
63 template = self.env.get_template(template_path)
64 return template.render(**self._data)