def compact_path_string(other_path:Path) -> str: if other_path.is_relative: relative_path_str = other_path.get_components_string() global_path_str = (self.path + other_path).get_components_string() else: relative_path_str = self.convert_to_relative_path(other_path).get_components_string() global_path_str = other_path.get_components_string() if len(relative_path_string) < len(global_path_string): return relative_path_string else: return global_path_str
def resolve_path(self, path:Path) -> SearchResult: if path.is_relative: nearest_container = self.as_container() if nearest_container is None: if self.parent is None: log.debug("Can't resolve relative path because we don't have a parent") nearest_container = self.parent.as_container() if nearest_container is None: log.debug("Expected parent to be a container") if not path.get_component(0).is_parent: log.debug("Expected component 0 to be a parent") path = path.tail() return nearest_container.content_at_path(path) else: return self.root_content_container().content_at_path(path)
def get_path(self): if self.parent is None: return Path() else: comps = [] child = self container = child.parent.as_container() while container: if child.has_valid_name(): comps.append(Component(name=child.name)) else: comps.append(Component(index=container.content.index_of(child))) child = container container = container.parent.as_container() return Path(comps)
def setUp(self): self.empty = Path() self.one_path = Path([Component(name='this')]) self.two_path = Path([Component(name='this'), Component(name='that')]) self.three_path = Path([ Component(name='this'), Component(name='that'), Component(name='other') ])
def convert_to_relative_path(self, global_path:Path) -> Path: own_path = self.path min_path_length = min(len(global_path), len(own_path)) last_shared_path_comp_index = None for i in range(min_path_length): own_comp = own_path.get_component(i) other_comp = global_path.get_component(i) if own_comp == other_comp: last_shared_path_comp_index = i else: break if last_shared_path_comp_index is None: return global_path num_upwards_moves = len(own_path) - 1 - last_shared_path_comp_index new_path_comps = [] for up in range(num_upwards_moves): new_path_comps.append(Component.to_parent()) for down in range(last_shared_path_comp_index + 1, len(global_path)): new_path_comps.append(global_path.get_component(down)) return Path(components=new_path_comps, relative=True)
def path_to_first_leaf_content(self) -> Path: path = Path() container = self while container is not None: if len(container.content) > 0: path.components.append(Component(0)) container = container.content[0].as_container() # This check is not part of the c# source, but is needed to prevent an # infinite loop else: raise ValueError("Unexpectedly found empty container") return self.path + path
def setUp(self): self.path = Path() self.index_c = Component(self.path, index=1) self.name_c = Component(self.path, name="name")
def setUp(self): self.path = Path(components_string="this.that.0.1") self.null_dt = DivertTargetValue() self.dt = DivertTargetValue(self.path)
def test_set_components_string(self): relative = Path(components_string='.this.that') self.assertEqual(str(relative), '.this.that')
class TestPath(TestCase): def setUp(self): self.empty = Path() self.one_path = Path([Component(name='this')]) self.two_path = Path([Component(name='this'), Component(name='that')]) self.three_path = Path([ Component(name='this'), Component(name='that'), Component(name='other') ]) def test_head(self): self.assertIsNone(self.empty.head()) self.assertEqual(self.one_path.head(), 'this') def test_tail(self): self.assertEqual(len(self.empty.tail()), 0) self.assertEqual(len(self.one_path.tail()), 0) self.assertEqual(len(self.two_path.tail()), 1) def test_last_component(self): self.assertIsNone(self.empty.last_component()) self.assertEqual(self.one_path.last_component(), 'this') def test_contains_named_component(self): self.assertFalse(self.empty.contains_named_component()) ixpath = Path([Component(index=1)]) self.assertFalse(ixpath.contains_named_component()) self.assertTrue(self.one_path.contains_named_component()) def test_add(self): self.assertEqual(len(self.empty + self.two_path), 2) self.assertEqual(len(self.one_path + self.two_path), 3) def test_get_components_string(self): self.assertEqual(self.empty.get_components_string(), '.') self.assertEqual(self.two_path.get_components_string(), 'this.that') self.two_path.is_relative = True self.assertEqual(self.two_path.get_components_string(), '.this.that') def test_set_components_string(self): relative = Path(components_string='.this.that') self.assertEqual(str(relative), '.this.that') def test_eq(self): self.assertEqual(self.one_path, self.one_path + self.empty) self.assertNotEqual(self.one_path, self.two_path)
def test_contains_named_component(self): self.assertFalse(self.empty.contains_named_component()) ixpath = Path([Component(index=1)]) self.assertFalse(ixpath.contains_named_component()) self.assertTrue(self.one_path.contains_named_component())
def set_path_string_for_count(self, value): if value is None: self.path_for_count = None else: self.path_for_count = Path(value)