def cli_pretty_print(fire: object): """Pretty print the current state Args: fire (object): The fire object """ if fire is not None and isinstance(fire, Chepy): print_in_colors(pformat(fire.state)) else: print(red("Nope. That didnt work.."))
def cli_highlight(fire: object, highlight: str): """Highlight regex match for cli Args: fire (object): The fire object. highlight (str): Regex to highlight """ current_state = fire.states[fire._current_index] if fire is not None and isinstance(fire, Chepy): try: print( re.sub('({})'.format(highlight), yellow_background(r'\1'), str(current_state))) except: red('Could not highlight because state is not a string') # elif type(current_state) == bytes or type(current_state) == bytearray: # print(re.sub('({})'.format(highlight).encode(), red(r'\1').encode(), current_state).decode()) else: print(type(fire))
def cli_get_attr(fire: object, attr: str): """Get attributes from current state type Args: fire (object): The fire object attr (str): Required. A valid attr name """ if fire is not None and not isinstance(fire, Chepy): print_in_colors(getattr(fire, attr)()) else: print(red("Nope. That didnt work.."))
def process_tag(tag, i1, i2, j1, j2): # pragma: no cover if tag == "replace": if colors: return _int_colors.blue(matcher.b[j1:j2]) else: return "{" + matcher.a[i1:i2] + "->" + matcher.b[j1:j2] + "}" if tag == "delete": if colors: return _int_colors.red(matcher.a[i1:i2]) else: return "{-" + matcher.a[i1:i2] + "}" if tag == "equal": return matcher.a[i1:i2] if tag == "insert": if colors: return _int_colors.green(matcher.b[j1:j2]) else: return "{+" + matcher.b[j1:j2] + "}" assert False, "Unknown tag %r" % tag
def main(): global fire_obj last_command = [] args = parse_args(sys.argv[1:]) args_data = args.data if args.recipe: print(Chepy(*args_data).load_recipe(args.recipe).o) else: args_data.append("-") history_file = config.history_path session = PromptSession( history=FileHistory(history_file), style=get_style(), wrap_lines=True, auto_suggest=AutoSuggestFromHistory(), ) try: while True: prompt = session.prompt( prompt_message(fire_obj=fire_obj), bottom_toolbar=bottom_toolbar(fire_obj), completer=FuzzyCompleter( merge_completers([CustomCompleter(), chepy_cli.CliCompleter()]) ), validator=CustomValidator(), rprompt=get_current_type(fire_obj), ) # check and output any commands that start with cli_ if re.match(r"^\!", prompt): print(magenta(subprocess.getoutput(re.sub(r"^\!\s?", "", prompt)))) # check if line is a comment elif re.match(r"^#", prompt): print(cyan(prompt)) # get help for a method elif re.match(r"^\?", prompt): _method_name = re.match(r"^\?(\s?)+([\w_]+)", prompt).group(2) chepy_cli.get_doc(_method_name) # check if method called is a cli method elif re.search(r"^cli_.+", prompt): cli_method = prompt.split()[0] cli_args = re.search(r"--(\w+)\s([\w\W]+)", prompt) # Show errors encountered if cli_method == "cli_show_errors": getattr(chepy_cli, "cli_show_errors")(errors) # show the current plugin path elif cli_method == "cli_plugin_path": getattr(chepy_cli, "cli_plugin_path")(config) # Edit the current state elif cli_method == "cli_edit_state": try: getattr(chepy_cli, "cli_edit_state")(fire_obj, args_data) args_data = args_data[0 : args_data.index("-")] + ["-"] except: e_type, e_msg, e_traceback = sys.exc_info() print(red(e_type.__name__), yellow("Could not edit state")) # Go back one step elif cli_method == "cli_go_back": args_data = args_data[: -len(last_command + ["-"])] print(cyan("Go back: {}".format(last_command))) # Delete the cli history file elif cli_method == "cli_delete_history": Path(config.history_path).unlink() elif cli_args: getattr(chepy_cli, cli_method)( fire_obj, **{cli_args.group(1): cli_args.group(2)} ) else: getattr(chepy_cli, cli_method)(fire_obj) else: for method in chepy: if not method.startswith("_") and not isinstance( getattr(Chepy, method), property ): fire.decorators._SetMetadata( getattr(Chepy, method), fire.decorators.ACCEPTS_POSITIONAL_ARGS, False, ) args_data += prompt.split() if args_data[-1] != "-": args_data.append("-") try: last_command = prompt.split() + ["-"] fire_obj = fire.Fire(Chepy, command=args_data) # handle required args for methods except fire.core.FireExit: args_data = args_data[: -len(last_command)] except TypeError as e: print(red(e.message)) except SystemExit: sys.exit() except: # go back to last working arg e_type, e_msg, e_traceback = sys.exc_info() print(red(e_type.__name__), yellow(e_msg.__str__())) args_data = args_data[: -len(last_command)] continue except KeyboardInterrupt: print(green("\nOKBye")) sys.exit()
def get_doc(method: str): """Get docs for a method""" try: print(yellow(getattr(Chepy, method).__doc__)) except: print(red(pformat("Could not find docs...")))
def main(): global fire_obj last_command = [] args = parse_args(sys.argv[1:]) args_data = args.data args_data.append("-") history_file = config.history_path session = PromptSession( history=FileHistory(history_file), style=get_style(), wrap_lines=True, auto_suggest=AutoSuggestFromHistory(), ) try: while True: prompt = session.prompt( prompt_message(fire_obj=fire_obj), bottom_toolbar=bottom_toolbar(fire_obj), completer=FuzzyCompleter( merge_completers([CustomCompleter(), chepy_cli.CliCompleter()]) ), validator=CustomValidator(), rprompt=get_current_type(fire_obj), ) # check and output any commands that start with cli_ if re.match(r"^\!", prompt): print(subprocess.getoutput(re.sub(r"^\!\s?", "", prompt))) elif re.search(r"^cli_.+", prompt): cli_method = prompt.split()[0] cli_args = re.search(r"--(\w+)\s(\w+)", prompt) if cli_method == "cli_show_errors": getattr(chepy_cli, "cli_show_errors")(errors) elif cli_method == "cli_go_back": args_data = args_data[: -len(last_command + ["-"])] print(cyan("Go back: {}".format(last_command))) elif cli_method == "cli_delete_history": Path(config.history_path).unlink() elif cli_args: getattr(chepy_cli, cli_method)( fire_obj, **{cli_args.group(1): cli_args.group(2)} ) else: getattr(chepy_cli, cli_method)(fire_obj) else: for method in chepy: if not method.startswith("_") and not isinstance( getattr(Chepy, method), property ): fire.decorators._SetMetadata( getattr(Chepy, method), fire.decorators.ACCEPTS_POSITIONAL_ARGS, False, ) args_data += prompt.split() if args_data[-1] != "-": args_data.append("-") try: last_command = prompt.split() + ["-"] fire_obj = fire.Fire(Chepy, command=args_data) except: # go back to last working arg e_type, e_msg, e_traceback = sys.exc_info() print(red(e_type.__name__), yellow(e_msg.__str__())) args_data = args_data[: -len(last_command)] continue except KeyboardInterrupt: print("OKBye") sys.exit()