Genshi:无法从 HTML 中的指令访问导入的模块

Genshi: Can't access imported modules from directives in HTML

背景:我正在使用 Genshi 生成 HTML 报告。

import genshi
import os
from genshi.template import MarkupTemplate

files = [
    r"a\b\c.txt",
    r"d/e/f.txt",
]

html = '''
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/">
        <head>
        </head>
        <body>
            <p py:for="f in sorted(files, key=lambda x: x.lower().split(os.path.sep))">
                ${f}
            </p>
        </body>
    </html>
'''
template = MarkupTemplate(html)
stream = template.generate(
    files = files
)
print(stream.render('html'))

问题: Genshi 抛出 UndefinedError 异常,因为它不知道我导入的模块。

D:\SVN\OSI_SVT[=12=].0.0.0_swr65430\srcPy\OSI_SVT>python36 test.py
Traceback (most recent call last):
  File "test.py", line 26, in <module>
    print(stream.render('html'))
  File "C:\Python36\lib\site-packages\genshi\core.py", line 184, in render
    return encode(generator, method=method, encoding=encoding, out=out)
  ...
genshi.template.eval.UndefinedError: "os" not defined

问题:有没有办法让Genshi自动识别导入的模块?

如果这在 Genshi 中是不可能的,我会接受以编程方式创建模块集合的答案,这些模块已被导入,因此它们可以传递给 generate() 调用。例如:generate(**args)

我试过的:

我找到了一种在 Genshi 之外执行此操作的方法。这种方法添加所有全局和局部对象(导入以及全局和局部变量)并将它们添加到字典中。然后将字典作为关键字参数传递给 generate()(如果您对此不熟悉,请参阅 this answer)。

import genshi
import os
import types
from genshi.template import MarkupTemplate

html = '''
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/">
        <head>
        </head>
        <body>
            <p py:for="f in sorted(files, key=lambda x: x.lower().split(os.path.sep))">
                ${f}
            </p>
        </body>
    </html>
'''

def main():

    files = [
        r"a\b\c.txt",
        r"d/e/f.txt",
    ]

    generation_args = {}
    for scope in [globals(), locals()]:
        for name, value in scope.items():
            generation_args[name] = value

    template = MarkupTemplate(html)
    stream = template.generate(**generation_args)

    print(stream.render('html'))

main()