summaryrefslogtreecommitdiffstats
path: root/src/sbus/codegen/sbus_Template.py
blob: 51016c358556871ea2c724a832c38af2f1c23eea (plain)
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
#
#   Authors:
#       Pavel Brezina <pbrezina@redhat.com>
#
#   Copyright (C) 2017 Red Hat
#
#   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, see <http://www.gnu.org/licenses/>.
#

import os
import re
import errno
import textwrap
import os.path


class Template:
    def __init__(self, name, templateFile, template):
        template = self.removeLines(template)

        self.templateFile = templateFile
        self.name = name
        self.loops = {}
        self.toggles = {}
        self.template = self.parse(template)
        self.output = ""

    def parse(self, template):
        template = self.parseLoops(template)
        template = self.parseToggles(template)
        return template

    def parseLoops(self, template):
        template = self.Pattern.Loop.sub(self.processLoops, template)
        return self.Pattern.LoopLine.sub(self.processLoops, template)

    def processLoops(self, match):
        name = match.group(1)
        template = self.removeLines(match.group(2))
        index = 0

        if name not in self.loops:
            self.loops[name] = self.Loop()

        index = self.loops[name].addTemplate(template)
        return '$${loop:%s:%d}' % (name, index)

    def parseToggles(self, template):
        template = self.Pattern.Toggle.sub(self.processToggles, template)
        return self.Pattern.ToggleLine.sub(self.processToggles, template)

    def processToggles(self, match):
        name = match.group(1)
        if_visible = self.removeLines(match.group(2))
        if_hidden = self.removeLines(match.group(4))
        index = 0

        if name not in self.toggles:
            self.toggles[name] = self.Toggle()

        index = self.toggles[name].addTemplate(if_visible, if_hidden)
        return '$${toggle:%s:%d}' % (name, index)

    def add(self, loop_name, values):
        """Add new item into <loop name="$loop_name"> template.
           Setting its attributes to $values.
        """
        if loop_name not in self.loops:
            return self
        self.loops[loop_name].set(values)
        return self

    def show(self, toggle_name, isVisible):
        """Make <toggle name="$toggle_name"> either visible or hidden
           within the template.
        """
        if not self.hasToggle(toggle_name):
            return

        self.toggles[toggle_name].show(isVisible)

    def hasToggle(self, name):
        return name in self.toggles

    def hasLoop(self, name):
        return name in self.loops

    def set(self, values):
        """Set template attributes to $values, push generated content into
           the output file and reset this template.
        """
        template = self.template
        for key, toggle in self.toggles.items():
            for idx, toggletpl in enumerate(toggle.templates):
                pattern = "$${toggle:%s:%d}" % (key, idx)
                template = template.replace(pattern, toggletpl.generate())

        self.output = self.Set(template, values)
        self.templateFile.push(self.generate())
        self.clear()

    def pushOriginal(self):
        """Push original template into the output file
        """
        self.templateFile.push(self.template)

    def clear(self):
        for loop in self.loops.values():
            loop.clear()

        for toggle in self.toggles.values():
            toggle.show(False)

        self.output = ""

    def generate(self):
        output = self.output
        for key, loop in self.loops.items():
            for idx, content in enumerate(loop.templates):
                pattern = "$${loop:%s:%d}" % (key, idx)
                output = output.replace(pattern, loop.get(idx), 1)
        return output

    @staticmethod
    def Set(content, values):
        output = content
        for key, value in values.items():
            output = output.replace("${" + key + "}", str(value))
        return output

    def removeLines(self, content):
        """Remove unneeded lines and spaces. There are some additional lines
           and spaces that may end up in the template after parsing. This
           method will remove new line after <@template-tag> and spaces
           from otherwise empty lines.
        """
        if content is None:
            return content

        content = self.Pattern.NewLine.sub('', content, 1)
        content = self.Pattern.EmptyLine.sub('', content)
        return content

    class Pattern:
        Template = re.compile(
            r' *<template name="(\S+)">(.*?)</template>\r?\n?',
            re.MULTILINE | re.DOTALL
        )

        Loop = re.compile(
            r' *<loop name="(\S+)">(.*?)</loop>\r?\n?',
            re.MULTILINE | re.DOTALL
        )

        LoopLine = re.compile(
            r'<loop line name="(\S+)">(.*?)</loop>',
            re.MULTILINE | re.DOTALL
        )

        Toggle = re.compile(
            r' *<toggle name="(\S+)">(.*?)(<or>(.*?))?</toggle>\r?\n?',
            re.MULTILINE | re.DOTALL
        )

        ToggleLine = re.compile(
            r'<toggle line name="(\S+)">(.*?)(<or>(.*?))?</toggle>',
            re.MULTILINE | re.DOTALL
        )

        NewLine = re.compile('^\r?\n')

        EmptyLine = re.compile('^ *$', re.MULTILINE)

    class Loop:
        def __init__(self):
            self.templates = []
            self.num_templates = 0

        def addTemplate(self, template):
            self.templates.append(self.LoopTemplate(template))
            self.num_templates += 1
            return self.num_templates - 1

        def set(self, values):
            for template in self.templates:
                template.set(values)

        def clear(self):
            for template in self.templates:
                template.clear()

        def get(self, index):
            return self.templates[index].generate()

        class LoopTemplate:
            def __init__(self, template):
                self.template = template
                self.output = ""

            def set(self, values):
                self.output += Template.Set(self.template, values)

            def clear(self):
                self.output = ""

            def generate(self):
                return self.output

    class Toggle:
        def __init__(self):
            self.templates = []
            self.num_templates = 0
            self.visible = False

        def addTemplate(self, if_visible, if_hidden):
            toggletpl = self.ToggleTemplate(self, if_visible, if_hidden)
            self.templates.append(toggletpl)
            self.num_templates += 1
            return self.num_templates - 1

        def show(self, isVisible):
            self.visible = isVisible

        class ToggleTemplate:
            def __init__(self, toggle, if_visible, if_hidden):
                self.toggle = toggle
                self.if_visible = if_visible
                self.if_hidden = if_hidden

            def generate(self):
                if self.toggle.visible:
                    return self.if_visible
                elif self.if_hidden is not None:
                    return self.if_hidden

                return ''


class TemplateFile:
    """Parse file contents into templates.

       Obtain template with .get and set its content. When all the content is
       set, you can call .generate to obtain generated content or .write
       to write it to a file.
    """

    def __init__(self, path):
        with open(path, "r") as file:
            contents = file.read()

        self.templates = {}
        self.output = ""
        self.parse(contents)

    def parse(self, template):
        for (name, content) in Template.Pattern.Template.findall(template):
            content = textwrap.dedent(content)
            self.templates[name] = Template(name, self, content)

    def get(self, name):
        return self.templates[name]

    def has(self, name):
        return name in self.templates

    def push(self, content):
        self.output += content

    def generate(self):
        return self.output

    def write(self, filename, postprocess=None):
        dirname = os.path.dirname(filename)
        if not os.path.exists(dirname):
            try:
                os.makedirs(dirname)
            except OSError as exception:
                if exception.errno == errno.EEXIST and os.path.isdir(filename):
                    pass
                else:
                    raise

        output = self.generate().rstrip() + '\n'
        if postprocess is not None:
            output = postprocess(output)

        if not self.needsOverride(filename, output):
            return

        with open(filename, "w") as file:
            file.write(output)

    def needsOverride(self, filename, content):
        """
            Do not override the file unless it is not yet present or its
            current content differs from the generated one. This ensure
            that the file is in correct state and yet it is not rebuild
            during make unless necessary.
        """
        if not os.path.isfile(filename):
            return True

        with open(filename, "r") as file:
            current_content = file.read()
            if current_content != content:
                return True

        return False

    def __str__(self):
        return self.generate()