def filter_only(keys, items): if isinstance(keys, str): keys = [keys] if isinstance(items, str): items = [items] # the default rule is to accept ret = True for key in keys: # ignore empty filters if key == '': continue for item in items: # key is part of the item, let the branch in if item.path.startswith(key): return True # siblings and their children, filter them out if item.parent.path.startswith(tree.path_parent(key)): ret = False continue # everything else should go in return ret
def filter_out(keys, items): if isinstance(keys, str): keys = [keys] if isinstance(items, str): items = [items] for key in keys: # ignore empty filters if key == '': continue for item in items: # key is part of the item, leave the branch out if item.path.startswith(key): return False # sibling and its children, let them in if item.path.startswith(tree.path_parent(key)): continue # everything else should get in return True
def test_false_direct_parent(self): self.assertNotEqual(tree.path_parent('/os/linux'), '/')
def test_direct_parent(self): self.assertEqual(tree.path_parent('/os/linux'), '/os')
def test_on_root(self): self.assertEqual(tree.path_parent('/'), '/')
def test_empty_string(self): self.assertEqual(tree.path_parent(''), '/')
def test_on_root(self): self.assertEqual(tree.path_parent('/'), '')
def test_empty_string(self): self.assertEqual(tree.path_parent(''), '')