| 1 | import os |
|---|
| 2 | import genshi.template.loader |
|---|
| 3 | import genshi.template.markup |
|---|
| 4 | |
|---|
| 5 | template_src = """<module |
|---|
| 6 | xmlns:py="http://genshi.edgewall.org/" |
|---|
| 7 | xmlns:xi="http://www.w3.org/2001/XInclude" py:strip="True"> |
|---|
| 8 | <div py:content="module['name']" /> |
|---|
| 9 | <py:for each="module in module.get('children', [])"> |
|---|
| 10 | <!--! Here's where the recursion occurs --> |
|---|
| 11 | <xi:include href="module.xml" /> |
|---|
| 12 | </py:for> |
|---|
| 13 | </module> |
|---|
| 14 | """ |
|---|
| 15 | |
|---|
| 16 | """ |
|---|
| 17 | A module with a nested module |
|---|
| 18 | """ |
|---|
| 19 | module = dict( |
|---|
| 20 | type='module', |
|---|
| 21 | name='module 1', |
|---|
| 22 | children=[ |
|---|
| 23 | dict(type='module', name='module 2'), |
|---|
| 24 | ], |
|---|
| 25 | ) |
|---|
| 26 | |
|---|
| 27 | def test_file_loader(): |
|---|
| 28 | try: |
|---|
| 29 | with open('module.xml', 'w') as template_file: |
|---|
| 30 | template_file.write(template_src) |
|---|
| 31 | tmpl = genshi.template.loader.TemplateLoader(['.']).load('module.xml') |
|---|
| 32 | print(tmpl.generate(module=module)) |
|---|
| 33 | finally: |
|---|
| 34 | os.remove('module.xml') |
|---|
| 35 | |
|---|
| 36 | class SimpleLoader: |
|---|
| 37 | def load(self, name, **kwargs): |
|---|
| 38 | return genshi.template.markup.MarkupTemplate(template_src, loader=self) |
|---|
| 39 | |
|---|
| 40 | def test_simple_loader(): |
|---|
| 41 | tmpl = SimpleLoader().load('module.xml') |
|---|
| 42 | print(tmpl.generate(module=module)) |
|---|
| 43 | |
|---|
| 44 | if __name__ == '__main__': |
|---|
| 45 | # works with simple_loader |
|---|
| 46 | test_simple_loader() |
|---|
| 47 | # infinite recursion with file_loader |
|---|
| 48 | #test_file_loader() |
|---|