对grok的静态资源库支持。
megrok.resourcelibrar的Python项目详细描述
megrok.resourcelibrary:grok中的资源
简介
grok已经提供了一种公开静态文件的简单方法。 资源,static目录。
^ tT2}$允许更灵活地包含 grok中的静态文件资源。它使用zc.resourcelibrary 打包来做这个。
资源库本质上类似于^{tt1}这样的目录$ 包的目录,充满静态资源,如css文件, javascript文件和图像。资源的用途是 HTML页面,作为帮助显示特定 布局或用户界面。
<{T{2}}比GROK默认更灵活 static目录?- 资源库可以位于层中。
- 资源库可以具有非公共权限。
- 一个资源库可以更容易地打包供其他人重用 图书馆。资源库在 开发商。
- 资源库可以自动包含一些资源(例如 在网页的head部分中 特定的小部件需要它。
- 资源库也可以依赖于其他库。
基本示例
让我们看看这一切是如何运作的。首先我们得摸摸这个包裹 本身(通常从zcml完成):
>>> from grok.testing import grok >>> grok('megrok.resourcelibrary.meta')
现在我们可以设置一个简单的资源库:
>>> import grok >>> import megrok.resourcelibrary >>> class SomeLibrary(megrok.resourcelibrary.ResourceLibrary): ... megrok.resourcelibrary.directory('tests/example')
我们需要摸索一下才能使它可用(在正常使用中,这已经完成了 自动为您提供):
>>> from grok.testing import grok_component >>> grok_component('SomeLibrary', SomeLibrary) True
默认情况下,此目录中的资源现在发布在 库的类名,小写(因此 somelibrary):
>>> from zope.testbrowser.testing import Browser >>> browser = Browser() >>> browser.open('http://localhost/@@/somelibrary/my-lib/included.js') >>> print browser.contents function be_annoying() { alert('Hi there!'); }
可以使用^{tt9}覆盖默认名称$ 指令:
>>> class SomeLibrary2(megrok.resourcelibrary.ResourceLibrary): ... grok.name('some-library') ... megrok.resourcelibrary.directory('tests/example') >>> grok_component('SomeLibrary2', SomeLibrary2) True >>> browser.open('http://localhost/@@/some-library/my-lib/included.js') >>> print browser.contents function be_annoying() { alert('Hi there!'); }
指向一个不存在的目录是一个错误:
>>> class WrongDirectory(megrok.resourcelibrary.ResourceLibrary): ... grok.name('wrong-directory') ... megrok.resourcelibrary.directory('tests/doesnt_exist') >>> grok_component('WrongDirectory', WrongDirectory) Traceback (most recent call last): ... GrokError: Directory 'tests/doesnt_exist' is not a valid directory passed to the 'wrong-directory' directive.
资源的自动包含
我们现在建立了一个资源库,它自动包含两个 在网页中使用资源,即included.js 以及included.css:
>>> class MyLib(megrok.resourcelibrary.ResourceLibrary): ... grok.name('my-lib') ... megrok.resourcelibrary.directory('tests/example/my-lib') ... megrok.resourcelibrary.include('included.js') ... megrok.resourcelibrary.include('included.css') >>> grok_component('MyLib', MyLib) True
这就是您要求在特定页面中加载库的方式 模板:
<tal:block replace="resource_library:my-lib"/>
test_template_2提出了这个要求,因此包含的javascript 应包括:
>>> browser.open('http://localhost/zc.resourcelibrary.test_template_2') >>> '/@@/my-lib/included.js' in browser.contents True
资源也被发布:
>>> browser.open('/@@/my-lib/included.js') >>> print browser.contents function be_annoying() { alert('Hi there!'); }
对css的引用也插入到html中:
>>> browser.open('http://localhost/zc.resourcelibrary.test_template_2') >>> '/@@/my-lib/included.css' in browser.contents True
css可以从引用的url获得:
>>> browser.open('/@@/my-lib/included.css') >>> print browser.contents div .border { border: 1px silid black; }
以编程方式发送资源需求信号
上面我们演示了resource_library命名空间的使用 在ZPT。对于 视图中的实例:
>>> import grok >>> from zope.interface import Interface >>> class View(grok.View): ... grok.context(Interface) ... def render(self): ... MyLib.need() ... return '<html><head></head><body>Example</body></html>' >>> grok_component('View', View) True >>> browser.open('http://localhost/view') >>> '/@@/my-lib/included.js' in browser.contents True
这也适用于没有显式grok.name:
的库。>>> class MyLib2(megrok.resourcelibrary.ResourceLibrary): ... megrok.resourcelibrary.directory('tests/example/my-lib') ... megrok.resourcelibrary.include('included.js') ... megrok.resourcelibrary.include('included.css') >>> grok_component('MyLib2', MyLib2) True >>> class View2(grok.View): ... grok.context(Interface) ... def render(self): ... MyLib2.need() ... return '<html><head></head><body>Example</body></html>' >>> grok_component('View2', View2) True >>> browser.open('http://localhost/view2') >>> '/@@/mylib2/included.js' in browser.contents True
您也可以通过库名称来表示包含(就像在页面模板中一样):
>>> class View3(grok.View): ... grok.context(Interface) ... def render(self): ... megrok.resourcelibrary.need('my-lib') ... return '<html><head></head><body>Example</body></html>' >>> grok_component('View3', View3) True >>> browser.open('http://localhost/view3') >>> '/@@/my-lib/included.js' in browser.contents True
使资源库依赖于其他资源库
我们可以使一个资源库依赖于另一个资源库:
>>> class Dependency(megrok.resourcelibrary.ResourceLibrary): ... megrok.resourcelibrary.directory('tests/example') ... megrok.resourcelibrary.include('1.js') >>> grok_component('Dependency', Dependency) True >>> class Dependent(megrok.resourcelibrary.ResourceLibrary): ... megrok.resourcelibrary.directory('tests/example') ... megrok.resourcelibrary.include('2.css') ... megrok.resourcelibrary.depend(Dependency) >>> grok_component('Dependent', Dependent) True
让我们创建一个需要Dependent:
>>> class DependentView(grok.View): ... grok.context(Interface) ... def render(self): ... Dependent.need() ... return '<html><head></head><body>Example</body></html>' >>> grok_component('DependentView', DependentView) True
包含的原始代码和依赖项代码现在将 显示:
>>> browser.open('http://localhost/dependentview') >>> '/@@/dependency/1.js' in browser.contents True >>> '/@@/dependent/2.css' in browser.contents True
保护资源
可以授予资源权限:
>>> class MyPermission(grok.Permission): ... grok.name("my.permission") >>> grok_component('MyPermission', MyPermission) True >>> class MyLib3(megrok.resourcelibrary.ResourceLibrary): ... megrok.resourcelibrary.directory('tests/example/my-lib') ... grok.require(MyPermission) >>> grok_component('MyLib3', MyLib3) True
xxx这还不起作用,因为资源本身不安全 检查,但依靠代理,这已删除……需要介绍 新资源/工厂需要手工检查。
更改
0.9.2(2008-08-08)
- Grokker错误地依赖(未导入)GrokImportError失败 案例。将其更正为GrokError,并为其添加了一个测试。
- 将use指令重命名为depend,并使其接受一个类 参数而不是库名称。
0.9.1(2008-08-06)
- 关闭拉链安全。
- 实际添加一个meta.zcml加载了Grokker!
0.9(2008-08-06)
- 首次公开发行。