def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) context.templates[kwargs['name']] = self self.name = kwargs['name'] self.order = kwargs.get('order', None) self.output = kwargs.get('output', None) self.groups = {} Group(self, **{'name': 'default', 'order': 9999999})
def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) context.templates[kwargs['name']] = self self.name = kwargs['name'] self.order = kwargs.get('order', None) self.output = kwargs.get('output', None) self.groups = {} Group(self, **{'name':'default', 'order': 9999999})
def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) self.name = kwargs['name'] self.order = kwargs.get('order', None) context.groups[kwargs['name']] = self self.single_options = {} self.options = {} self.exclude_options = {} self.excludes_options = {}
def __init__(self, context, content_type, kind=None): GroupingContextDecorator.__init__(self, context) self.context = context self.content_type = content_type if not kind is None: kind = kind.upper() self.kind = kind self.options = {} self.attribute_options = {}
def with_package(self, package): """ Return a new Configurator instance with the same registry as this configurator using the package supplied as the ``package`` argument to the new configurator. ``package`` may be an actual Python package object or a :term:`dotted Python name` representing a package.""" context = self._ctx if context is None: context = self._ctx = self._make_context(self.autocommit) context = GroupingContextDecorator(context) context.package = package return self.__class__.with_context(context)
def exclude(_context, file=None, package=None, files=None): """Exclude a zcml file This directive should be used before any ZML that includes configuration you want to exclude. """ if files: if file: raise ValueError("Must specify only one of file or files") elif not file: file = 'configure.zcml' context = GroupingContextDecorator(_context) if package is not None: context.package = package context.basepath = None if files: paths = glob(context.path(files)) paths = sorted(zip([path.lower() for path in paths], paths)) paths = [path for (l, path) in paths] else: paths = [context.path(file)] for path in paths: # processFile returns a boolean indicating if the file has been # processed or not, it *also* marks the file as having been processed, # here the side effect is used to keep the given file from being # processed in the future context.processFile(path)
def __init__(self, context, interface, member, entity, collection=None, collection_root_name=None, collection_title=None, repository=None, expose=True): GroupingContextDecorator.__init__(self, context) self.context = context self.interface = interface self.member = member self.entity = entity self.collection = collection self.collection_root_name = collection_root_name self.collection_title = collection_title self.repository = repository self.expose = expose self.representers = {}
def action(self, discriminator, callable=None, args=(), kw=None, order=0): """ Register an action which will be executed when :meth:`pyramid.config.Configurator.commit` is called (or executed immediately if ``autocommit`` is ``True``). .. warning:: This method is typically only used by :app:`Pyramid` framework extension authors, not by :app:`Pyramid` application developers. The ``discriminator`` uniquely identifies the action. It must be given, but it can be ``None``, to indicate that the action never conflicts. It must be a hashable value. The ``callable`` is a callable object which performs the action. It is optional. ``args`` and ``kw`` are tuple and dict objects respectively, which are passed to ``callable`` when this action is executed. ``order`` is a crude order control mechanism, only rarely used (has no effect when autocommit is ``True``). """ if kw is None: kw = {} context = self._ctx if context is None: autocommit = self.autocommit else: autocommit = context.autocommit if autocommit: if callable is not None: callable(*args, **kw) else: if context is None: # defer expensive creation of context context = self._ctx = self._make_context(self.autocommit) if not context.info: # Try to provide more accurate info for conflict reports by # wrapping the context in a decorator and attaching caller info # to it, unless the context already has info (if it already has # info, it's likely a context generated by a ZCML directive). context = GroupingContextDecorator(context) if self._ainfo: info = self._ainfo[0] else: info = '' context.info = info context.action(discriminator, callable, args, kw, order)
def load_zcml(self, spec='configure.zcml', lock=threading.Lock()): """ Load configuration from a :term:`ZCML` file into the current configuration state. The ``spec`` argument is an absolute filename, a relative filename, or a :term:`asset specification`, defaulting to ``configure.zcml`` (relative to the package of the method's caller).""" package_name, filename = self._split_spec(spec) if package_name is None: # absolute filename package = self.package else: __import__(package_name) package = sys.modules[package_name] registry = self.registry self.manager.push({'registry':registry, 'request':None}) context = self._ctx if context is None: context = self._ctx = self._make_context(self.autocommit) # To avoid breaking people's expectations of how ZCML works, we # cannot autocommit ZCML actions incrementally. If we commit actions # incrementally, configuration outcome will be controlled purely by # ZCML directive execution order, which isn't what anyone who uses # ZCML expects. So we don't autocommit each ZCML directive action # while parsing is happening, but we do make sure to pass # execute=self.autocommit to xmlconfig.file below, which will cause # the actions implied by the ZCML that was parsed to be committed # right away once parsing is finished if autocommit is True. context = GroupingContextDecorator(context) context.autocommit = False lock.acquire() try: context.package = package xmlconfig.file(filename, package, context=context, execute=self.autocommit) finally: lock.release() self.manager.pop() return registry
def include(_context, file=None, package=None, files=None): """Include a zcml file See examples in tests/text_xmlconfig.py """ if files: if file: raise ValueError("Must specify only one of file or files") elif not file: file = 'configure.zcml' # This is a tad tricky. We want to behave as a grouping directive. context = GroupingContextDecorator(_context) if package is not None: context.package = package context.basepath = None if files: paths = glob(context.path(files)) paths = sorted(zip([path.lower() for path in paths], paths)) paths = [path for (l, path) in paths] else: paths = [context.path(file)] for path in paths: if context.processFile(path): with openInOrPlain(path) as f: logger.debug("include %s" % f.name) context.basepath = os.path.dirname(path) context.includepath = _context.includepath + (f.name, ) _context.stack.append(GroupingStackItem(context)) processxmlfile(f, context) assert _context.stack[-1].context is context _context.stack.pop()
def __init__(self, context, name): GroupingContextDecorator.__init__(self, context) self.context = context self.name = name self.options = {}
def include(self, callable, route_prefix=None): """Include a configuration callables, to support imperative application extensibility. .. warning:: In versions of :app:`Pyramid` prior to 1.2, this function accepted ``*callables``, but this has been changed to support only a single callable. A configuration callable should be a callable that accepts a single argument named ``config``, which will be an instance of a :term:`Configurator` (be warned that it will not be the same configurator instance on which you call this method, however). The code which runs as the result of calling the callable should invoke methods on the configurator passed to it which add configuration state. The return value of a callable will be ignored. Values allowed to be presented via the ``callable`` argument to this method: any callable Python object or any :term:`dotted Python name` which resolves to a callable Python object. It may also be a Python :term:`module`, in which case, the module will be searched for a callable named ``includeme``, which will be treated as the configuration callable. For example, if the ``includeme`` function below lives in a module named ``myapp.myconfig``: .. code-block:: python :linenos: # myapp.myconfig module def my_view(request): from pyramid.response import Response return Response('OK') def includeme(config): config.add_view(my_view) You might cause it be included within your Pyramid application like so: .. code-block:: python :linenos: from pyramid.config import Configurator def main(global_config, **settings): config = Configurator() config.include('myapp.myconfig.includeme') Because the function is named ``includeme``, the function name can also be omitted from the dotted name reference: .. code-block:: python :linenos: from pyramid.config import Configurator def main(global_config, **settings): config = Configurator() config.include('myapp.myconfig') Included configuration statements will be overridden by local configuration statements if an included callable causes a configuration conflict by registering something with the same configuration parameters. If the ``route_prefix`` is supplied, it must be a string. Any calls to :meth:`pyramid.config.Configurator.add_route` within the included callable will have their pattern prefixed with the value of ``route_prefix``. This can be used to help mount a set of routes at a different location than the included callable's author intended while still maintaining the same route names. For example: .. code-block:: python :linenos: from pyramid.config import Configurator def included(config): config.add_route('show_users', '/show') def main(global_config, **settings): config = Configurator() config.include(included, route_prefix='/users') In the above configuration, the ``show_users`` route will have an effective route pattern of ``/users/show``, instead of ``/show`` because the ``route_prefix`` argument will be prepended to the pattern. The ``route_prefix`` parameter is new as of Pyramid 1.2. """ _context = self._ctx if _context is None: _context = self._ctx = self._make_context(self.autocommit) if self.route_prefix: old_prefix = self.route_prefix.rstrip('/') + '/' else: old_prefix = '' if route_prefix: route_prefix = old_prefix + route_prefix.lstrip('/') c = self.maybe_dotted(callable) module = inspect.getmodule(c) if module is c: c = getattr(module, 'includeme') spec = module.__name__ + ':' + c.__name__ sourcefile = inspect.getsourcefile(c) if _context.processSpec(spec): context = GroupingContextDecorator(_context) context.basepath = os.path.dirname(sourcefile) context.includepath = _context.includepath + (spec,) context.package = package_of(module) context.route_prefix = route_prefix config = self.__class__.with_context(context) c(config)
def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) self.name = kwargs['name'] self.templates = {} self.plugins = []
def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) self.prefix = kwargs['prefix'] self.type = kwargs.get('type', None) self.default = kwargs.get('default', None) self.context.options[self.prefix] = self
def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) self.name = kwargs['name'] self.type = kwargs.get('type', None) self.default = kwargs.get('default', None) self.context.single_options[self.name] = self
def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) self.prefix = kwargs['prefix'] self.context.groups['default'].excludes_options[self.prefix] = self
def __init__(self, context, content_type=None, representer_class=None): GroupingContextDecorator.__init__(self, context) self.context = context self.content_type = content_type self.representer_class = representer_class self.options = {}
def __init__(self, context, *args, **kwargs): GroupingContextDecorator.__init__(self, context, *args, **kwargs) self.name = kwargs['name'] self.type = kwargs.get('type', '') self.context.groups['default'].exclude_options[self.name] = self