【发布时间】:2012-11-30 16:44:02
【问题描述】:
我想尝试在web2py 之外编写一些胶子衍生组件(web2py html 助手)。通过这种方式,我可以在命令行上测试组件,并生成简单的 html 文件,我可以在将它们与完整的web2py 应用程序堆栈集成之前对其进行分析。这加快了我的开发周期,因为我可以专注于微调组件的 HTML 而无需担心任何其他问题(重新部署、数据库等)。快速简单:使用 gluon.html 助手准备一个 web2py 组件,查看生成的 HTML 是否看起来不错,然后然后在 web2py 中使用它。
测试基本上是成功的。我只需要:
from gluon.html import A, TABLE, DIV, SPAN, THEAD, TR, TH, TBODY, URL, H1
def T(txt):
return txt
def my_simple_component(title):
return H1(T(title))
def test():
print my_simple_component('Hello')
产生预期的输出:
$ python test_web2py_components.py
<h1>Hello</h1>
(我不关心我的测试中的T。对于我想要使用的任何测试URL,我只传递一个假的应用程序/控制器/函数。)
我发现的唯一问题是我需要一个指向 web2py 的符号链接才能使 gluon.html 导入工作,即使 gluon 位于 PYTHONPATH 上。
ln -s /install_dir/web2py/gluon/ .
有没有办法在没有额外链接的情况下导入胶子组件?
考虑到在测试阶段我的组件在 web2py 应用程序树之外。
这是我得到的错误:
Traceback (most recent call last):
File "lib_test1.py", line 4, in <module>
from gluon.html import A, TABLE, DIV, SPAN, THEAD, TR, TH, TBODY, URL
File "/install_dir/web2py/gluon/__init__.py", line 15, in <module>
from globals import current
File "/install_dir/web2py/gluon/globals.py", line 24, in <module>
from serializers import json, custom_json
File "/install_dir/web2py/gluon/serializers.py", line 11, in <module>
from languages import lazyT
File "/install_dir/web2py/gluon/languages.py", line 264, in <module>
PLURAL_RULES = read_possible_plurals()
File "/install_dir/web2py/gluon/languages.py", line 250, in read_possible_plurals
for pname in os.listdir(pdir):
OSError: [Errno 2] No such file or directory: '/current_dir/gluon/contrib/rules'
(不要考虑install_dir和current_dir:他们已经为这个问题引入了)
【问题讨论】: