def __init__(self, locals=None, formatter="dark", background=None): # noqa """This class builds upon Python's code.InteractiveConsole so as to provide friendly tracebacks. It keeps track of code fragment executed by treating each of them as an individual source file. """ _ = current_lang.translate friendly.exclude_file_from_traceback(codeop.__file__) self.fake_filename = "<friendly-console:%d>" self.counter = 1 self.old_locals = {} self.saved_builtins = {} for name in dir(builtins): self.saved_builtins[name] = getattr(builtins, name) self.rich_console = False friendly.set_formatter(formatter, background=background) if formatter in ["dark", "light"]: self.rich_console = session.console if formatter == "dark": self.prompt_color = "[bold bright_green]" else: self.prompt_color = "[bold dark_violet]" super().__init__(locals=locals) self.check_for_builtins_changes() self.check_for_annotations()
def bw(): """Black and White theme for Mu's REPL. This is similar to Mu's high contrast theme.""" set_formatter("bw", color_system="truecolor", force_jupyter=False, background="#000000")
def ft(): """ft = Friendly Theme Mu's REPL (high contrast). This uses the standard colours for Friendly with dark consoles. """ set_formatter("dark", color_system="truecolor", force_jupyter=False, background="#000000")
def install(lang="en"): """Installs Friendly in the IDLE shell, with a custom formatter. For Python versions before 3.10, this is not directly supported, so a Friendly console is used instead of IDLE's shell. """ sys.stderr = sys.stdout.shell # noqa friendly.set_formatter(idle_formatter) if sys.version_info >= (3, 10): install_in_idle_shell(lang=lang) else: idle_writer("Friendly cannot be installed in this version of IDLE.\n") idle_writer("Using Friendly's own console instead.\n") start_console(lang=lang)
def install(lang="en"): """Installs Friendly in the IDLE shell, with a custom formatter. For Python versions before 3.10, this was not directly supported, so a Friendly console is used instead of IDLE's shell. Changes introduced in Python 3.10 were back-ported to Python 3.9.5. """ sys.stderr = sys.stdout.shell # noqa friendly.set_formatter(_idle_formatter.idle_formatter) if sys.version_info >= (3, 9, 5): install_in_idle_shell(lang=lang) sys.displayhook = _displayhook else: idle_writer("Friendly cannot be installed in this version of IDLE.\n") idle_writer("Using Friendly's own console instead.\n") start_console(lang=lang, displayhook=_displayhook)
import os import sys import platform this_dir = os.path.dirname(__file__) sys.path.append(os.path.join(this_dir, "..")) import friendly # Make it possible to find docs and tests source docs_root_dir = os.path.abspath( os.path.join(this_dir, "..", "..", "friendly-traceback-docs")) assert os.path.isdir(docs_root_dir), "Separate docs repo need to exist" LANG = "en" friendly.install() friendly.set_lang(LANG) friendly.set_formatter("docs") sys.path.insert(0, this_dir) py_version = f"{sys.version_info.major}.{sys.version_info.minor}" import trb_syntax_common target = os.path.normpath( os.path.join(docs_root_dir, f"docs/source/syntax_tracebacks_{LANG}_{py_version}.rst")) intro_text = """ Friendly SyntaxError tracebacks - in English ============================================= Friendly aims to provide friendlier feedback when an exception
# is correct! import os import sys import platform import friendly # Make it possible to find docs and tests source this_dir = os.path.dirname(__file__) docs_root_dir = os.path.abspath( os.path.join(this_dir, "..", "..", "friendly-traceback-docs")) assert os.path.isdir(docs_root_dir), "Separate docs repo need to exist" friendly.install() friendly.set_lang("en") friendly.set_formatter("markdown_docs") sys.path.insert(0, this_dir) py_version = f"{sys.version_info.major}.{sys.version_info.minor}" import trb_common target = os.path.normpath( os.path.join(docs_root_dir, "docs/source/tracebacks_markdown.md")) intro_text = """ # Friendly tracebacks - markdown_docs format <div class="admonition note"> <p class="admonition-title">Note</p> <p>The content of this page is generated by running
def run(filename, lang=None, include="friendly_tb", args=None, console=True): """This function executes the code found in a python file. ``filename`` should be either an absolute path or, it should be the name of a file (filename.py) found in the same directory as the file from which ``run()`` is called. If friendly_console is set to ``False`` (the default) and the Python version is greater or equal to 3.10, ``run()`` returns an empty dict if a ``SyntaxError`` was raised, otherwise returns the dict in which the module (``filename``) was executed. If console is set to ``True`` (the default), the execution continues as an interactive session in a Friendly console, with the module dict being used as the locals dict. Other arguments include: ``lang``: language used; currently only ``en`` (default) and ``fr`` are available. ``include``: specifies what information is to be included if an exception is raised. ``args``: strings tuple that is passed to the program as though it was run on the command line as follows:: python filename.py arg1 arg2 ... """ _ = current_lang.translate sys.stderr = sys.stdout.shell # noqa friendly.set_formatter(idle_formatter) friendly.set_stream(idle_writer) filename = Path(filename) if not filename.is_absolute(): frame = inspect.stack()[1] # This is the file from which run() is called run_filename = Path(frame[0].f_code.co_filename) run_dir = run_filename.parent.absolute() filename = run_dir.joinpath(filename) if not filename.exists(): print(_("The file {filename} does not exist.").format(filename=filename)) return if not console: if sys.version_info >= (3, 10): install_in_idle_shell() elif sys.version_info < (3, 10): sys.stderr.write("Friendly cannot be installed in this version of IDLE.\n") return friendly.run( filename, lang=lang, include=include, args=args, console=console, formatter=idle_formatter, )
def night(): """Night theme for Mu's REPL""" set_formatter("dark", color_system="truecolor", force_jupyter=False, background="#373737")
def day(): """Day theme for Mu's REPL""" set_formatter("light", color_system="truecolor", force_jupyter=False, background="#FEFEF7")
out_file = f"data_{major}_{minor}.py" results = {"version": (major, minor)} def _formatter(info, include=None): items = {"cause": None} for key in info: if key in ("message", "parsing_error_source", "cause"): if key in info: items[key] = info[key] return str(items) # formatter expect a string friendly.set_formatter(formatter=_formatter) info = {} for filename in catch_syntax_error.descriptions: try: exec("import %s" % filename) except Exception: friendly.explain_traceback(redirect="capture") out = eval(friendly.get_output()) # convert back to dict. info[filename] = out with open(out_file, "w", encoding="utf8") as f: f.write("info = ") pprint.pprint(info, stream=f, width=100)