def path_lookups(path): if path.startswith(BRANCH_ID_ALIAS_PREFIX + '/'): try: parts = path.split('/', 2) branch_id = int(parts[1]) except (ValueError, IndexError): return trailing = '/'.join([''] + parts[2:]) yield {'type': 'id', 'branch_id': branch_id, 'trailing': trailing} return alias_prefix_trailing = BRANCH_ALIAS_PREFIX + '/' if path.startswith(alias_prefix_trailing): yield {'type': 'alias', 'lp_path': path[len(alias_prefix_trailing):]} return for unique_name, trailing in iter_split(path, '/', [5, 3]): yield { 'type': 'branch_name', 'unique_name': unique_name, 'trailing': trailing, } for control_name, trailing in iter_split(path, '/', [4, 2]): yield { 'type': 'control_name', 'control_name': control_name, 'trailing': trailing, }
def test_iter_split(self): # iter_split loops over each way of splitting a string in two using # the given splitter. self.assertEqual([('one', '')], list(iter_split('one', '/'))) self.assertEqual([], list(iter_split('', '/'))) self.assertEqual([('one/two', ''), ('one', '/two')], list(iter_split('one/two', '/'))) self.assertEqual([('one/two/three', ''), ('one/two', '/three'), ('one', '/two/three')], list(iter_split('one/two/three', '/')))
def test_iter_split(self): # iter_split loops over each way of splitting a string in two using # the given splitter. self.assertEqual([('one', '')], list(iter_split('one', '/'))) self.assertEqual([], list(iter_split('', '/'))) self.assertEqual( [('one/two', ''), ('one', '/two')], list(iter_split('one/two', '/'))) self.assertEqual( [('one/two/three', ''), ('one/two', '/three'), ('one', '/two/three')], list(iter_split('one/two/three', '/')))
def _getBranchIdAndTrailingPath(self, location): """Return the branch id and trailing path for 'location'. In addition this method returns whether the answer can from the cache or from the database. """ for first, second in iter_split(location[1:], '/'): if first in self._cache: branch_id, inserted_time = self._cache[first] if (self._now() < inserted_time + config.codehosting.branch_rewrite_cache_lifetime): return branch_id, second, "HIT" lookup = getUtility(IBranchLookup) branch, trailing = lookup.getByHostingPath(location.lstrip('/')) if branch is not None: try: branch_id = branch.id except Unauthorized: pass else: unique_name = location[1:-len(trailing)] self._cache[unique_name] = (branch_id, self._now()) return branch_id, trailing, "MISS" return None, None, "MISS"