compile(self) -> None Compile self into Python code.
(self)
| 55 | yield ref |
| 56 | |
| 57 | def compile(self): |
| 58 | """ |
| 59 | compile(self) -> None |
| 60 | |
| 61 | Compile self into Python code. |
| 62 | """ |
| 63 | |
| 64 | if self.code is not None: |
| 65 | return |
| 66 | |
| 67 | level = 0 |
| 68 | source = '' |
| 69 | |
| 70 | for token in self.tokenize(): |
| 71 | indent = ' ' * level * 2 |
| 72 | |
| 73 | if token.startswith('###'): |
| 74 | source = source + '%s_ = _ + %s\n' % \ |
| 75 | (indent, token[3:].strip()) |
| 76 | |
| 77 | elif token.startswith('<?') and token.endswith('?>'): |
| 78 | content = token[2:-2] |
| 79 | |
| 80 | if content == 'end': |
| 81 | level = level - 1 |
| 82 | |
| 83 | elif content.startswith('if ') \ |
| 84 | or content.startswith('for ') \ |
| 85 | or content.startswith('while '): |
| 86 | source = source + '%s%s:\n' % (indent, content) |
| 87 | level = level + 1 |
| 88 | |
| 89 | elif content.startswith('='): |
| 90 | source = source + '%s_ = _ + %s\n' % \ |
| 91 | (indent, content[1:].strip()) |
| 92 | |
| 93 | else: |
| 94 | source = source + '%s_ = _ + """%s"""\n' % \ |
| 95 | (indent, token.replace('"""', r'\"\"\"')) |
| 96 | |
| 97 | self.code = compile(source, '<string>', 'exec') |
no test coverage detected