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