def get_object(client: CommandClient, argv: List[str]) -> CommandClient: """ Constructs a path to object and returns given object (if it exists). """ if argv[0] == "cmd": argv = argv[1:] # flag noting if we have consumed arg1 as the selector, eg screen[0] parsed_next = False for arg0, arg1 in itertools.zip_longest(argv, argv[1:]): # previous argument was an item, skip here if parsed_next: parsed_next = False continue # check if it is an item try: client = client.navigate(arg0, arg1) parsed_next = True continue except SelectError: pass # check if it is an attr try: client = client.navigate(arg0, None) continue except SelectError: pass print("Specified object does not exist: " + " ".join(argv)) sys.exit(1) return client
def _ls(self, client: CommandClient, object_type: str | None) -> tuple[list[str], list[str]]: if object_type is not None: allow_root, items = client.items(object_type) str_items = [str(i) for i in items] if allow_root: children = client.navigate(object_type, None).children else: children = [] return children, str_items else: return client.children, []
def _find_node( self, src: CommandClient, *paths: str) -> Tuple[Optional[CommandClient], Optional[str]]: """Find an object in the command graph Return the object in the command graph at the specified path relative to the given node. """ if len(paths) == 0: return src, None path, *next_path = paths if path == "..": return self._find_node(src.parent or src, *next_path) if path not in src.children: return None, None if len(next_path) == 0: return src, path item, *maybe_next_path = next_path allow_root, items = src.items(path) for transformation in [str, int]: try: transformed_item = transformation(item) except ValueError: continue if transformed_item in items: next_node = src.navigate(path, transformed_item) return self._find_node(next_node, *maybe_next_path) if not allow_root: return None, None next_node = src.navigate(path, None) return self._find_node(next_node, *next_path)