Back to index

plone3  3.1.7
meta.py
Go to the documentation of this file.
00001 
00002 import os
00003 from zope.security.checker import CheckerPublic, NamesChecker
00004 from zope.configuration.exceptions import ConfigurationError
00005 from zope.publisher.interfaces.browser import IBrowserRequest
00006 from zope.app.component.metaconfigure import handler
00007 from zope.interface import Interface
00008 from resource import ConcatResourceFactory
00009 
00010 try:
00011     import Products.Five
00012 except ImportError:
00013     __five__ = False
00014 else:
00015     __five__ = True
00016     from Products.Five.metaclass import makeClass
00017     from Products.Five.security import getSecurityInfo, protectClass, protectName,\
00018         initializeClass
00019 
00020 try:
00021     from zope.publisher.interfaces.browser import IDefaultBrowserLayer
00022     _layer = IDefaultBrowserLayer
00023     __pre_3_2__ = False
00024 except ImportError:
00025     from zope.app.servicenames import Presentation
00026     _layer = 'default'
00027     __pre_3_2__ = True
00028 
00029 try:
00030     from zope.component import zcml
00031     __pre_3_3__ = False
00032 except:
00033     __pre_3_3__ = True
00034 
00035 # z3 only
00036 allowed_names = ('GET', 'HEAD', 'publishTraverse', 'browserDefault',
00037                  'request', '__call__')
00038 
00039 # We keep this in order to allow an occasional merge to browser:resource
00040 _factory_map = {
00041     'files': {
00042         'prefix': 'ConcatResource',
00043         'count': 0,
00044         'factory': ConcatResourceFactory
00045         },
00046     }
00047 
00048 def concatresource(_context, name, files=None, compress_level='safe',
00049         caching='default', lmt_check_period=60.0,
00050         layer=_layer, permission='zope.Public'):
00051 
00052     if not files:
00053         raise ConfigurationError(
00054             "Must use a files"
00055             " attribute for concatresource directives, with at least"
00056             " one file contained."
00057             )
00058 
00059     res = files
00060     res_type = 'files'
00061     factory_info = _factory_map.get(res_type)
00062     factory_info['count'] += 1
00063     res_factory = factory_info['factory']
00064 
00065     if __five__:
00066         checker = None
00067         _class_name = '%s%s' % (factory_info['prefix'], factory_info['count'])
00068         new_class = makeClass(_class_name, (res_factory.resource,), {})
00069 
00070         _context.action(
00071             discriminator = ('five:protectClass', new_class),
00072             callable = protectClass,
00073             args = (new_class, permission)
00074             )
00075         _context.action(
00076             discriminator = ('five:initialize:class', new_class),
00077             callable = initializeClass,
00078             args = (new_class,)
00079             )
00080 
00081     else:
00082         new_class = res_factory.resource
00083 
00084         if permission == 'zope.Public':
00085             permission = CheckerPublic
00086 
00087         checker = NamesChecker(allowed_names, permission)
00088 
00089     factory = res_factory(res, name, compress_level, caching, lmt_check_period,
00090             resource_factory=new_class, checker=checker)
00091 
00092     if __pre_3_2__:
00093         _context.action(
00094             discriminator = ('resource', name, IBrowserRequest, layer),
00095             callable = handler,
00096             args = (Presentation, 'provideResource',
00097                     name, IBrowserRequest, factory, layer),
00098             )
00099     else:
00100         if __pre_3_3__:
00101             _context.action(
00102                 discriminator = ('resource', name, IBrowserRequest, layer),
00103                 callable = handler,
00104                 args = ('provideAdapter',
00105                     (layer,), Interface, name, factory, _context.info),
00106                 )
00107         else:
00108             _context.action(
00109                 discriminator = ('resource', name, IBrowserRequest, layer),
00110                 callable = handler,
00111                 args = ('registerAdapter',
00112                     factory, (layer,), Interface, name, _context.info),
00113                 )