Exemple #1
0
def register_settings(name, *fields, **kw):
    """ register settings group """
    iname = name
    for ch in ('.', '-'):
        iname = iname.replace(ch, '_')

    category = InterfaceClass('SettingsGroup:%s' % iname.upper(), (),
                              __doc__='Settings group: %s' % name,
                              __module__='ptah.config.settings')

    for field in fields:
        field.required = False
        if field.default is form.null:
            raise StopException(
                'Default value is required for "{0}.{1}"'.format(
                    name, field.name))

    group = Group(name=name, *fields, **kw)
    interface.directlyProvides(Group, category)

    info = config.DirectiveInfo()
    discr = (ID_SETTINGS_GROUP, name)
    intr = config.Introspectable(ID_SETTINGS_GROUP, discr, group.__title__,
                                 ID_SETTINGS_GROUP)
    intr['name'] = name
    intr['group'] = group
    intr['codeinfo'] = info.codeinfo

    info.attach(
        config.Action(
            lambda config, group: config.get_cfg_storage(ID_SETTINGS_GROUP)\
                .update({group.__name__: group.clone(config.registry)}),
            (group,), discriminator=discr, introspectables=(intr,))
        )
Exemple #2
0
def auth_checker(checker, __cfg=None, __depth=1):
    """Register authentication checker. Checker function accepts
    :py:class:`ptah.authentication.AuthInfo` object.

    :param checker: Checker function.

    Checker function interface :py:func:`ptah.interfaces.auth_checker`

    .. code-block:: python

      @ptah.auth_checker
      def my_checker(info):
          ...

    """
    info = config.DirectiveInfo(__depth)
    discr = (AUTH_CHECKER_ID, hash(checker))
    intr = config.Introspectable(AUTH_CHECKER_ID, discr, checker.__name__,
                                 'ptah-authchecker')
    intr['name'] = '{0}.{1}'.format(info.codeinfo.module, checker.__name__)
    intr['callable'] = checker
    intr['codeinfo'] = info.codeinfo

    info.attach(
        config.Action(
            lambda config, checker: config.get_cfg_storage(AUTH_CHECKER_ID)\
                .update({id(checker): checker}),
            (checker,), discriminator=discr, introspectables=(intr,)),
        __cfg)
    return checker
Exemple #3
0
def library(name,
            path='',
            type='',
            require='',
            prefix='',
            postfix='',
            extra=None):
    """
    Registers a library with one or more assets.  Used to logically group
    JS and CSS collections.  Provides ability to declarare
    dependency/requirements between library collections.

    :param name: Library collection name
    :param path: The ``path`` argument points at a file or directory on disk.
    :param type:  A string, either `css` or `js`
    :param require: A library name to be considered dependency (Optional)
    :param prefix: A string which will be generated before the library HTML
    :param postfix: A string which will be generated after the library HTML
                    An example of prefix/postfix is <!-- JS/CSS -->
    :param extra: Additional attributes for computed library tag
                  An example of extra is {'type':'text/pythonscript'}
    """
    if type not in ('js', 'css'):
        raise ValueError("Uknown type '%s'" % type)

    if isinstance(path, string_types):
        path = (path, )

    if require and isinstance(require, string_types):
        require = (require, )

    if extra is None:
        extra = {}

    discr = (LIBRARY_ID, name, tuple(path))
    intr = config.Introspectable(LIBRARY_ID, discr, name, LIBRARY_ID)
    intr['name'] = name
    intr['path'] = path
    intr['type'] = type
    intr['require'] = require
    intr['prefix'] = prefix
    intr['postfix'] = postfix
    intr['extra'] = extra

    def library_impl(cfg, name, path, type, require, prefix, postfix, extra):
        data = cfg.get_cfg_storage(LIBRARY_ID)

        lib = data.get(name)
        if lib is None:
            lib = Library(name)
            data[name] = lib

        lib.add(path, type, require, prefix, postfix, extra)

    info = config.DirectiveInfo()
    info.attach(
        config.Action(library_impl,
                      (name, path, type, require, prefix, postfix, extra),
                      discriminator=discr,
                      introspectables=(intr, )))
Exemple #4
0
def register_migration(pkg, path, title='', force=False):
    """Registers a migration for package.
    Check :ref:`data_migration_chapter` chapter for detailed description.

    :param pkg: Package name
    :param path: String implying a path or `asset specification`
        (e.g. ``ptah:migrations``). Path to directory with migration scripts.
    :param title: Optional human readable title.
    :param force: Force execute migration during bootstrap process

    .. code-block:: python

      import ptah

      ptah.register_migration(
          'ptah', 'ptah:migrations', 'Ptah database migration')

    """
    info = config.DirectiveInfo()
    discr = (MIGRATION_ID, pkg)

    intr = config.Introspectable(MIGRATION_ID, discr, pkg, MIGRATION_ID)
    intr['package'] = pkg
    intr['path'] = path
    intr['title'] = title
    intr['force'] = force

    def _complete(cfg, pkg, path):
        cfg.get_cfg_storage(MIGRATION_ID)[pkg] = path

    info.attach(
        config.Action(_complete, (pkg, path),
                      discriminator=discr,
                      introspectables=(intr, )))
Exemple #5
0
def password_changer(schema):
    """ decorator for password changer registration::

        @ptah.password_change('myuser')
        def change_password(user):
            ...
    """
    info = config.DirectiveInfo()

    def wrapper(changer):
        discr = (ID_PASSWORD_CHANGER, schema)

        intr = config.Introspectable(ID_PASSWORD_CHANGER, discr,
                                     changer.__doc__, ID_PASSWORD_CHANGER)
        intr.update(schema=schema, callable=changer)

        info.attach(
            config.Action(
                lambda config, schema, changer: \
                    config.get_cfg_storage(ID_PASSWORD_CHANGER).update(
                            {schema: changer}),
                (schema, changer),
                discriminator=discr, introspectables = (intr,))
            )
        return changer

    return wrapper
Exemple #6
0
    def __init__(self,
                 name,
                 title,
                 description='',
                 prefix='role:',
                 system=False):
        id = '%s%s' % (prefix, name)

        self.id = id
        self.name = name
        self.title = title
        self.description = description
        self.system = system

        self.allowed = set()
        self.denied = set()

        # conflict detection and introspection
        info = config.DirectiveInfo()

        discr = (ID_ROLE, name)
        intr = config.Introspectable(ID_ROLE, discr, title, 'ptah-role')
        intr['role'] = self
        intr['codeinfo'] = info.codeinfo

        info.attach(
            config.Action(
                lambda config, r: \
                    config.get_cfg_storage(ID_ROLE).update({r.name: r}),
                (self, ), discriminator=discr, introspectables=(intr,))
            )
Exemple #7
0
def auth_provider(name):
    """ decorator for authentication provider registration::

       @ptah.auth_provider('my-provider')
       class AuthProvider(object):
           ...
    """
    info = config.DirectiveInfo()

    def wrapper(cls):
        discr = (AUTH_PROVIDER_ID, name)
        intr = config.Introspectable(
            AUTH_PROVIDER_ID, discr, name, AUTH_PROVIDER_ID)
        intr['id'] = name
        intr['name'] = '{0}.{1}'.format(info.codeinfo.module, cls.__name__)
        intr['provider'] = cls
        intr['codeinfo'] = info.codeinfo

        info.attach(
            config.Action(
                lambda config, n, p: config.get_cfg_storage(AUTH_PROVIDER_ID)\
                    .update({n: cls()}),
                (name, cls), discriminator=discr, introspectables=(intr,))
            )
        return cls

    return wrapper
Exemple #8
0
def principal_searcher(name):
    """ decorator for principal searcher registration::

        @ptah.principal_searcher('test')
        def searcher(term):
           ...

        searcher function recives text as term variable, and
        should return iterator to principal objects.
     """
    info = config.DirectiveInfo()

    def wrapper(searcher):
        discr = (AUTH_SEARCHER_ID, name)
        intr = config.Introspectable(
            AUTH_SEARCHER_ID, discr, name, AUTH_SEARCHER_ID)
        intr['name'] = name
        intr['callable'] = searcher

        info.attach(
            config.Action(
                lambda config, name, searcher:
                    config.get_cfg_storage(AUTH_SEARCHER_ID)\
                        .update({name: searcher}),
                (name, searcher), discriminator=discr, introspectables=(intr,))
            )

        return searcher

    return wrapper
Exemple #9
0
    def __init__(self, schema, __depth=1):
        self.info = config.DirectiveInfo(__depth)
        self.discr = (ID_PASSWORD_CHANGER, schema)

        self.intr = config.Introspectable(ID_PASSWORD_CHANGER, self.discr,
                                          schema, ID_PASSWORD_CHANGER)
        self.intr['schema'] = schema
Exemple #10
0
def register_uri_resolver(schema, resolver, depth=1):
    """ Register resolver for given schema

    :param schema: uri schema
    :type schema: string
    :param resolver: Callable object that accept one parameter.

    Resolver interface::

      class Resolver(object):

         def __call__(self, uri):
             return content
    """
    discr = (ID_RESOLVER, schema)
    info = config.DirectiveInfo(depth=depth)

    intr = config.Introspectable(ID_RESOLVER, discr, resolver.__doc__,
                                 ID_RESOLVER)
    intr['schema'] = schema
    intr['callable'] = resolver
    intr['codeinfo'] = info.codeinfo

    info.attach(
        config.Action(_register_uri_resolver, (schema, resolver),
                      discriminator=discr,
                      introspectables=(intr, )))
Exemple #11
0
    def __init__(self, name, __depth=1):
        self.depth = __depth
        self.info = config.DirectiveInfo(__depth)

        self.discr = (AUTH_SEARCHER_ID, name)
        self.intr = config.Introspectable(AUTH_SEARCHER_ID, self.discr, name,
                                          AUTH_SEARCHER_ID)
        self.intr['name'] = name
Exemple #12
0
    def __init__(self, name, __depth=1):
        self.depth = __depth
        self.info = config.DirectiveInfo(__depth)

        self.discr = (AUTH_PROVIDER_ID, name)
        self.intr = config.Introspectable(AUTH_PROVIDER_ID, self.discr, name,
                                          'ptah-authprovider')
        self.intr['id'] = name
        self.intr['codeinfo'] = self.info.codeinfo
Exemple #13
0
    def __init__(self, schema, __depth=1):
        self.depth = __depth
        self.info = config.DirectiveInfo(__depth)
        self.discr = (ID_RESOLVER, schema)

        self.intr = config.Introspectable(
            ID_RESOLVER, self.discr, schema, 'ptah-uriresolver')
        self.intr['schema'] = schema
        self.intr['codeinfo'] = self.info.codeinfo
Exemple #14
0
def Type(name, title=None, fieldset=None, **kw):
    """ Declare new type. This function has to be called within a content
    class declaration.

    .. code-block:: python

        class MyContent(ptahcms.Content):

            __type__ = Type('My content')

    """
    info = config.DirectiveInfo(allowed_scope=('class', ))

    kw['title'] = name.capitalize() if title is None else title
    fs = ContentSchema if fieldset is None else fieldset

    typeinfo = TypeInformation(None, name, fieldset=fs, **kw)

    f_locals = sys._getframe(1).f_locals
    if '__mapper_args__' not in f_locals:
        f_locals['__mapper_args__'] = {
            'polymorphic_identity': typeinfo.__uri__
        }

    if '__id__' not in f_locals and '__tablename__' in f_locals:
        f_locals['__id__'] = sqla.Column(
            'id',
            sqla.Integer,
            sqla.ForeignKey('ptahcms_contents.id'),
            primary_key=True)

    if '__uri_factory__' not in f_locals:
        schema = 'type-{0}'.format(name)
        typeinfo.schema = schema

        f_locals['__uri_factory__'] = ptah.UriFactory(schema)

        def resolve_content(uri):
            return typeinfo.cls.__uri_sql_get__.first(uri=uri)

        resolve_content.__doc__ = 'CMS Content resolver for %s type' % name

        ptah.resolver(schema, 2)(resolve_content)

    # config actino and introspection info
    discr = (TYPES_DIR_ID, name)
    intr = config.Introspectable(TYPES_DIR_ID, discr, name, TYPES_DIR_ID)
    intr['name'] = name
    intr['type'] = typeinfo
    intr['codeinfo'] = info.codeinfo

    info.attach(ClassAction(register_type_impl, (typeinfo, name, fieldset),
                            kw,
                            discriminator=discr,
                            introspectables=(intr, )),
                depth=2)
    return typeinfo
Exemple #15
0
    def __init__(self, name, __depth=1):
        self.info = config.DirectiveInfo(__depth)
        self.discr = (ID_ROLES_PROVIDER, name)

        self.intr = intr = config.Introspectable(ID_ROLES_PROVIDER, self.discr,
                                                 name, ID_ROLES_PROVIDER)

        intr['name'] = name
        intr['codeinfo'] = self.info.codeinfo
Exemple #16
0
    def __init__(self, name, title='', active=True, requires=(), __depth=1):
        self.info = config.DirectiveInfo(__depth)
        self.discr = (POPULATE_ID, name)

        self.intr = intr = config.Introspectable(POPULATE_ID, self.discr, name,
                                                 POPULATE_ID)

        intr['name'] = name
        intr['title'] = title
        intr['active'] = active
        intr['requires'] = requires
        intr['codeinfo'] = self.info.codeinfo
Exemple #17
0
    def test_action(self):
        info = config.DirectiveInfo()

        action = config.Action(None, discriminator=('test', ))

        self.assertIsNone(action.hash)

        info.attach(action)

        self.assertIsNotNone(action.hash)
        self.assertIsNotNone(hash(action))
        self.assertRaises(TypeError, info.attach, action)
        self.assertEqual(repr(action), '<Action "test">')
Exemple #18
0
def register_principal_searcher(name, searcher):
    """ register principal searcher """
    discr = (AUTH_SEARCHER_ID, name)
    intr = config.Introspectable(AUTH_SEARCHER_ID, discr, name, AUTH_SEARCHER_ID)
    intr['name'] = name
    intr['callable'] = searcher

    info = config.DirectiveInfo()
    info.attach(
        config.Action(
            lambda config, name, searcher:
               config.get_cfg_storage(AUTH_SEARCHER_ID).update({name:searcher}),
            (name, searcher), discriminator=discr, introspectables=(intr,))
        )
Exemple #19
0
def auth_checker(checker):
    """ register authentication checker::

        @ptah.auth_checker
        def my_checker(info):
            ...

    """
    info = config.DirectiveInfo()
    discr = (AUTH_CHECKER_ID, hash(checker))
    intr = config.Introspectable(
        AUTH_CHECKER_ID, discr, checker.__name__, AUTH_CHECKER_ID)
    intr['name'] = '{0}.{1}'.format(info.codeinfo.module, checker.__name__)
    intr['callable'] = checker
    intr['codeinfo'] = info.codeinfo

    info = config.DirectiveInfo()
    info.attach(
        config.Action(
            lambda config, checker: config.get_cfg_storage(AUTH_CHECKER_ID)\
                .update({id(checker): checker}),
            (checker,), discriminator=discr, introspectables=(intr,))
        )
    return checker
Exemple #20
0
def register_field_factory(cls, name, layer=''):
    info = config.DirectiveInfo()
    discr = (FIELD_ID, name, layer)

    intr = config.Introspectable(FIELD_ID, discr, name, FIELD_ID)
    intr['name'] = name
    intr['layer'] = layer
    intr['field'] = cls

    info.attach(
        config.Action(config.LayerWrapper(register_field_impl, discr), (
            cls,
            name,
        ),
                      discriminator=discr,
                      introspectables=(intr, )))
Exemple #21
0
def fieldpreview(cls):
    info = config.DirectiveInfo()

    def wrapper(func):
        discr = (PREVIEW_ID, cls)
        intr = config.Introspectable(PREVIEW_ID, discr, '', PREVIEW_ID)
        intr['field'] = cls
        intr['preview'] = func

        info.attach(
            config.Action(lambda config, cls, func: config.get_cfg_storage(
                PREVIEW_ID).update({cls: func}), (cls, func),
                          discriminator=discr,
                          introspectables=(intr, )))
        return func

    return wrapper
Exemple #22
0
def uiaction(context,
             id,
             title,
             description='',
             action='',
             condition=None,
             permission=None,
             category='',
             sort_weight=1.0,
             **kw):
    """ Register ui action """

    kwargs = {
        'id': id,
        'title': title,
        'description': description,
        'category': category,
        'condition': condition,
        'permission': permission,
        'sort_weight': sort_weight,
        'data': kw
    }

    if callable(action):
        kwargs['action_factory'] = action
    else:
        kwargs['action'] = action

    ac = Action(**kwargs)

    info = config.DirectiveInfo()
    discr = (ID_UIACTION, id, context, category)
    intr = ptah.config.Introspectable(ID_UIACTION, discr, title,
                                      'ptah-uiaction')
    intr['action'] = ac
    intr['codeinfo'] = info.codeinfo

    info.attach(
        config.Action(
            lambda cfg, id, category, context, ac: \
                cfg.registry.registerAdapter(\
                   ac, (context,), IAction, '%s-%s'%(category, id)),
            (id, category, context, ac,),
            discriminator = discr, introspectables = (intr,))
        )
Exemple #23
0
def register_snippet(name, context=None, view=None, renderer=None, layer=''):
    info = config.DirectiveInfo()
    discr = (SNIPPET_ID, name, context, layer)

    intr = config.Introspectable(SNIPPET_ID, discr, name, SNIPPET_ID)
    intr['name'] = name
    intr['view'] = view
    intr['context'] = context
    intr['renderer'] = renderer
    intr['layer'] = layer
    intr['module'] = info.module.__name__
    intr['codeinfo'] = info.codeinfo

    info.attach(
        config.Action(config.LayerWrapper(register_snippet_impl, discr),
                      (view, name, context, renderer),
                      discriminator=discr,
                      introspectables=(intr, )))
Exemple #24
0
    def __init__(self, id, title, description=''):
        self.id = id
        self.title = title
        self.description = description

        info = config.DirectiveInfo()
        discr = (ID_ACL, id)
        intr = config.Introspectable(ID_ACL, discr, title, 'ptah-aclmap')
        intr['acl'] = self
        intr['codeinfo'] = info.codeinfo

        info.attach(
            config.Action(
                lambda config, p: \
                    config.get_cfg_storage(ID_ACL).update({id: p}),
                (self,), discriminator=discr, introspectables=(intr,))
            )
        self.directiveInfo = info
Exemple #25
0
    def __init__(self, id, timeout, title='', description=''):
        self.id = id
        self.timeout = timeout
        self.title = title
        self.description = description

        info = config.DirectiveInfo()
        discr = (ID_TOKEN_TYPE, id)
        intr = config.Introspectable(ID_TOKEN_TYPE, discr, title,
                                     ID_TOKEN_TYPE)
        intr['ttype'] = self
        intr['codeinfo'] = info.codeinfo

        info.attach(
            config.Action(
                lambda config, id, tp: \
                    config.get_cfg_storage(ID_TOKEN_TYPE).update({id: tp}),
                (id, self), discriminator=discr, introspectables=(intr,))
            )
Exemple #26
0
def action(func=None, name=None, permission = View):
    info = config.DirectiveInfo(allowed_scope=('class',))

    actions = info.locals.get('__ptahcms_actions__', None)
    if actions is None:
        actions = {}
        info.locals['__ptahcms_actions__'] = actions

    def wrapper(func, name=name):
        if name is None:
            name = func.__name__

        actions[name] = (func.__name__, permission)

        return func

    if func is not None:
        return wrapper(func)
    return wrapper
Exemple #27
0
def RestService(name, title, description=''):
    srv = Service(name, title, description)

    apidoc = ServiceAPIDoc(name)
    srv.actions['apidoc'] = Action(apidoc, 'apidoc', apidoc.title)

    def _register(cfg, srv):
        cfg.get_cfg_storage(ID_REST)[name] = srv

    discr = (ID_REST, name)
    intr = config.Introspectable(ID_REST, discr, name, ID_REST)
    intr['service'] = srv

    info = config.DirectiveInfo()
    info.attach(
        config.Action(
            _register, (srv,), discriminator=discr, introspectables=(intr,))
        )

    return srv
Exemple #28
0
def formatter(name):
    info = config.DirectiveInfo()

    def wrapper(func):
        discr = (ID_FORMATTER, name)

        intr = config.Introspectable(ID_FORMATTER, discr, name, ID_FORMATTER)

        intr['name'] = name
        intr['callable'] = func
        intr['description'] = func.__doc__
        intr['codeinfo'] = info.codeinfo

        info.attach(
            config.Action(lambda config, name, func: config.get_cfg_storage(
                ID_FORMATTER).update({name: func}), (name, func),
                          discriminator=discr,
                          introspectables=(intr, )))
        return func

    return wrapper
Exemple #29
0
def module(name):
    info = config.DirectiveInfo()

    def wrapper(cls):
        discr = (MANAGE_ID, name)
        intr = config.Introspectable(MANAGE_ID, discr, cls.title, MANAGE_ID)
        intr['name'] = name
        intr['factory'] = cls
        intr['codeinfo'] = info.codeinfo

        def _complete(cfg, cls, name):
            cls.name = name
            cfg.get_cfg_storage(MANAGE_ID)[name] = cls

        info.attach(
            config.Action(_complete, (cls, name),
                          discriminator=discr,
                          introspectables=(intr, )))
        return cls

    return wrapper
Exemple #30
0
def field(name, layer=''):
    info = config.DirectiveInfo()
    discr = (FIELD_ID, name, layer)

    def wrapper(cls):
        intr = config.Introspectable(FIELD_ID, discr, name, FIELD_ID)
        intr['name'] = name
        intr['layer'] = layer
        intr['field'] = cls
        intr['codeinfo'] = info.codeinfo

        info.attach(
            config.Action(config.LayerWrapper(register_field_impl, discr), (
                cls,
                name,
            ),
                          discriminator=discr,
                          introspectables=(intr, )))
        return cls

    return wrapper