Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install z3ext.resource

How to install z3ext.resource

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install z3ext.resource
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
1.3.0 Available View build log
Windows (64-bit)
1.3.0 Available View build log
Mac OS X (10.5+)
1.3.0 Available View build log
Linux (32-bit)
1.3.0 Available View build log
Linux (64-bit)
1.3.0 Available View build log
Web
 
Author
License
ZPL 2.1
Lastest release
version 1.3.0 on Jan 5th, 2011

z3ext:resource Directive

This package provides a new directive similar to browser:resource directive.

>>> from zope import component, interface
>>> from z3ext.resource import interfaces, fileresource
>>> import z3ext.resource
>>> from zope.configuration import xmlconfig
>>> context = xmlconfig.file('meta.zcml', z3ext.resource)

Now we can register a resource:

>>> import tempfile, os, os.path
>>> fn = tempfile.mktemp('.js')
>>> open(fn, 'w').write('''some resource data''')
>>> context = xmlconfig.string('''
... <configure xmlns:z3ext="http://namespaces.zope.org/z3ext">
...   <z3ext:resource
...       name="resource.js"
...       file="%s" />
... </configure>
... '''%fn, context=context)

Now let's see whether the adapter has been registered:

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> response = request.response
>>> resource = component.getAdapter(
...    request, interface.Interface, name='resource.js')
>>> modified = resource.modified()
>>> resource.render(request)
'some resource data'

By default resource is FileResource

>>> isinstance(resource.context, fileresource.File)
True

We can set resource type explicitly

>>> context = xmlconfig.string('''
... <configure xmlns:z3ext="http://namespaces.zope.org/z3ext">
...   <z3ext:resource
...       name="resource-image"
...       type="imageresource"
...       file="%s" />
... </configure>
... ''' %fn, context=context)
>>> resource = component.getAdapter(
...    request, interface.Interface, name='resource-image')
>>> isinstance(resource.context, fileresource.Image)
True
Custom resource type

We have to register IResourceFactoryType utility

>>> from z3ext.resource.tests import CustomFileResourceFactory
>>> custom = component.factory.Factory(
...   CustomFileResourceFactory, interfaces=(interfaces.IResourceFactory,))
>>> component.provideUtility(
...     custom, interfaces.IResourceFactoryType, name='custom')
>>> context = xmlconfig.string('''
... <configure xmlns:z3ext="http://namespaces.zope.org/z3ext">
...   <z3ext:resource
...       name="resource-custom"
...       type="custom"
...       file="%s" />
... </configure>
... ''' %fn, context=context)
>>> resource = component.getAdapter(
...    request, interface.Interface, name='resource-custom')
>>> resource
<z3ext.resource.tests.CustomResource object at ...>
>>> os.unlink(fn)

z3ext:resourceDirectory Directive

We need some temporary directories and files

>>> import tempfile
>>> dn = tempfile.mkdtemp()
>>> os.mkdir(os.path.join(dn, 'subfolder'))
>>> open(os.path.join(dn, 'resource1.css'), 'w').write('''\
... /* zrt-cssregistry: */
... h1 {
...   color: fontColor;
...   font: fontFamily;
...   background: url('../img1/mybackground.gif');
... }''')
>>> open(os.path.join(dn, 'resource2.js'), 'w').write('test')
>>> open(os.path.join(dn, 'resource3.css'), 'w').write('test')
>>> open(os.path.join(dn, 'resource4.jpg'), 'w').write('test')
>>> open(os.path.join(dn, 'resource5.png'), 'w').write('test')

Directive require directory path

>>> context = xmlconfig.string('''
... <configure xmlns:z3ext="http://namespaces.zope.org/z3ext">
...   <z3ext:resourcedirectory
...       name="myresources"
...       directory="%s" />
... </configure>
... ''' %(dn+'123123234534234'), context=context)
Traceback (most recent call last):
...
ZopeXMLConfigurationError: ...

Now we can register a directory of resources, also we can set resource types:

>>> context = xmlconfig.string('''
... <configure xmlns:z3ext="http://namespaces.zope.org/z3ext">
...   <z3ext:resourcedirectory
...       name="myresources"
...       directory="%s"
...         mapping=".css:zrt .js:fileresource
...                resource3.css:cutom .png:null" />
... </configure>
... ''' %dn, context=context)
>>> dirresource = component.getAdapter(
...    request, interface.Interface, name='myresources')

Now we can get resource

>>> dirresource.browserDefault(request)
(<function empty at ...>, ())
>>> resource = dirresource.publishTraverse(request, 'resource1.css')
>>> print resource.GET()
h1 {
color: #11111111;
font: Verdana;
background: url('../img1/mybackground.gif');
}
>>> print dirresource['resource1.css'].GET()
h1 {
color: #11111111;
font: Verdana;
background: url('../img1/mybackground.gif');
}
>>> dirresource.publishTraverse(request, 'unknown.css')
Traceback (most recent call last):
...
NotFound: ...
>>> dirresource['unknown.css']
Traceback (most recent call last):
...
KeyError: 'unknown.css'
Types mapping

In 'mapping' we defined that all files with '.css' extension should be custom type, '.js' should be file resource and filename 'test.css' should be file resource, '.png' should be not available

>>> dirresource.publishTraverse(request, 'resource1.css')
<z3ext.resource.zrtresource.zrtresource.ZRTFileResource ...>
>>> dirresource.publishTraverse(request, 'resource2.js')
<z3ext.resource.fileresource.FileResource object at ...>
>>> dirresource.publishTraverse(request, 'resource3.css')
<z3ext.resource.fileresource.FileResource object at ...>
>>> dirresource.publishTraverse(request, 'resource4.jpg')
<z3ext.resource.fileresource.FileResource object at ...>
>>> dirresource.publishTraverse(request, 'resource5.png')
Traceback (most recent call last):
...
NotFound: Object: ...

Or we can use 'resourceType' subdirective:

>>> context = xmlconfig.string('''
... <configure xmlns:z3ext="http://namespaces.zope.org/z3ext">
...   <z3ext:resourcedirectory
...       name="myresources2"
...       directory="%s">
...     <resourceType file=".css" type="zrt" />
...     <resourceType file=".js" type="fileresource" />
...     <resourceType file="resource3.css" type="custom" />
...     <resourceType file=".png" type="null" />
...   </z3ext:resourcedirectory>
... </configure>
... ''' %dn, context=context)
>>> dirresource = component.getAdapter(
...    request, interface.Interface, name='myresources2')
>>> dirresource.publishTraverse(request, 'resource1.css')
<z3ext.resource.zrtresource.zrtresource.ZRTFileResource ...>
>>> dirresource.publishTraverse(request, 'resource2.js')
<z3ext.resource.fileresource.FileResource object at ...>
>>> dirresource.publishTraverse(request, 'resource3.css')
<z3ext.resource.tests.CustomResource object at ...>
>>> dirresource.publishTraverse(request, 'resource4.jpg')
<z3ext.resource.fileresource.FileResource object at ...>
>>> dirresource.publishTraverse(request, 'resource5.png')
Traceback (most recent call last):
...
NotFound: Object: ...

We can get sub directories

>>> subdir = dirresource.publishTraverse(request, 'subfolder')
>>> os.unlink(os.path.join(dn, 'resource1.css'))
>>> os.unlink(os.path.join(dn, 'resource2.js'))
>>> os.unlink(os.path.join(dn, 'resource3.css'))
>>> os.unlink(os.path.join(dn, 'resource4.jpg'))
>>> os.unlink(os.path.join(dn, 'resource5.png'))
>>> os.rmdir(os.path.join(dn, 'subfolder'))
>>> os.rmdir(dn)

CHANGES

1.3.0 (2009-08-28)
  • Fixed zrt resource, always return rendered content
  • Copyright holder changed
1.2.2 (2009-04-15)
  • Do not use z3c.autoinclude
1.2.1 (2008-11-25)
  • Added z3ext.cssregistry as dependency
1.2.0 (2008-10-06)
  • Update deoendencies
  • Removed 'stylesheet' resource type because it's useless
1.1.0 (2008-03-28)
  • Added 'stylesheet' resource type
  • Code moved to svn.zope.org
1.0.0 (2007-12-14)
  • Initial release.

Subscribe to package updates

Last updated Jan 5th, 2011

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.