def get_object(client: InteractiveCommandClient, argv: List[str]) -> InteractiveCommandClient: """ Constructs a path to object and returns given object (if it exists). """ if argv[0] == "cmd": argv = argv[1:] # Generate full obj specification for arg in argv: try: # check if it is an item client = client[client.normalize_item(arg)] continue except KeyError: pass try: # check if it is an attr client = getattr(client, arg) continue except AttributeError: pass print("Specified object does not exist " + " ".join(argv)) sys.exit(1) return client
def print_commands(prefix: str, obj: InteractiveCommandClient) -> None: """Print available commands for given object.""" prefix += " -f " output = [] max_cmd = 0 # max len of cmd for formatting try: cmds = obj.commands() except AttributeError: print("error: Sorry no commands in ", prefix) sys.exit(1) except CommandError: print("error: Sorry no such object ", prefix) sys.exit(1) for cmd in cmds: doc_args = get_formated_info(obj, cmd) pcmd = prefix + cmd max_cmd = max(len(pcmd), max_cmd) output.append([pcmd, doc_args]) # Print formatted output formating = "{:<%d}\t{}" % (max_cmd + 1) for line in output: print(formating.format(line[0], line[1]))
def get_formated_info(obj: InteractiveCommandClient, cmd: str, args=True, short=True) -> str: """Get documentation for command/function and format it. Returns: * args=True, short=True - '*' if arguments are present and a summary line. * args=True, short=False - (function args) and a summary line. * args=False - a summary line. If 'doc' function is not present in object or there is no doc string for given cmd it returns empty string. The arguments are extracted from doc[0] line, the summary is constructed from doc[1] line. """ if hasattr(obj, "doc"): doc = obj.doc(cmd).splitlines() else: doc = None if doc is not None: tdoc = doc[0] doc_args = tdoc[tdoc.find("(") + 1:tdoc.find(")")].strip() short_description = doc[1] if len(doc) > 1 else "" if doc_args: # return formatted args doc_args = "({})".format(doc_args) else: doc_args = "" short_description = "" if args is False: doc_args = "" elif args and short: doc_args = "*" if len(doc_args) > 1 else " " return (doc_args + " " + short_description).rstrip()
def cmd_obj(args) -> None: "Runs tool according to specified arguments." if args.obj_spec: sock_file = args.socket or find_sockfile() ipc_client = Client(sock_file) cmd_object = IPCCommandInterface(ipc_client) cmd_client = InteractiveCommandClient(cmd_object) obj = get_object(cmd_client, args.obj_spec) if args.function == "help": print_commands("-o " + " ".join(args.obj_spec), obj) elif args.info: print(get_formated_info(obj, args.function, args=True, short=False)) else: ret = run_function(obj, args.function, args.args) if ret is not None: pprint.pprint(ret) else: print_base_objects() sys.exit(1)
def f(cmd): if cmd: # c here is used in eval() below q = QtileCommandInterface(self) c = InteractiveCommandClient(q) # noqa: F841 try: cmd_arg = str(cmd).split(' ') except AttributeError: return cmd_len = len(cmd_arg) if cmd_len == 0: logger.info('No command entered.') return try: result = eval(u'c.{0:s}'.format(cmd)) except (CommandError, CommandException, AttributeError) as err: logger.error(err) result = None if result is not None: from pprint import pformat message = pformat(result) if messenger: self.cmd_spawn('{0:s} "{1:s}"'.format(messenger, message)) logger.debug(result)
def __init__(self, qtile: CommandObject) -> None: q = QtileCommandInterface(qtile) self.client = InteractiveCommandClient(q) self.thisfinal = None # type: Optional[str] self.reset()
from libqtile.command.client import InteractiveCommandClient c = InteractiveCommandClient() print(c.status())
return False if self._layouts and q.current_layout.name not in self._layouts: return False return True class LazyCommandInterface(CommandInterface): """A lazy loading command object Allows all commands and items to be resolved at run time, and returns lazily evaluated commands. """ def execute(self, call: CommandGraphCall, args: Tuple, kwargs: Dict) -> LazyCall: """Lazily evaluate the given call""" return LazyCall(call, args, kwargs) def has_command(self, node: CommandGraphNode, command: str) -> bool: """Lazily resolve the given command""" return True def has_item(self, node: CommandGraphNode, object_type: str, item: Union[str, int]) -> bool: """Lazily resolve the given item""" return True lazy = InteractiveCommandClient(LazyCommandInterface())
#!/bin/python from libqtile.command.client import InteractiveCommandClient c = InteractiveCommandClient().layout.info()['name'] if c == 'max': print('MONO') elif c == 'bsp': print('BSPM') elif c == 'monadtall': print('TALL') elif c == 'zoomy': print('ZOOM') else: print(c.upper())
from libqtile.command.client import InteractiveCommandClient c = InteractiveCommandClient() print(c.screen.info()["index"])