可选的可扩展资源实现。

falkolab.resource的Python项目详细描述


Falkolab.资源组件
============================ < BR>
这是一组可扩展资源的组件 < BR>
>;>>来自Zope导入组件,接口
>;>>来自falkolab.资源导入接口,资源类型 < BR>
>>gt;导入falkolab.resource
>>gt;导入操作系统,操作系统路径 < BR>
>>gt;导入临时文件
>;>;脚本文件名=tempfile.mktemp('script.js')
>;>;打开(scriptfilename,'w').write('script data')
>;>;imagefilename=tempfile.mktemp('image.jpg')
>;>;打开(imagefilename,'w').write('jpg image data')
>;>;ptfilename=tempfile.mktemp('template.pt')
>>gt;打开(ptfilename,'w').write('zope page template') < BR>
现在我们可以注册资源: < BR>
>>gt;从zope.configuration导入xmlconfig
>>gt;导入falkolab.resource
>>gt;导入zope.app.zcml文件 < BR>
>>>context=xmlconfig.file('configure.zcml',zope.app.zcmlfiles,execute=true)
>>>context=xmlconfig.file('configure.zcml',zope.app.schema,context=context,execute=true)
>>>context=xmlconfig.file('meta.zcml',falkolab.resource,context=context,execute=true)
>>>context=xmlconfig.file('configure.zcml',falkolab.resource,context=context,execute=true) < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…资源
…name="资源.js"
…src="%s"/>;
<;/配置>;
…""%scriptfilename,context=context,execute=true) < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…资源
…name="图像资源"
…src="%s"/>;
<;/配置>;
…""%imagefilename,context=context,execute=true) < BR>
检查适配器是否已注册: < BR>
>;>>来自zope.publisher.browser导入测试请求
>;>;请求=测试请求()
>>gt;响应=请求。响应 < BR>
>>>资源=组件。getadapter(
…请求,interface.interface,name='resource.js') < BR>
>>>资源
<;falkolab.resource.resourcetypes.fileresource对象位于…>; < BR>
>;>isInstance(resource.context,resourceTypes.file) 真的 < BR>
>>>资源=组件。getadapter(
…请求,interface.interface,name='image-resource') < BR>
>>>资源
<;falkolab.resource.resourcetypes.fileresource对象位于…>; < BR>
>;>;iInstance(resource.context,resourceTypes.image) 真的 < BR>
我们可以显式地指定资源类型 < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…资源
…name="image2资源"
type="图像"
…src="%s"/>;
<;/配置>;
…""%scriptfilename,context=context,execute=true) < BR>
>>>资源=组件。getadapter(
…请求,interface.interface,name='image2-resource') < BR>
>;>;iInstance(resource.context,resourceTypes.image) 真的 < BR>< BR>
自定义资源类型
---- < BR>
我们可以实现自定义资源类型。你只需要定义一个类
实现接口iresourcefactory的。 < BR>
>>gt;上下文=xmlconfig.strinG("""
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;
…<;资源类型
…name="自定义"
…factory="falkolab.resource.tests.customFileResourceFactory"/>;
<;/配置>;
…"",context=context,execute=true) < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…资源
…name="自定义资源"
type="自定义"
…src="%s"/>;
<;/配置>;
…""%scriptfilename,context=context,execute=true) < BR>
>>>资源=组件。getadapter(
…请求,interface.interface,name='custom-resource') < BR>
>>>资源
<;falkolab.resource.tests.customresource对象位于…>; < BR>< BR>
类型替换
---- < BR>
我们可以替换以前注册的类型: < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;
…<;资源类型
…name="zpt"
…掩码="*.pt"
…factory="falkolab.resource.tests.customFileResourceFactory"/>;
<;/配置>;
…"",context=context,execute=true) < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…资源
…name="pt资源"
…src="%s"/>;
<;/配置>;
…""%ptfilename,context=context,execute=true) < BR>
>>>资源=组件。getadapter(
…请求,interface.interface,name='pt-resource') < BR>
>>>资源
<;falkolab.resource.tests.customresource对象位于…>; < BR>
>;>;操作系统取消链接(scriptfilename)
>;>;操作系统。取消链接(imagefilename)
>;>;操作系统取消链接(ptfilename) < BR>< BR>
ZRT资源
---- < BR>
ZRT资源的文件名必须与以下掩码之一匹配:*.zrt.css*.zrt.js*.zrt
或者必须设置类型属性:type="zrt" < BR>
>;>;zrtfilename=tempfile.mktemp('style.zrt.css')
>;>;打开(Zrtfilename,'w')。写入('''\
/*ZRT替换:"foo""bar"*/
foo foo foo foo'') < BR>
>>gt;context=xmlconfig.string("")
<;配置
…xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…资源
…name="zpt资源"
…src="%s"/>;
<;/配置>;
…""%ZrtFileName,context=context,execute=true) < BR>
>>>资源=组件。getadapter(
…请求,interface.interface,name='zpt-resource') < BR>
>>>资源
<;falkolab.resource.zrt.zrtresourcetype.zrtresource对象位于…>; < BR>
>;>;打印资源。获取()
酒吧酒吧酒吧 < BR>
>;>;操作系统取消链接(ZrtFileName) < BR>
我们可以用"property"子指令指定zrt commant。我们也可以
如果文件扩展名与ZRT资源掩码不匹配,则为资源类型。 < BR>
>;>;zrtfilename=tempfile.mktemp('style.css')
>;>;打开(Zrtfilename,'w')。写入('''\
foo foo foo foo'') < BR>
>>gt;context=xmlconfig.string("")
<;配置
…xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…资源
…name="具有属性的zpt资源"
SRC="%s"
…type="zrt">;
…财产;财产
…name="zrt命令"
…值='/*zrt replace:"foo""muu"*/'/>;
…<;/资源>;
<;/配置>;
…""%ZrtFileName,context=context,execute=true) < BR>
>>>资源=组件。getadapter(
…请求,interface.interface,name='zpt-resource-with-property') < BR>
>;>;打印资源。获取()
缪缪缪缪 < BR>
>;>;操作系统取消链接(ZrtFileName) < BR>< BR>
目录资源
----- < BR>
>;>gt;目录名=tempfile.mkdtemp()
>;>;os.mkdir(os.path.join(dirname,'subfolder'))
>>gt;打开(os.path.join(dirname,'script.js'),'w').write('script data')
>>gt;打开(os.path.join(dirname,'style.css'),'w').write('style data')
>>gt;打开(os.path.join(dirname,'image.png'),'w').write('png image data') < BR>
我们可以注册资源目录并指定允许的资源类型: < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…<;资源
…name="resdir"
…type="目录"
…src="%s"/>;

<;/配置>;
…""%(dirname+"\u"),context=context,execute=true)
回溯(最近一次通话时间):

zopexmlconfigurationerror:…
配置错误:目录…不存在 < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;

…<;资源
…name="resdir"
…type="目录"
…src="%s"/>;

<;/配置>;
…""%dirname,context=context,execute=true) < BR>< BR>
>>gt;dirresource=component.getadapter(
…请求,interface.interface,name='resdir')
>>>直接资源
<;falkolab.resource.resourceTypes.directoryResource对象位于…>; < BR>
>>gt;dirresource.browserdefault(请求)
(<;函数在…>;,() < BR>
>>gt;资源=无
>>>resource=dirresource.publishtraverse(请求,'style.css') < BR>
>>>资源
<;falkolab.resource.resourcetypes.fileresource对象位于…>; < BR>
>;>isInstance(resource.context,resourceTypes.file) 真的 < BR>
>;>;打印资源。获取()
样式数据
>>gt;打印目录资源['style.css'].get()
样式数据 < BR>
>;>>目录资源['notpresent.css']
回溯(最近一次通话时间):

键错误:"notpresent.css" < BR>
>>gt;资源=无
>>>resource=dirresource.publishtraverse(请求,'notpresent.css')
回溯(最近一次通话时间):

未找到:… < BR>
>>>resource=dirresource.publishtraverse(请求,'image.png')
>>>资源
<;falkolab.resource.resourcetypes.fileresource对象位于…>; < BR>
>;>;iInstance(resource.context,resourceTypes.image) 真的 < BR>
>;>;打印资源。获取()
PNG图像数据 < BR>
>>>subdir=dirresource.publishtraverse(请求,"子文件夹")

<;falkolab.resource.resourceTypes.directoryResource对象位于…>; < BR>
我们可以为目录指定允许的资源类型: < BR>
>>gt;context=xmlconfig.string("")
<;配置
xmlns="http://namespaces.falkolab.ru/zope"
…package="falkolab.resource">;
< BR>…<;资源
…name="resdir2"
…type="目录"
…src="%s">;
…<;property name="types"value="image"/>;
…<;/资源>;

<;/配置>;
…""%dirname,context=context,execute=true) < BR>
>>gt;dirresource=无
>>gt;dirresource=component.getadapter(
…请求,interface.interface,name='resdir2') < BR>
>>>目录资源类型
[u'image'] < BR>
>>>resource=dirresource.publishtraverse(请求,'style.css')
回溯(最近一次通话时间):

未找到:… < BR>
>>>resource=dirresource.publishtraverse(请求,'image.png')
>>>资源
<;falkolab.resource.resourcetypes.fileresource对象位于…>; < BR>
>;>;iInstance(resource.context,resourceTypes.image) 真的 < BR>
>;>os.unlink(os.path.join(dirname,'script.js'))
>;>os.unlink(os.path.join(dirname,'style.css'))
>;>;os.unlink(os.path.join(dirname,'image.png'))
>;>;os.rmdir(os.path.join(dirname,'subfolder'))
>>>os.rmdir(目录名) < BR> 变化/变化 [谚] < BR>
1.0.3(2009-08-21)
-----
-更新测试
-一些小的修复 < BR>
1.0.2(2009-03-13)
----- < BR>
-错误修复:ZRT资源属性分配无效 < BR>
1.0.1(2009-03-13)
----- < BR>
-错误修复:无法为不同层注册两个同名资源
具有相同命名的属性子目录 < BR>
1.0.0(2009-03-11)
----- < BR>
-首次发布

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Switch语句,用于根据用户输入确定字母等级(十进制)   使用rich:datascroller更改页面后,java actionListener无法在rich:dataTable中工作   使用Azure AD帐户时java强制密码更改问题   java如何在flink中模拟异步操作   java如何将整数格式化为两个小数?   基于javascript条件访问amazon lamda结果?   java成本/从源节点到目标节点的所有可能路径的距离   java为什么我的光标在NextLine()之后的错误行上?   java Gridlayout未根据约束调整大小   windows使用特殊路径名在java中加载文件   如何在java上使用selenium在移动浏览器中滑动   Java中字符串类的成员使用了哪种utf8编码?   java删除截击缓存   java Musicg库不适用于mp3、rm、wma