def json_service(req, identifier): """Handle a JSON service req.""" handler = req.app._services.get(identifier) if handler is None: raise NotFound() #! if this event returns a handler it is called instead of the default #! handler. Useful to intercept certain requests. for callback in iter_listeners('before-json-service-called'): rv = callback(identifier, handler) if rv is not None: handler = rv result = handler(req) #! called right after json callback returned some data with the identifier #! of the req method and the result object. Note that events *have* #! to return an object, even if it's just changed in place, otherwise the #! return value will be `null` (None). for callback in iter_listeners('after-json-service-called'): result = callback(identifier, result) return Response(dump_json(result), mimetype='text/javascript')
def parse(input_data, parser=None, reason='unknown'): """Generate a doc tree out of the data provided. If we are not in unbound mode the `process-doc-tree` event is sent so that plugins can modify the tree in place. The reason is useful for plugins to find out if they want to render it or now. For example a normal blog post would have the reason 'post', a comment 'comment', an isolated page from a plugin maybe 'page' etc. """ input_data = u'\n'.join(input_data.splitlines()) app = get_application() if parser is None: try: parser = app.parsers[app.cfg['default_parser']] except KeyError: # the plugin that provided the default parser is not # longer available. reset the config value to the builtin # parser and parse afterwards. t = app.cfg.edit() t.revert_to_default('default_parser') t.commit() parser = app.parsers[app.cfg['default_parser']] else: try: parser = app.parsers[parser] except KeyError: raise ValueError('parser %r does not exist' % (parser, )) tree = parser.parse(input_data, reason) #! allow plugins to alter the doctree. for callback in iter_listeners('process-doc-tree'): item = callback(tree, input_data, reason) if item is not None: tree = item return tree
def parse(input_data, parser=None, reason='unknown'): """Generate a doc tree out of the data provided. If we are not in unbound mode the `process-doc-tree` event is sent so that plugins can modify the tree in place. The reason is useful for plugins to find out if they want to render it or now. For example a normal blog post would have the reason 'post', a comment 'comment', an isolated page from a plugin maybe 'page' etc. """ input_data = u'\n'.join(input_data.splitlines()) app = get_application() if parser is None: try: parser = app.parsers[app.cfg['default_parser']] except KeyError: # the plugin that provided the default parser is not # longer available. reset the config value to the builtin # parser and parse afterwards. t = app.cfg.edit() t.revert_to_default('default_parser') t.commit() parser = app.parsers[app.cfg['default_parser']] else: try: parser = app.parsers[parser] except KeyError: raise ValueError('parser %r does not exist' % (parser,)) tree = parser.parse(input_data, reason) #! allow plugins to alter the doctree. for callback in iter_listeners('process-doc-tree'): item = callback(tree, input_data, reason) if item is not None: tree = item return tree