def testing(): from pyxer.controller import getObjectsFullName, isController static = "pyxer.routing:static" if __name__=="__main__": module = "__main__" else: module = "pyxer.routing" data = [ ("", "public:index"), ("/", "public:index"), ("index", "public:index"), ("/index", "public:index"), # slash is ignored ("index.htm", "public:index"), ("index.html", "public:index"), ("index.gif", "pyxer.routing:static", dict(static="index.gif")), # Without slash a module is not recognized (could be handled by 'static' though) ("sub1", 'pyxer.routing:static', {'static': 'sub1'}), # sub1 ("sub1/", "public.sub1:index"), ("sub1/dummy", "public.sub1:dummy"), ("sub1/dummy2", "public.sub1:default"), ("sub1/content1", "public.sub1:content1"), ("sub1/content1/some", "public.sub1:content1", dict(name="some")), ("sub1/content2/some", "public.sub1:content2", dict(name="some")), ("sub1/content1/some/more", "public.sub1:content1", dict(name="some/more")), ("sub1/content2/some/more", 'pyxer.routing:static', {'static': 'content2/some/more'}), # Doesn't match at all and is therefore passed to 'static' ("/some/path/index.gif", "pyxer.routing:static", dict(static="some/path/index.gif")), # Referencing an external module ("sub1/pub2/", "public2:index", dict()), ("sub1/pub2/path/index.gif", "pyxer.routing:static", dict(static="path/index.gif")), ] router = Router("public") for sample in data: if len(sample)==3: path, object_name, object_vars = sample else: path, object_name = sample object_vars = dict() obj, vars = router.match(path) if vars is None: vars = dict() else: vars.pop("controller") vars.pop("module") for k in vars.keys(): if k.startswith("pyxer."): del vars[k] name = getObjectsFullName(obj) ct = isController(obj) # print "%-35r %r, %r" % (path, name, vars) assert object_name == name assert object_vars == vars
def _match(self, path, module = None, urlvars = {}): # Normalize module infos self.set_module(module) # Search for route in self.routes + self.routes_default: match = route.template.match(path) # log.debug("Try to match %r %r", route, match) if match: urlvars = {} urlvars.update(route.vars) urlvars.update(match.groupdict()) tail = path[match.end():].lstrip("/") urlvars["pyxer.tail"] = tail urlvars["pyxer.match"] = path[match.start():match.end()] urlvars["pyxer.path"] = os.path.dirname(os.path.abspath(self.module.__file__)) log.debug("Matched %r %r %r %r", path, route, urlvars, route.vars) # Abort matching if urlvars["module"] is None and urlvars["controller"] is None: return (None, None) # Handle module if urlvars["module"] is not None: obj = urlvars["module"] # If it is a module go ahead if isinstance(obj, types.ModuleType): module = obj # If it is a string it could be a module or a elif isinstance(obj, basestring): # Load module relatively or absolute module = ( self.load_module(self.module_name, obj) or self.load_module(obj)) if module is None: log.debug("Module %r not found", obj) continue # If it is anything else, let the caller decide what to do else: raise Exception("No module") # Let's see if they need a Router() if not hasattr(module, "router"): module.router = Router(module) # The router goes to the next round return module.router._match(tail, module) #, urlvars) # Handle controller if urlvars["controller"] is not None: obj = urlvars["controller"] if isinstance(obj, basestring): if hasattr(self.module, obj): obj = getattr(self.module, obj) if hasattr(obj, "iscontroller") or isController(obj): return obj, urlvars else: log.debug("Object %r is not a controller", obj) continue return (None, None)