Beispiel #1
0
def add_layer(cfg, layer, name='', path='', description=''):
    """ add new layer

    :param layer: layer id
    :param name: name
    :param path: asset path
    :param description: module description
    """
    if not path:
        raise ConfigurationError('Path is required')

    discr = (ID_LAYER, name, layer)

    resolver = AssetResolver()
    directory = resolver.resolve(path).abspath()

    intr = Introspectable(ID_LAYER, discr, name, ID_LAYER)
    intr['name'] = name
    intr['layer'] = layer
    intr['path'] = directory
    intr['asset'] = path
    intr['description'] = description
    intr['filters'] = {}

    storage = cfg.registry.setdefault(ID_LAYER, {})
    layers = storage.setdefault(layer, [])
    layers.insert(0, intr)

    cfg.action(discr, introspectables=(intr, ))
    log.info("Add layer: %s path:%s" % (layer, path))
Beispiel #2
0
def register_less_bundle(cfg, name, path, description='', watchdir=None):
    """ Register less bundle;

    :param name: module name
    :param path: asset path
    :param description:
    """
    resolver = AssetResolver()
    abs_path = resolver.resolve(path).abspath()

    if not os.path.isfile(abs_path):
        raise ConfigurationError("less file is required: %s" % path)

    discr = (ID_BUNDLE, name)

    intr = Introspectable(ID_BUNDLE, discr, name, ID_BUNDLE)
    intr['name'] = name
    intr['path'] = path
    intr['abs_path'] = abs_path
    intr['watchdir'] = watchdir or os.path.dirname(abs_path)
    intr['description'] = description

    storage = cfg.registry.setdefault(ID_BUNDLE, {})
    storage[name] = intr

    cfg.action(discr, introspectables=(intr, ))
Beispiel #3
0
def register_handlebars_bundle(cfg,
                               name,
                               path='',
                               description='',
                               i18n_domain=''):
    """ Register handlebars bundle;

    :param name: module name
    :param path: asset path
    :param description:
    :param i18n_domain: i18n domain

    """
    resolver = AssetResolver()
    abs_path = resolver.resolve(path).abspath()

    if not path or not os.path.isdir(abs_path):
        raise ConfigurationError("Directory is required: %s" % path)

    discr = (ID_AMD_MODULE, name)

    intr = Introspectable(ID_AMD_MODULE, discr, name, ID_AMD_MODULE)
    intr['name'] = name
    intr['path'] = path
    intr['abs_path'] = abs_path
    intr['description'] = description
    intr['i18n_domain'] = i18n_domain

    storage = cfg.registry.setdefault(ID_BUNDLE, {})
    storage[name] = intr

    cfg.action(discr, introspectables=(intr, ))
Beispiel #4
0
def add_js_module(cfg, name, path, description='', requires=()):
    """ register amd js module

    :param name: name
    :param path: asset path
    :param description: module description
    :param requires: module dependencies
    """
    discr = (ID_AMD_MODULE, name)

    intr = Introspectable(ID_AMD_MODULE, discr, name, ID_AMD_MODULE)
    intr['name'] = name
    intr['path'] = path
    intr['description'] = description
    intr['tp'] = JS_MOD

    filepath = RESOLVER.resolve(path).abspath()
    md5 = hashlib.md5()
    with open(filepath, 'rb') as f:
        md5.update(f.read())

    intr['md5'] = md5.hexdigest()

    if isinstance(requires, str):
        requires = (requires,)
    intr['requires'] = requires

    storage = cfg.registry.setdefault(ID_AMD_MODULE, {})
    storage[name] = intr

    cfg.action(discr, introspectables=(intr,))
    log.info("Add js module: %s path:%s" % (name, path))
Beispiel #5
0
def add_css_module(cfg, name, path, description=''):
    """ register css module

    :param name: name
    :param path: asset path
    :param description: module description
    """
    discr = (ID_AMD_MODULE, name)

    intr = Introspectable(ID_AMD_MODULE, discr, name, ID_AMD_MODULE)
    intr['name'] = name
    intr['path'] = path
    intr['description'] = description
    intr['tp'] = CSS_MOD

    filepath = RESOLVER.resolve(path).abspath()
    md5 = hashlib.md5()
    with open(filepath, 'rb') as f:
        md5.update(f.read())
    intr['md5'] = md5.hexdigest()

    storage = cfg.registry.setdefault(ID_AMD_MODULE, {})
    storage[name] = intr

    cfg.action(discr, introspectables=(intr,))
    log.info("Add css module: %s path:%s" % (name, path))
Beispiel #6
0
def add_field(cfg, name, cls):
    """ Field registration directive. Field should be inherited from
    :py:class:`ptah.form.Field` class.

    .. code-block:: python

      class TextField(ptah.form.Field):
          ...


      config = Configurator(...)
      config.include('ptah.form')

      config.provide_form_field('text', TextField)

    """
    discr = (ID_FIELD, name)

    intr = Introspectable(ID_FIELD, discr, name, 'ptah.form-field')
    intr['name'] = name
    intr['field'] = cls

    cls.__field__ = name

    def action():
        storage = cfg.registry.get(ID_FIELD)
        if storage is None:
            storage = cfg.registry[ID_FIELD] = {}

        storage[name] = cls

    cfg.action(discr, action, introspectables=(intr, ))
Beispiel #7
0
def add_formatter(cfg, name, callable):
    discr = (ID_FORMATTER, name)

    intr = Introspectable(ID_FORMATTER, discr, name, 'ptah.renderer-formatter')
    intr['name'] = name
    intr['callable'] = callable
    intr['description'] = callable.__doc__

    def action():
        storage = cfg.registry.get(ID_FORMATTER)
        if storage is None:
            storage = cfg.registry[ID_FORMATTER] = {}

        storage[name] = callable

    cfg.action(discr, action, introspectables=(intr, ))
Beispiel #8
0
def add_fieldpreview(cfg, cls, callable):
    discr = (ID_PREVIEW, cls)

    intr = Introspectable(ID_PREVIEW, discr, 'Field preview for "%s"' % cls,
                          'ptah.form-preview')
    intr['field'] = cls
    intr['preview'] = callable

    def action():
        storage = cfg.registry.get(ID_PREVIEW)
        if storage is None:
            storage = cfg.registry[ID_PREVIEW] = {}

        storage[cls] = callable

    cfg.action(discr, action, introspectables=(intr, ))
Beispiel #9
0
    def wrapper(func):
        discr = (ID_ADAPTER, required, _getProvides(func), name)

        intr = Introspectable(ID_ADAPTER, discr, 'Adapter', ID_ADAPTER)
        intr['name'] = name
        intr['required'] = required
        intr['adapter'] = func
        intr['codeinfo'] = info.codeinfo

        def _register(cfg, name, func, required):
            cfg.registry.registerAdapter(func, required, name=name)

        info.attach(
            Action(_register, (name, func, required),
                   discriminator=discr,
                   introspectables=(intr, )))
        return func
Beispiel #10
0
    def wrapper(func):
        required = tuple(args)
        discr = (ID_SUBSCRIBER, func, required)

        intr = Introspectable(ID_SUBSCRIBER, discr, 'Subscriber',
                              ID_SUBSCRIBER)
        intr['required'] = required
        intr['handler'] = func
        intr['codeinfo'] = info.codeinfo

        def _register(cfg, func, required):
            cfg.registry.registerHandler(func, required)

        info.attach(
            Action(_register, (func, required),
                   discriminator=discr,
                   introspectables=(intr, )))
        return func
Beispiel #11
0
def add_layout(cfg,
               name='',
               context=None,
               root=None,
               parent=None,
               renderer=None,
               route_name=None,
               use_global_views=True,
               view=None):
    """Registers a layout.

    :param name: Layout name
    :param context: Specific context for this layout.
    :param root:  Root object
    :param parent: A parent layout. None means no parent layout.
    :param renderer: A pyramid renderer
    :param route_name: A pyramid route_name. Apply layout only for
        specific route
    :param use_global_views: Apply layout to all routes. even is route
        doesnt use use_global_views.
    :param view: View callable


    Simple example with one default layout and 'page' layout.

    .. code-block:: python

      class PageLayout(object):
           ...

      config.add_layout('page', parent='page', renderer='my_package:template/page.pt')


    To use layout with pyramid view use ``renderer=ptah.renderer.layout('my_pkg:template/page.pt')``

    Example:

    .. code-block:: python

      config.add_view('
          index.html',
          renderer = ptah.renderer.layout('...'))

    in this example '' layout is beeing used. You can specify specific layout
    name for pyramid view ``ptah.renderer.layout('page', 'layout name')``

    """
    #    (scope, module,
    #     f_locals, f_globals, codeinfo) = venusian.getFrameInfo(sys._getframe(2))

    #    codeinfo = CodeInfo(
    #        codeinfo[0], codeinfo[1], codeinfo[2], codeinfo[3], module.__name__)

    discr = (LAYOUT_ID, name, context, route_name)

    intr = Introspectable(LAYOUT_ID, discr, name, 'ptah.renderer_layout')

    intr['name'] = name
    intr['context'] = context
    intr['root'] = root
    intr['renderer'] = renderer
    intr['route_name'] = route_name
    intr['parent'] = parent
    intr['use_global_views'] = use_global_views
    intr['view'] = view
    #    intr['codeinfo'] = codeinfo

    if not parent:
        parent = None
    elif parent == '.':
        parent = ''

    if isinstance(renderer, string_types):
        renderer = RendererHelper(name=renderer, registry=cfg.registry)

    if context is None:
        context = Interface

    def register():
        request_iface = IRequest
        if route_name is not None:
            request_iface = cfg.registry.getUtility(IRouteRequest,
                                                    name=route_name)

        if use_global_views:
            request_iface = Interface

        mapper = getattr(view, '__view_mapper__', DefaultViewMapper)
        mapped_view = mapper()(view)

        info = LayoutInfo(name, parent, mapped_view, view, renderer, intr)
        cfg.registry.registerAdapter(info, (root, request_iface, context),
                                     ILayout, name)

    cfg.action(discr, register, introspectables=(intr, ))