Exemplo n.º 1
0
def test_broken_str():
    class BrokenStr(Exception):
        def __str__(self):
            1 / 0

    console = Console(width=100, file=io.StringIO())
    try:
        raise BrokenStr()
    except Exception:
        console.print_exception()
    result = console.file.getvalue()
    print(result)
    assert "<exception str() failed>" in result
Exemplo n.º 2
0
class Echo:
    """Convenience class for pretty console printing."""
    def __init__(self):
        """Initialize Echo()."""
        self.console = Console()

    def __call__(  # noqa: C901 your mom is too complex
        self,
        message,
        *args,
        error=False,
        **kwargs,
    ):  # noqa: C901
        """Format message based on type."""
        try:
            to_print = message

            if isinstance(message, bytes):
                message = message.decode()

            if isinstance(message, str):
                color = "cyan"
                if error:
                    color = "red"

                if args:
                    fmt_args = (f"[bold {color}]{a}[/bold {color}]"
                                for a in args)
                    to_print = message.format(*fmt_args)

                elif kwargs:
                    fmt_args = {
                        k: f"[bold {color}]{v}[/bold {color}]"
                        for k, v in kwargs
                    }
                    to_print = message.format(**fmt_args)

            elif isinstance(message, (list, tuple, Generator, dict)):
                try:
                    to_print = Syntax(json.dumps(message, indent=2),
                                      "json",
                                      theme="native")
                except json.JSONDecodeError:
                    pass

            elif isinstance(message, Exception):
                raise message

            return self.console.print(to_print)
        except Exception:
            self.console.print_exception()
Exemplo n.º 3
0
 def _fancy_msg(self, f, **event_dict):
     c = Console(file=f, highlight=False, emoji=False, markup=False)
     lll = event_dict.pop('level').lower()
     llu = lll.upper()
     exc = event_dict.pop('exception', None)
     event_dict.pop('exception_type', None)
     event_dict.pop('exception_file', None)
     name = event_dict.pop('name', 'root')
     pid = event_dict.pop('pid')
     ts = event_dict.pop('timestamp')[0:-3] + "Z"
     try:
         msg = event_dict.pop('event')
     except KeyError:
         msg = "None"
     for key in self._json_only_keys:  # pylint: disable=E1133
         try:
             event_dict.pop(key)
         except KeyError:
             pass
     extra = ""
     if len(event_dict) > 0:
         extra = kv_renderer(None, None, event_dict)
     if lll in ['notset', 'debug', 'info', 'warning',
                'error', 'critical']:
         ls = "logging.level.%s" % lll
     else:
         ls = "none"
     output = Table(show_header=False, expand=True, box=None,
                    padding=(0, 1, 0, 0))
     output.add_column(style="log.time")
     output.add_column(width=10, justify="center")
     output.add_column(justify="center")
     output.add_column(ratio=1)
     row = []
     row.append(Text(ts))
     row.append(Text("[%s]" % llu, style=ls))
     row.append(Text(name, style="bold") + Text("#") + Text("%i" % pid,
                                                            style="yellow"))
     row.append(Text(msg))
     output.add_row(*row)
     if extra != "":
         output.add_row(
             "", "", "",
             Text("{ ", style="repr.attrib_name") +
             Text(extra, style="repr.attrib_name") +
             Text(" }", style="repr.attrib_name"))
     c.print(output)
     if exc is not None:
         c.print_exception()
         if Config.auto_dump_locals:
             _dump_locals(f)
Exemplo n.º 4
0
 def wrap(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except BaiduPCSError as err:
         print(
             f"(v{__version__}) [bold red]ERROR[/bold red]: BaiduPCSError: {err}"
         )
         if DEBUG:
             console = Console()
             console.print_exception()
     except Exception as err:
         print(f"(v{__version__}) [bold red]System ERROR[/bold red]: {err}")
         if DEBUG:
             console = Console()
             console.print_exception()
Exemplo n.º 5
0
def test_recursive():
    def foo(n):
        return bar(n)

    def bar(n):
        return foo(n)

    console = Console(width=100, file=io.StringIO())
    try:
        foo(1)
    except Exception:
        console.print_exception(max_frames=6)
    result = console.file.getvalue()
    print(result)
    assert "frames hidden" in result
    assert result.count("in foo") < 4
Exemplo n.º 6
0
def main(*args):
    enable_rich_traceback()

    commands = get_installed_commands()

    parser = argparse.ArgumentParser()
    parser.add_argument("--debug", default=False, action="store_true")
    parser.add_argument("--verbose", default=False, action="store_true")
    subparsers = parser.add_subparsers(dest="command")
    subparsers.required = True

    for cmd_name, cmd_class in commands.items():
        parser_kwargs = {}

        cmd_help = cmd_class.get_help()
        if cmd_help:
            parser_kwargs["help"] = cmd_help

        subparser = subparsers.add_parser(cmd_name, **parser_kwargs)
        cmd_class._add_arguments(subparser)

    args = parser.parse_args()

    logging.basicConfig(
        format="%(message)s",
        datefmt="[%X]",
        handlers=[RichHandler()],
        level=logging.DEBUG if args.verbose else logging.INFO,
    )

    if args.debug:
        import debugpy

        debugpy.listen(5678)
        debugpy.wait_for_client()

    console = Console()

    try:
        commands[args.command](args).handle()
    except exceptions.BarbariError as e:
        console.print(f"[red]{e}[/red]")
    except exceptions.BarbariUserError as e:
        console.print(f"[yellow]{e}[/yellow]")
    except Exception:
        console.print_exception()
Exemplo n.º 7
0
    def wrap(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except BaiduPCSError as err:
            logger.debug("`app`: BaiduPCSError: %s", traceback.format_exc())

            print(f"(v{__version__}) [bold red]ERROR[/bold red]: BaiduPCSError: {err}")
            if DEBUG:
                console = Console()
                console.print_exception()
        except Exception as err:
            logger.debug("`app`: System Error: %s", traceback.format_exc())

            print(f"(v{__version__}) [bold red]System ERROR[/bold red]: {err}")
            if DEBUG:
                console = Console()
                console.print_exception()
Exemplo n.º 8
0
def main(args=None):
    if args is None:
        args = sys.argv[1:]

    parser = argparse.ArgumentParser(
        epilog=get_command_list(),
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("command", type=str, nargs=1, choices=COMMANDS.keys())
    parser.add_argument("--loglevel", type=str, default="INFO")
    parser.add_argument("--traceback-locals", action="store_true")
    parser.add_argument("--debugger", action="store_true")
    args, extra = parser.parse_known_args()

    # Set up a simple console logger
    logging.basicConfig(level=args.loglevel)

    console = Console()

    if args.debugger:
        import debugpy

        console.print(
            "[blue]Awaiting debugger connection on 0.0.0.0:5678...[/blue]")
        debugpy.listen(("0.0.0.0", 5678))
        debugpy.wait_for_client()

    logging.basicConfig(
        level=args.loglevel,
        format="%(message)s",
        datefmt="[%X]",
        handlers=[RichHandler()],
    )

    try:
        if args.command[0] in COMMANDS:
            COMMANDS[args.command[0]]["function"](args, *extra)
    except Exception:
        console.print_exception(show_locals=args.traceback_locals)
        console.print(
            "[bold][red] An unexpected error occurred while processing your "
            "request; please create a bug issue at "
            "http://github.com/coddingtonbear/python-myfitnesspal/issues "
            "including the above traceback and a description of what "
            "you were trying to accomplish.[/red][/bold]")
Exemplo n.º 9
0
def main():
    from rich.logging import RichHandler

    logging.basicConfig(level="INFO",
                        format="%(message)s",
                        datefmt="[%X]",
                        handlers=[RichHandler(rich_tracebacks=True)])

    console = Console()
    obj = NanoContext(console)

    try:
        cli(obj=obj, show_default=True)
    except Exception as e:
        console.log("[bold red]Exception caught[/bold red]")
        if not obj.print_traceback:
            console.log(e)
        else:
            console.print_exception()
Exemplo n.º 10
0
def test_traceback_console_theme_applies():
    """
    Ensure that themes supplied via Console init work on Tracebacks.
    Regression test for https://github.com/Textualize/rich/issues/1786
    """
    r, g, b = 123, 234, 123
    console = Console(
        force_terminal=True,
        _environ={"COLORTERM": "truecolor"},
        theme=Theme({"traceback.title": f"rgb({r},{g},{b})"}),
    )

    console.begin_capture()
    try:
        1 / 0
    except Exception:
        console.print_exception()

    result = console.end_capture()

    assert f"\\x1b[38;2;{r};{g};{b}mTraceback \\x1b[0m" in repr(result)
Exemplo n.º 11
0
                cfg['order'] = start_order
            elif c == 'stop':
                cfg['order'] = start_order[::-1]

            json.dump(cfg, f, indent=4, sort_keys=True)

    console.log(f"Generating boot json file")
    with open(join(json_dir, 'boot.json'), 'w') as f:
        cfg = generate_boot(
            tp_sink_spec={
                "name": app_tp_sink,
                "host": "localhost",
                "port": 3333
            },
            readout_spec={
                "name": app_readout,
                "host": "localhost",
                "port": 3334
            },
        )
        json.dump(cfg, f, indent=4, sort_keys=True)
    console.log(f"MDAapp config generated in {json_dir}")


if __name__ == '__main__':

    try:
        cli(show_default=True, standalone_mode=True)
    except Exception as e:
        console.print_exception()
Exemplo n.º 12
0
class jobParser(cmd.Cmd):
    """sudo code
    fetch data from the db
    presents it in the cli
    asks how many job you want to apply to today
    check if you have applied to thet job already (whether that's not interreted or applied)
    asks if you want to apply
    - yes so opens a webpage to the job application, produces a cover letter, and updates the db to 
    - no updates the database and mark it as not interrested
    waits for your next command
    packages cmd/rich/pymongo"""
    intro = 'Welcome to the command line interactive data Structure program.\nType help or ? to list commands.\n'
    prompt = '(job engine) '

    def __init__(self):
        super(jobParser, self).__init__()
        self.console = Console()

    def do_mongoDBInfo(self, arg):
        """
        method where that will return a number of predetermined statistics about the number of job application in the db.
        """
        # types of collections
        # numbers of job you have yet to apply

        df = read_mongo(MONGO_DATABASE, MONGO_COLLECTION, {}, no_id=True)
        print(
            f'jobs not yet viewed {len(df[df["applied"].isin([False])])} Jobs viewed {len(df[df["applied"].isin([True])])}'
        )
        print(
            f'Out of the viewed jobs you rejected (so far): {len(df[df["filtered"].isin(["Rejected"])])} and applied to {len(df[df["filtered"].isin(["Applied"])])} '
        )

    def do_applyToJobs(self, line):
        """
        
        description:
            - method where you will pass an int
        """
        # fun stats
        numberOfApplication, collection = [arg for arg in line.split()]
        print(numberOfApplication, collection)
        applied = 0
        rejected = 0
        if not numberOfApplication or not numberOfApplication.isnumeric():
            print(f'you have entered {numberOfApplication} which is invalid')
            return
        query = {
            "$and": [{
                "site": 'stackOverflow'
            }, {
                'applied': False
            }]
        }  #we we should not filter per collection and instead per applied
        # fetching the info from the df and populating a dataFrame
        df = read_mongo(MONGO_DATABASE, MONGO_COLLECTION, query)
        # since mongo limit requires us to use a different synthax for the querry method it is easier to shrink the df
        df = df[:int(numberOfApplication)]
        for rowIndex, row in df.iterrows():
            print(f'job number: {rowIndex}')
            # printing the information
            for colIndex, col in row.iteritems():

                # we want to skip the unfiltered description
                # we could also drop the column from the df
                if colIndex == 'description':
                    pass

                else:
                    print(colIndex, col)

            answer = input("\nDo you want to apply for this job (y/n)?")
            if answer.lower() in ['y', 'yes']:
                # opens a webpage where job offer is hosted
                webbrowser.open_new_tab(row['url'])
                # need to integrate the cover letter
                df.at[rowIndex, 'applied'] = True
                # sometimes it is possible that the spider did not scrape the right item so we must give flexibility to the user
                doubleCheck = input(
                    "If the job posting did not fit what you set the spider to do do you want to continue with the job posting (y/n)?"
                )
                if doubleCheck.lower() in ['n', 'no']:
                    df.at[rowIndex, 'filtered'] = 'Rejected'
                    print(f'rejecting the job posting')
                    rejected += 1
                else:
                    df.at[rowIndex, 'filtered'] = 'Applied'
                    applied += 1
                    # git clone
                    # https://stackoverflow.com/questions/1911109/how-do-i-clone-a-specific-git-branch
                    coverLetterFilePath = os.path.join(
                        BASE_DIR, 'Cover-Letter-Generator/')

                    call(["python", coverLetterFilePath + 'main.py'])
                    # /lowriter --headless --convert-to pdf *.docx
                    call(["lowriter --headless --convert-to pdf *.docx"],
                         cwd=coverLetterFilePath + 'coverLetters/',
                         shell=True)
                    call(["rm *.docx"],
                         cwd=coverLetterFilePath + 'coverLetters/',
                         shell=True)
            # lowriter --headless --convert-to pdf *.docx && rm *.docx
            else:
                print(f'rejecting the job posting')
                rejected += 1
                df.at[rowIndex, 'filtered'] = 'Rejected'
                df.at[rowIndex, 'applied'] = True
        # db changes
        print(
            f'Done with the search. You have applied to {applied} jobs online and ignored {rejected} jobs'
        )
        print('updating mongo database')
        print(df)
        update_mongo(MONGO_DATABASE, MONGO_COLLECTION, df)

    def default(self, line):
        """
        Allows us to use the app as a normal commandline
        """
        try:
            return exec(line, globals())
        except:
            self.console.print_exception()

    def do_exit(self, arg):
        """
        exits the command line
        """
        exit()
Exemplo n.º 13
0
"""

Demonstrates Rich tracebacks for recursion errors.

Rich can exclude frames in the middle to avoid huge tracebacks.

"""

from rich.console import Console


def foo(n):
    return bar(n)


def bar(n):
    return foo(n)


console = Console()

try:
    foo(1)
except Exception:
    console.print_exception(max_frames=20)
class interactiveDataStructures(cmd.Cmd):
    """
    documentation: https://docs.python.org/3/library/cmd.html
    """
    intro = 'Welcome to the command line interactive data Structure program.\nType help or ? to list commands.\n'
    prompt = None


    def __init__(self, dataStructure):
        super(interactiveDataStructures, self).__init__()
        # rich module elements
        pretty.install()
        traceback.install()
        self.console = Console()
        # Datastructure elements
        availableDataStrutuces = {
            'DynamicArray': DynamicArray(),
            'SingleLinkedList': SinglyLinkedList(),
            'DoublyLinkedList': DoublyLinkedList(),
            'Stack': Stack(),
            'Queue': Queue(),
            'PriorityQueue': PriorityQueue()
            }

        correspondingNodes = {
            'DynamicArray': None,
            'SingleLinkedList': ListNode(None),
            'DoublyLinkedList': ListNode(None),
            'Stack': StackNode(None),
            'Queue': QueueNode(None),
            'PriorityQueue': PQNode(None)
            }

        if dataStructure in availableDataStrutuces:
            self.dataStructure = availableDataStrutuces[dataStructure]
            self.DSNode = correspondingNodes[dataStructure]
            self.DSname = dataStructure
            interactiveDataStructures.prompt = text.Text(f'({dataStructure}) ', style="bold magenta") # doesn't quite work
        else:
            raise ValueError(f'Please choose one of the following available data structure: {availableDataStrutuces.keys()}')


    def do_DSInfo(self,info=False):
        """
        print all non special method of the data structure that was selected
        """
        print(f'Info regarding the Class:{type(self.dataStructure).__name__}.__init__\n{self.dataStructure.__init__.__doc__}\n')
        print(f'Info regarding the Node used for the Class: {type(self.DSNode).__name__}.__init__\n{self.DSNode.__init__.__doc__}\n')
        print(f'The following methods for {type(self.dataStructure).__name__} are available:\n{vdir(self.dataStructure)}\n')
        if info:
            print(f'Detailled overview of each methods of {type(self.dataStructure).__name__}:\n{vdir(self.dataStructure,info=True)}')
        # print(f'If you want more info about what a specific {type(self.DSNode).__name__} method use')

    def do_manual(self,arg):
        """
        If you want to learn more about data structures
        """
        #taken from Django
        # BASE_DIR = Path(__file__).resolve().parent.parent
        # woah pathlib is amazing
        BASE_DIR = Path('../dataStructure.md')

        with open(BASE_DIR) as readme:
            markdown = Markdown(readme.read())
        self.console.print(markdown)

    def default(self, line): #! this is what I was looking for the exec() cmd
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        try:
            # https://stackoverflow.com/questions/7969949/whats-the-difference-between-globals-locals-and-vars
            # glo
            
            return exec(line, globals())

            # exec(print(str(line)))

        except:# Exception as e:
            self.console.print_exception()
            # print(f'{e.__class__}:{e}')


    def do_exit(self,arg):
        """
        exits the command line
        """
        exit()
Exemplo n.º 15
0
def extract(lines: List[str], nlp, use_dict=False, category=None):
    # extracting... 👇
    try:
        if category == "noun":
            terms: Iterator[str] = extract_by_lines(lines, nlp,
                                                    extract_noun_by_line)
            result = count_terms(terms, use_dict)
            yield from result
        if category == "verb":
            terms: Iterator[str] = extract_by_lines(lines, nlp,
                                                    extract_verb_by_line)
            result = count_terms(terms, use_dict)
            yield from result
        elif category == "np":
            terms: Iterator[str] = extract_by_lines(lines, nlp,
                                                    extract_np_by_line)
            result = count_terms(terms, use_dict)
            yield from result
        elif category == "vp":
            terms: Iterator[str] = extract_by_lines(lines, nlp,
                                                    extract_vb_by_line)
            result = count_terms(terms, use_dict)
            yield from result
        elif category == "pp":
            terms: Iterator[str] = extract_by_lines(lines, nlp,
                                                    extract_pp_by_line)
            result = count_terms(terms, use_dict)
            yield from result
        elif category == "abbr":
            if nlp.pipe_names[-1] == "AbbreviationDetector":
                pass
            else:
                from .vendor.abbr import AbbreviationDetector
                abbreviation_pipe = AbbreviationDetector(nlp)
                nlp.add_pipe(abbreviation_pipe)
            terms: Iterator[str] = extract_by_lines(lines, nlp,
                                                    extract_abbr_by_line)
            return count_terms(terms, use_dict)
        elif category == "compound-complex":
            result = extract_sentences_by_lines(lines, nlp, extract_cc_by_line)
            yield from sorted(result, key=lambda entry: entry[1], reverse=True)
        elif category == "inversion":
            result = extract_sentences_by_lines(lines, nlp,
                                                extract_inversion_by_line)
            yield from result
        elif category == "imperative":
            result = extract_sentences_by_lines(lines, nlp,
                                                extract_imperative_by_line)
            yield from result
        elif category == "passive":
            result = extract_sentences_by_lines(lines, nlp,
                                                extract_passive_by_line)
            yield from result
        elif category == "condition":
            result = extract_sentences_by_lines(lines, nlp,
                                                extract_condition_by_line)
            yield from result
        elif category == "infinitive":
            result = extract_sentences_by_lines(lines, nlp,
                                                extract_infinitive_by_line)
            yield from result
    except:
        from rich.console import Console
        console = Console()
        console.print_exception()
Exemplo n.º 16
0
        "-f",
        "--filter",
        type=str,
        help="Execute the check only for directories containing this string.",
        default="")
    args = parser.parse_args()
    result = False
    try:
        checker = ComponentGenerationChecker()

        result = checker.check_components_generation(
            args.test_folder, args.dry_run, args.dirty, args.generate_only,
            args.no_execution, args.filter, args.avoid, args.clean)

    except (KeyboardInterrupt, SystemExit):
        console.log("\nExiting in the middle of the execution.", style="red")
        console.log("Some files will be left on the directories.",
                    style="yellow")
        console.log("Use -c option to clean all the generated files.",
                    style="yellow")
        sys.exit(-1)
    except Exception as e:
        console.log("Unexpected exception: %s" % e, style="red")
        console.print_exception(show_locals=True)
        sys.exit(-1)
    finally:
        if result:
            sys.exit(0)
        else:
            sys.exit(-1)
Exemplo n.º 17
0
        f"git remote add origin https://github.com/{login}/{PROJECTNAME}.git")
    commands.append("git push -u origin master")

# Global actions
try:
    # Move files to destination
    for fileobj in os.scandir(path.join(LOCAL, "toClone")):
        shutil.copy2(
            path.join(fileobj.path),
            path.join(NEWFOLDER, fileobj.name.replace("template", "")),
        )

    # Change to destination folder
    os.chdir(NEWFOLDER)

    if args.python:
        open(path.join(NEWFOLDER, "main.py"), "a").close()

    # Create project
    for c in commands:
        os.system(c)

    print(f"{PROJECTNAME} created")
except:
    c.print_exception()

finally:
    c.print("Done!")
    os.system("code .")  # Open edtor
    c.print(f"cd {NEWFOLDER}")
Exemplo n.º 18
0
class Shell:
    """Shell is wrapper for Plumbum Commands Machine

    To use:
    >>> sh = Shell()
    >>> sh.check_command("python")
    True
    >>> sh.cmd["python"]["-c"]("print(123)")
    '123\n'
    """
    @property
    def name(self) -> str:
        """ Name """
        return self.__class__.__name__.lower()

    def __init__(self, executor: Machine = LocalMachine()):
        self.logger = logger.getChild(self.__class__.__name__)
        self.exec = executor
        self.console = Console()

    def is_sudo(self) -> bool:
        """ Check is this shell session is sudo """
        self.logger.warning("is_sudo() not support by Shell!")
        return False

    def is_installed(self) -> bool:
        return True

    def which(self, command_name: str) -> Machine:
        return self.exec.which(command_name)

    def check_command(self, command_name):
        try:
            self.which(command_name)
            return True
        except CommandNotFound:
            return False

    @property
    def cmd(self) -> Machine:
        return self.exec

    def get_sudo(self) -> bool:
        """ Elevate the privileges of the current  shell session

        To use:
        >>> sh = Shell()
        >>> sh.is sudo()
        False
        >>> sh.get_sudo()
        True
        Returns:
            bool: is sudo
        """
        sudo = self.exec["sudo"]
        try:
            sudo["ls"]()
            return True
        except ProcessExecutionError as e:
            if ("no password" in e.stderr) or ("-S" in e.stderr):
                __pass = getpass("Sudo password: "******"--stdin"]["ls"].popen(stdin=PIPE)
                out = p.communicate(f"{__pass}\n".encode(), timeout=2)
                return True
            else:
                self.console.print_exception()

        return self.is_sudo()

    @property
    def sudo_cmd(self) -> BoundCommand:
        sudo = self.exec["sudo"]
        return sudo[self.cmd]