Beispiel #1
0
    def update(self):
        ev = self.request.params.get('ev')

        all_events = config.registry.storage[directives.EVENT_ID]
        self.event = event = all_events.get(ev)

        if event is None:
            events = []
            for n, ev in all_events.items():
                if isinstance(n, basestring):
                    events.append((ev.title, ev))

            events.sort()
            self.events = [ev for _t, ev in events]
        else:
            pkgs = list_packages()
            evinst = event.instance

            seen = set()
            actions = []
            for pkg in pkgs:
                for action in directives.scan(pkg, seen, exclude):
                    if action.discriminator[0] == 'ptah.config:subscriber':
                        required = action.args[2]
                        if len(required) == 2 and required[1] == evinst:
                            actions.append(action)
                        elif required[0] == evinst:
                            actions.append(action)

            self.actions = actions
Beispiel #2
0
    def test_introspect_view_introspect(self):
        from ptah.config import directives
        from ptah.manage.introspect import ViewDirective

        ptah.view.register_route("test-introspect", "/test/introspect")

        global view1, view2, view3

        @ptah.view.pview(route="test-introspect")
        def view1(request):
            """ """

        @ptah.view.pview("view2.html", route="test-introspect")
        def view2(request):
            """ """

        class view3(ptah.view.View):
            ptah.view.pview("view3.html")

        data = directives.scan(self.__class__.__module__, set())

        actions = []
        for action in data:
            if action.discriminator[0] == "ptah.view:view":
                actions.append(action)

        ti = ViewDirective(self.request)
        res = ti.renderActions(*actions)

        self.assertIn('route: "test-introspect"', res)
        self.assertIn('view: "view2.html" route: "test-introspect"', res)
        self.assertIn("view: view3.html", res)
Beispiel #3
0
    def actions(self):
        actions = directives.scan(self.pkg, set(), exclude)

        info = {}

        for action in actions:
            d = action.discriminator[0]
            d_data = info.setdefault(d, {})
            mod_data = d_data.setdefault(action.info.module.__name__, [])
            mod_data.append(action)

        return info
Beispiel #4
0
def initialize(config, packages=None, excludes=(),
               autoinclude=False, initsettings=True):
    """ Load ptah packages, scan and execute all configuration
    directives. """
    registry = config.registry
    registry.registerHandler(objectEventNotify, (IObjectEvent,))

    def exclude_filter(modname):
        if modname in packages:
            return True
        return exclude(modname, excludes)

    # list all packages
    if autoinclude:
        if packages is None:
            packages = list_packages(excludes=excludes)
            packages.extend([mod for mod in mods if exclude_filter(mod)])
        else:
            packages = list_packages(packages, excludes=excludes)
    elif packages is None:
        packages = ()

    # scan packages and load actions
    seen = set()
    actions = []

    for pkg in packages:
        actions.extend(directives.scan(pkg, seen, exclude_filter))

    cfg = Config(registry, actions)

    # execute actions
    actions = directives.resolveConflicts(actions)

    def runaction(action, cfg):
        cfg.action = action
        action(cfg)

    for action in actions:
        config.action(action.discriminator, runaction, (action, cfg))

    config.action(None, registry.notify, (Initialized(registry),))

    if initsettings:
        import settings

        config.action(
            None, settings.initialize_settings,
            (config.registry.settings, config))
Beispiel #5
0
    def test_type_introspect(self):
        from ptah.config import directives
        from ptah.manage.model import TypeIntrospection

        data = directives.scan(self.__class__.__module__, set())

        actions = []
        for action in data:
            if action.discriminator[0] == 'ptah-cms:type':
                actions.append(action)

        ti = TypeIntrospection(self.request)
        res = ti.renderActions(*actions)

        self.assertIn('<small>cms-type:content1</small>', res)
        self.assertIn('<small>cms-type:content2</small>', res)
Beispiel #6
0
    def test_introspect_route_introspect(self):
        from ptah.config import directives
        from ptah.manage.introspect import RouteDirective

        ptah.view.register_route("test-introspect", "/test/introspect")
        data = directives.scan(self.__class__.__module__, set())

        actions = []
        for action in data:
            if action.discriminator[0] == "ptah.view:route":
                actions.append(action)

        ti = RouteDirective(self.request)
        res = ti.renderActions(*actions)

        self.assertIn("test-introspect: /test/introspect", res)
Beispiel #7
0
    def test_uri_introspect(self):
        from ptah.config import directives
        from ptah.manage.uri import RESOLVER_ID, UriIntrospection

        data = directives.scan(self.__class__.__module__, set())

        actions = []
        for action in data:
            if action.discriminator[0] == RESOLVER_ID:
                actions.append(action)

        ti = UriIntrospection(self.request)
        res = ti.renderActions(*actions)

        self.assertIn('uri-intro-test', res)
        self.assertIn('ptah.manage.tests.test_uri', res)
Beispiel #8
0
    def update(self):
        data = []

        for dist in ADDONS:
            data.append(dist)

        self.addons = data

        if 'form.button.install' in self.request.POST:
            for name in self.request.POST.getall('addon'):
                for dist in self.addons:
                    if dist.project_name == name:
                        dist.activate()
                        actions = directives.scan(name, set())
                        pkg_resources.working_set.add(dist)
                        for action in actions:
                            action()
Beispiel #9
0
def initialize(packages=None, excludes=(), reg=None):
    """ Load ptah packages, scan and execute all configuration
    directives. """

    if reg is None:
        reg = Components('ptah')
        reg.registerHandler(objectEventNotify, (IObjectEvent,))

    sys.modules['ptah.config'].registry = reg
    sys.modules['ptah.config.api'].registry = reg

    def exclude_filter(modname):
        if modname in packages:
            return True
        return exclude(modname, excludes)

    # list all packages
    if packages is None:
        packages = list_packages(excludes=excludes)
        packages.extend([mod for mod in mods if exclude_filter(mod)])
    else:
        packages = list_packages(packages, excludes=excludes)

    # scan packages and load actions
    seen = set()
    actions = []

    for pkg in packages:
        actions.extend(directives.scan(pkg, seen, exclude_filter))

    config = Config(registry, actions)

    # execute actions
    actions = directives.resolveConflicts(actions)

    for action in actions:
        if action.id and action.id not in config.storage:
            config.storage[action.id] = {}
        action(config)
Beispiel #10
0
    def test_introspect_subscriber_introspect(self):
        from ptah.config import directives
        from ptah.manage.introspect import SubscriberDirective

        @ptah.config.subscriber(TestEvent)
        def eventHandler1(ev):
            """ """

        @ptah.config.subscriber(None, TestEvent)
        def eventHandler2(context, ev):
            """ """

        data = directives.scan(self.__class__.__module__, set())

        actions = []
        for action in data:
            if action.discriminator[0] == "ptah.config:subscriber":
                actions.append(action)

        ti = SubscriberDirective(self.request)
        res = ti.renderActions(*actions)

        self.assertIn("ptah.manage.tests.test_introspect.eventHandler1", res)
        self.assertIn("ptah.manage.tests.test_introspect.eventHandler2", res)
Beispiel #11
0
    def update(self):
        #ev = self.request.params.get('ev')
        self.route = route = None #directives.events.get(ev)

        if route is None:
            packages = list_packages()

            viewactions = []

            seen = set()
            routes = {}
            for pkg in packages:
                actions = directives.scan(pkg, seen, exclude)

                for action in actions:
                    d = action.discriminator[0]
                    if d == 'ptah.view:route':
                        name, pattern, factory = action.args[:3]
                        routes[name] = (pattern, name, factory, [])
                    elif d == 'ptah.view:view':
                        factory = action.info.context
                        if inspect.isclass(factory):
                            isclass = True
                            name = action.args[0]
                            context = action.args[1]
                            route = action.args[3]
                        else:
                            isclass = False
                            factory = action.args[0]
                            name = action.args[1]
                            context = action.args[2]
                            route = action.args[4]
                        if route:
                            viewactions.append(
                                (route, name, context, factory, action))

            sm = config.registry

            # add pyramid routes
            for route in sm.getUtility(IRoutesMapper).get_routes():
                if route.name not in routes:
                    routes[route.name] = (
                        route.pattern, route.name, route.factory, [])

            # attach views to routes
            for route, name, context, factory, action in viewactions:
                try:
                    rdata = routes[route][3]
                except: # pragma: no cover
                    continue
                rdata.append([getattr(factory, '__intr_path__', name),
                              action.info.module.__name__, lineno(factory),
                              factory, action.discriminator[-1]])
                rdata.sort()

            routes = routes.values()
            routes.sort()
            self.routes = routes

            # views
            route_requests = [i for n, i in sm.getUtilitiesFor(IRouteRequest)]

            views = []
            data = sm.adapters._adapters[3]
            for classifier, data in data.items():
                if classifier in (IViewClassifier, IExceptionViewClassifier):
                    for req, data2 in data.items():
                        if req in route_requests:
                            continue

                        for context, data3 in data2.items():
                            if isinstance(context, InterfaceClass):
                                context = '%s.%s'%(
                                    context.__module__, context.__name__)
                            else:
                                context = context.__name__

                            for provides, adapters in data3.items():
                                for name, factory in adapters.items():
                                    views.append(
                                        (context,name,classifier,req,factory))

            views.sort()
            self.views = views