class PathRoutingEngine(object): """ A simple routing engine for path-based patterns based on the powerful Pyramid request router. """ def __init__(self): self.mapper = RoutesMapper() def add_route(self, name, pattern, methods=[]): predicates = [] for method in methods: predicate = RequestMethodPredicate(method.upper(), None) predicates.append(predicate) self.mapper.connect(name, pattern, predicates=predicates) def match(self, method, path): #print 'PathRoutingEngine attempt to match path:', path request = self._getRequest(attributes={'method': method}, environ={'PATH_INFO': path}) result = self.mapper(request) if result['route']: #print 'PathRoutingEngine matched result: ', result return result def _getRequest(self, attributes, environ): # from pyramid.tests.test_urldispatch environ_default = {'SERVER_NAME':'localhost', 'wsgi.url_scheme':'http'} environ.update(environ_default) request = DummyRequest(attributes, environ) reg = get_current_registry() request.registry = reg return request
class PathRoutingEngine(object): """ A simple routing engine for path-based patterns based on the powerful Pyramid request router. """ def __init__(self): self.mapper = RoutesMapper() def add_route(self, name, pattern, methods=None): methods = methods or [] predicates = [] for method in methods: predicate = RequestMethodPredicate(method.upper(), None) predicates.append(predicate) self.mapper.connect(name, pattern, predicates=predicates) def match(self, method, path): request = self._getRequest(attributes={'method': method}, environ={'PATH_INFO': path}) result = self.mapper(request) if result['route']: return result def _getRequest(self, attributes, environ): # from pyramid.tests.test_urldispatch environ_default = { 'SERVER_NAME': 'localhost', 'wsgi.url_scheme': 'http' } environ.update(environ_default) request = DummyRequest(attributes, environ) reg = get_current_registry() request.registry = reg return request
def _connectRoute(self, name, path, factory=None): from pyramid.interfaces import IRoutesMapper from pyramid.urldispatch import RoutesMapper mapper = self.registry.queryUtility(IRoutesMapper) if mapper is None: mapper = RoutesMapper() self.registry.registerUtility(mapper, IRoutesMapper) mapper.connect(name, path, factory)
def get_routes_mapper(self): """ Return the :term:`routes mapper` object associated with this configurator's :term:`registry`.""" mapper = self.registry.queryUtility(IRoutesMapper) if mapper is None: mapper = RoutesMapper() self.registry.registerUtility(mapper, IRoutesMapper) return mapper
def get_registry(settings): registry = Registry('testing') config = Configurator(registry=registry) if getattr(registry, 'settings', None) is None: config._set_settings(settings) registry.registerUtility(RoutesMapper(), IRoutesMapper) config.commit() return registry
def register_route_impl(cfg, name, pattern, factory, predicates, pregenerator, use_global_views): request_iface = cfg.registry.queryUtility(IRouteRequest, name=name) if request_iface is None: if use_global_views: bases = (IRequest,) else: bases = () request_iface = route_request_iface(name, bases) cfg.registry.registerUtility(request_iface, IRouteRequest, name=name) mapper = cfg.registry.queryUtility(IRoutesMapper) if mapper is None: mapper = RoutesMapper() cfg.registry.registerUtility(mapper, IRoutesMapper) return mapper.connect(name, pattern, factory, predicates=predicates, pregenerator=pregenerator, static=False)
def __init__(self): self.mapper = RoutesMapper()