def assert_explicit_permissions(module, app_class): morepath.autoscan() app_class.commit() for action, fn in dectate.Query('view')(app_class): if fn.__module__.startswith('onegov'): assert action.permission is not None, ( f'{fn.__module__}.{fn.__name__} has no permission')
def get_for_context(cls, registry, context=None): q = dectate.Query('toctree') # Get the registrations for this context, sorted by # system_order qr = sorted(q.filter(context=context)(registry), key=lambda t: t[0].system_order) # Return the first match's decorated object return qr[0][1]
def get_callbacks(cls, registry, event_name: SphinxEvent): # Presumes the registry has been committed # First ensure that event_name is valid assert event_name in SphinxEvent q = dectate.Query('event') qr = sorted(q(registry), key=lambda args: args[0].order) return [args[1] for args in qr if args[0].name == event_name]
def get_genericpage(cls, kb_app): """ Return the one class if configured, otherwise default """ # Presumes the registry has been committed q = dectate.Query('genericpage') klasses = sorted(q(kb_app), key=lambda args: args[0].order) if not klasses: # The site doesn't configure a genericpage, return Genericpage else: return klasses[0][1]
def initialize(self): """ Commit the actions and initialize the registry """ dectate.commit(self.registry) # Get the injectables injectables = self.injectables # Stash ServiceManager stuff in injectables self.add_injectable(self.config) self.add_injectable(self) # Make the Registry injectable using a well-known name injectables['Registry'] = self.registry # Make each service config available as injectable for serviceconfig in self.config.serviceconfigs.values(): self.add_injectable(serviceconfig) # Get a list of services q = dectate.Query('service') services = list(q(self.registry)) # Go through each service, initialize it, then # put an instance in the service manager for action, target in services: # Use injector to make our target class props = dict() service = inject(props, injectables, target) # Store this in our dict of services name = action.name self.services[name] = service # Add this service, and its config, as injectable self.add_injectable(service) # TODO Should move more of this to a setup() function in each # service, to let them provide injectables if 'resource' in self.services: resources = self.services['resource'].resources self.add_injectable(resources) # Now the adapters in services['adapter']. Each adapter # can be dependency-injected...albeit carefully. Add the # (unique) for_ targets as injectable adapters. try: for action in AdapterAction.sorted_actions(self.registry): f = action[0].predicates['for_'].value self.adapters[f.__name__] = f except dectate.error.QueryError: # Likely a unit test that doesn't include adapters in # the registry pass
def actions(committed_registry): q = dectate.Query('view') actions = list(q(committed_registry)) return actions
def get_callbacks(cls, registry): # Presumes the registry has been committed q = dectate.Query('dumper') return [args[1] for args in q(registry)]
def sorted_actions(cls, registry: dectate.App) -> Sequence[dectate.Action]: q = dectate.Query(cls.action_name) actions = list(q(registry)) sorted_actions = sorted(actions, reverse=True, key=lambda x: x[0]) return sorted_actions