| 1 | import genshi.template.markup |
|---|
| 2 | |
|---|
| 3 | template_src = """<module |
|---|
| 4 | xmlns:py="http://genshi.edgewall.org/" |
|---|
| 5 | xmlns:xi="http://www.w3.org/2001/XInclude" py:strip="True"> |
|---|
| 6 | <div py:content="module['name']" /> |
|---|
| 7 | <py:for each="module in module.get('children', [])"> |
|---|
| 8 | <!--! Here's where the recursion occurs --> |
|---|
| 9 | <xi:include href="module.xml" /> |
|---|
| 10 | </py:for> |
|---|
| 11 | </module> |
|---|
| 12 | """ |
|---|
| 13 | |
|---|
| 14 | module = dict( |
|---|
| 15 | type='module', |
|---|
| 16 | name='module 1', |
|---|
| 17 | children=[ |
|---|
| 18 | dict(type='module', name='module 2'), |
|---|
| 19 | ], |
|---|
| 20 | ) |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class SimpleLoader(object): |
|---|
| 24 | |
|---|
| 25 | def __init__(self, auto_reload): |
|---|
| 26 | self.auto_reload = auto_reload |
|---|
| 27 | self.tmpl = genshi.template.markup.MarkupTemplate( |
|---|
| 28 | template_src, filepath=None, filename='module.xml', loader=self) |
|---|
| 29 | self.loaded = 0 |
|---|
| 30 | |
|---|
| 31 | def load(self, name, **kwargs): |
|---|
| 32 | self.loaded += 1 |
|---|
| 33 | return self.tmpl |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | def test_simple_loader(auto_reload): |
|---|
| 37 | loader = SimpleLoader(auto_reload) |
|---|
| 38 | tmpl = loader.load('module.xml') |
|---|
| 39 | print(tmpl.generate(module=module)) |
|---|
| 40 | print "Loaded", loader.loaded, "templates." |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | if __name__ == '__main__': |
|---|
| 44 | print "Testing with auto_reload = True, expecting:" |
|---|
| 45 | print " - 1 load on prepare" |
|---|
| 46 | print " - 1 load during rendering" |
|---|
| 47 | test_simple_loader(auto_reload=True) |
|---|
| 48 | |
|---|
| 49 | print "Testing with auto_reload = False, expecting:" |
|---|
| 50 | print " - 1 load on prepare" |
|---|
| 51 | print " - 1 load to check filepath during prepare" |
|---|
| 52 | print " - 1 load to during rendering" |
|---|
| 53 | test_simple_loader(auto_reload=False) |
|---|