def get_view(self, path): """ Try to map a given path to it's specific view. If a match is found, a view object is created with the right regex groups(named or unnamed). :param str path: path to be matched :rtype: view object, relative path """ for route in self.routes: result = re.search(route['regex'], path) if result is None: continue groups = result.groups() relative_path = re.sub(route['regex'], '', path) relative_path = '/' if not relative_path else relative_path cache_key = result.group(0) log.debug("Router: Cache key for %s: %s", path, cache_key) view = lru_cache.get_if_exists(cache_key) if view is not None: log.debug("Router: Serving %s from cache", path) return view, relative_path kwargs = result.groupdict() # TODO: move all this to a nice config variable kwargs['repo'] = self.repo kwargs['ignore'] = self.repo.ignore kwargs['repo_path'] = self.repo_path kwargs['mount_path'] = self.mount_path kwargs['regex'] = route['regex'] kwargs['relative_path'] = relative_path kwargs['current_path'] = self.current_path kwargs['history_path'] = self.history_path kwargs['uid'] = self.uid kwargs['gid'] = self.gid kwargs['branch'] = self.branch kwargs['mount_time'] = self.mount_time kwargs['queue'] = self.commit_queue kwargs['max_size'] = self.max_size kwargs['max_offset'] = self.max_offset args = set(groups) - set(kwargs.values()) view = route['view'](*args, **kwargs) lru_cache[cache_key] = view log.debug("Router: Added %s to cache", path) return view, relative_path raise ValueError("Found no view for '{}'".format(path))
def get_view(self, path): """ Try to map a given path to it's specific view. If a match is found, a view object is created with the right regex groups(named or unnamed). :param str path: path to be matched :rtype: view object, relative path """ for route in self.routes: result = re.search(route['regex'], path) if result is None: continue groups = result.groups() relative_path = re.sub(route['regex'], '', path) relative_path = '/' if not relative_path else relative_path cache_key = result.group(0) log.debug("Router: Cache key for %s: %s", path, cache_key) view = lru_cache.get_if_exists(cache_key) if view is not None: log.debug("Router: Serving %s from cache", path) return view, relative_path kwargs = result.groupdict() # TODO: move all this to a nice config variable kwargs['repo'] = self.repo kwargs['ignore'] = self.repo.ignore kwargs['repo_path'] = self.repo_path kwargs['mount_path'] = self.mount_path kwargs['regex'] = route['regex'] kwargs['relative_path'] = relative_path kwargs['current_path'] = self.current_path kwargs['history_path'] = self.history_path kwargs['uid'] = self.uid kwargs['gid'] = self.gid kwargs['branch'] = self.branch kwargs['mount_time'] = self.mount_time kwargs['queue'] = self.commit_queue kwargs['max_size'] = self.max_size kwargs['max_offset'] = self.max_offset args = set(groups) - set(kwargs.values()) view = route['view'](*args, **kwargs) lru_cache[cache_key] = view log.debug("Router: Added %s to cache", path) return view, relative_path raise ValueError("Found no view for '{}'".format(path))