Esempio n. 1
0
def get_stringifier(iinfo: InspectInfo) -> Callable:
    """
    :return: a function that turns an object into a Unicode text object.
    """
    try:
        return STRINGIFIERS[iinfo.display_type]
    except KeyError:
        try:
            if not custom_stringifier_dict:  # Only execfile once
                from os.path import expanduser
                custom_stringifier_fname = expanduser(iinfo.display_type)
                with open(custom_stringifier_fname) as inf:
                    exec(compile(inf.read(), custom_stringifier_fname, "exec"),
                         custom_stringifier_dict, custom_stringifier_dict)
        except Exception:
            ui_log.exception("Error when importing custom stringifier")
            return error_stringifier
        else:
            if "pudb_stringifier" not in custom_stringifier_dict:
                ui_log.error(
                    f"{iinfo.display_type} does not contain a function "
                    "named pudb_stringifier at the module level.")
                return lambda value: str(
                    "ERROR: Invalid custom stringifier file: "
                    "pudb_stringifer not defined.")
            else:
                return (lambda value: str(custom_stringifier_dict[
                    "pudb_stringifier"](value)))
Esempio n. 2
0
 def entries(cls, collection, label: str):
     """
     :yield: ``(label, entry, id_path_ext)`` tuples for each entry in the
     collection.
     """
     assert isinstance(collection, cls)
     try:
         for count, entry in enumerate(collection):
             yield None, entry, "[{k:d}]".format(k=count)
     except (AttributeError, TypeError) as error:
         ui_log.error("Object {l!r} appears to be a collection, but does "
                      "not behave like one: {m}".format(l=label, m=error))
Esempio n. 3
0
 def entries(cls, sequence, label: str):
     """
     :yield: ``(label, entry, id_path_ext)`` tuples for each entry in the
     sequence.
     """
     assert isinstance(sequence, cls)
     try:
         for count, entry in enumerate(sequence):
             yield str(count), entry, f"[{count:d}]"
     except (AttributeError, TypeError) as error:
         ui_log.error("Object {l!r} appears to be a sequence, but does "
                      "not behave like one: {m}".format(l=label, m=error))
Esempio n. 4
0
 def entries(cls, mapping, label: str):
     """
     :yield: ``(label, entry, id_path_ext)`` tuples for each entry in the
     mapping.
     """
     assert isinstance(mapping, cls)
     try:
         for key in mapping.keys():
             key_repr = cls._safe_key_repr(key)
             yield (key_repr, mapping[key], f"[{key_repr}]")
     except (AttributeError, TypeError) as error:
         ui_log.error("Object {l!r} appears to be a mapping, but does "
                      "not behave like one: {m}".format(l=label, m=error))
Esempio n. 5
0
def get_palette(may_use_fancy_formats: bool, theme: str = "classic") -> list:
    """
    Load the requested theme and return a list containing all palette entries
    needed to highlight the debugger UI, including syntax highlighting.
    """
    inheritance_overrides = {}

    if may_use_fancy_formats:

        def add_setting(color, setting):
            return f"{color}, {setting}"
    else:

        def add_setting(color, setting):
            return color

    def link(child: str, parent: str):
        inheritance_overrides[child] = parent

    # {{{ themes

    if theme == "classic":
        # {{{ classic theme
        link("current breakpoint", "current frame name")
        link("focused current breakpoint", "focused current frame name")

        palette_dict = {
            # {{{ base styles
            "background": ("black", "light gray"),
            "selectable": ("black", "dark cyan"),
            "focused selectable": ("black", "light cyan"),
            "highlighted": ("dark blue", "yellow"),
            "hotkey": (add_setting("black", "underline"), "light gray"),
            # }}}
            # {{{ general ui
            "header": ("dark blue", "light gray"),
            "dialog title": (add_setting("white", "bold"), "dark blue"),
            "warning": (add_setting("white", "bold"), "dark red"),
            # }}}
            # {{{ source view
            "source": ("yellow", "dark blue"),
            "current source": ("dark blue", "dark green"),
            "breakpoint source": (add_setting("yellow", "bold"), "dark red"),
            "line number": ("light gray", "dark blue"),
            "breakpoint marker": (add_setting("dark red",
                                              "bold"), "dark blue"),
            # }}}
            # {{{ sidebar
            "sidebar two": ("dark blue", "dark cyan"),
            "sidebar three": ("dark gray", "dark cyan"),
            "focused sidebar two": ("dark blue", "light cyan"),
            "focused sidebar three": ("dark gray", "light cyan"),
            # }}}
            # {{{ variables view
            "return label": ("white", "dark blue"),
            "focused return label": ("light gray", "dark blue"),
            # }}}
            # {{{ stack
            "current frame name": (add_setting("white", "bold"), "dark cyan"),
            "focused current frame name": (add_setting("black",
                                                       "bold"), "light cyan"),
            # }}}
            # {{{ shell
            "command line output": ("light cyan", "dark blue"),
            "command line prompt": (add_setting("white", "bold"), "dark blue"),
            "command line error": (add_setting("light green",
                                               "bold"), "dark blue"),
            "command line clear button": (add_setting("white",
                                                      "bold"), "dark blue"),
            "command line focused button": ("dark blue", "dark cyan"),
            # }}}
            # {{{ Code syntax
            "keyword": (add_setting("white", "bold"), "dark blue"),
            "function": ("light cyan", "dark blue"),
            "literal": (add_setting("light green", "bold"), "dark blue"),
            "punctuation": ("light gray", "dark blue"),
            "comment": ("dark cyan", "dark blue"),
            # }}}
        }
        # }}}
    elif theme == "vim":
        # {{{ vim theme
        link("current breakpoint", "current frame name")
        link("focused current breakpoint", "focused current frame name")

        palette_dict = {
            # {{{ base styles
            "background": ("black", "light gray"),
            "selectable": ("black", "dark cyan"),
            "focused selectable": ("black", "light cyan"),
            "hotkey": (add_setting("black", "bold, underline"), "light gray"),
            "highlighted": ("black", "yellow"),
            # }}}
            # {{{ general ui
            "header": (add_setting("black", "bold"), "light gray"),
            "group head": ("dark blue", "light gray"),
            "dialog title": (add_setting("white", "bold"), "dark blue"),
            "input": ("black", "dark cyan"),
            "focused input": ("black", "light cyan"),
            "warning": (add_setting("dark red", "bold"), "white"),
            "header warning": (add_setting("dark red", "bold"), "light gray"),
            # }}}
            # {{{ source view
            "source": ("black", "white"),
            "current source": ("black", "dark cyan"),
            "breakpoint source": ("dark red", "light gray"),
            "line number": ("dark gray", "white"),
            "current line marker": ("dark red", "white"),
            "breakpoint marker": ("dark red", "white"),
            # }}}
            # {{{ sidebar
            "sidebar one": ("black", "dark cyan"),
            "sidebar two": ("dark blue", "dark cyan"),
            "sidebar three": ("dark gray", "dark cyan"),
            "focused sidebar one": ("black", "light cyan"),
            "focused sidebar two": ("dark blue", "light cyan"),
            "focused sidebar three": ("dark gray", "light cyan"),
            # }}}
            # {{{ variables view
            "highlighted var label": ("dark blue", "yellow"),
            "return label": ("white", "dark blue"),
            "focused return label": ("light gray", "dark blue"),
            # }}}
            # {{{ stack
            "current frame name": (add_setting("white", "bold"), "dark cyan"),
            "focused current frame name": (add_setting("black",
                                                       "bold"), "light cyan"),
            # }}}
            # {{{ shell
            "command line output": (add_setting("dark gray", "bold"), "white"),
            # }}}
            # {{{ Code syntax
            "keyword2": ("dark magenta", "white"),
            "namespace": ("dark magenta", "white"),
            "literal": ("dark red", "white"),
            "exception": ("dark red", "white"),
            "comment": ("dark gray", "white"),
            "function": ("dark blue", "white"),
            "pseudo": ("dark gray", "white"),
            "builtin": ("light blue", "white"),
            # }}}
        }
        # }}}
    elif theme == "dark vim":
        # {{{ dark vim

        link("current breakpoint", "current frame name")
        link("focused current breakpoint", "focused current frame name")
        palette_dict = {
            # {{{ base styles
            "background": ("black", "light gray"),
            "selectable": ("white", "dark gray"),
            "focused selectable": (add_setting("white", "bold"), "light blue"),
            "highlighted": ("black", "dark green"),
            "hotkey": (add_setting("dark blue", "underline"), "light gray"),
            # }}}
            # {{{ general ui
            "header": ("dark blue", "light gray"),
            "dialog title": (add_setting("white", "bold"), "black"),
            "warning": (add_setting("light red", "bold"), "black"),
            "header warning": (add_setting("light red", "bold"), "light gray"),
            # }}}
            # {{{ source view
            "source": ("white", "black"),
            "current source": (add_setting("white", "bold"), "dark gray"),
            "line number": (add_setting("dark gray", "bold"), "black"),
            "breakpoint marker": (add_setting("light red", "bold"), "black"),
            "breakpoint source": (add_setting("white", "bold"), "dark red"),
            # }}}
            # {{{ sidebar
            "sidebar two": ("yellow", "dark gray"),
            "focused sidebar two": ("light cyan", "light blue"),
            "sidebar three": ("light gray", "dark gray"),
            "focused sidebar three": ("yellow", "light blue"),
            # }}}
            # {{{ stack
            "current frame name": (add_setting("white", "bold"), "dark gray"),
            # }}}
            # {{{ shell
            "command line output": (add_setting("yellow", "bold"), "black"),
            # }}}
            # {{{ Code syntax
            "keyword": ("yellow", "black"),
            "literal": ("light magenta", "black"),
            "function": (add_setting("light cyan", "bold"), "black"),
            "punctuation": ("yellow", "black"),
            "comment": ("dark cyan", "black"),
            "exception": ("light red", "black"),
            "builtin": ("light green", "black"),
            "pseudo": ("dark green", "black"),
            # }}}
        }
        # }}}
    elif theme == "midnight":
        # {{{ midnight

        # Based on XCode's midnight theme
        # Looks best in a console with green text against black background
        link("current breakpoint", "current frame name")
        link("focused current breakpoint", "focused current frame name")
        palette_dict = {
            # {{{ base styles
            "background": ("black", "light gray"),
            "selectable": ("light gray", "black"),
            "focused selectable": ("black", "light green"),
            "hotkey": (add_setting("black", "underline"), "light gray"),
            "highlighted": ("black", "brown"),
            # }}}
            # {{{ general ui
            "focused sidebar": (add_setting("black", "bold"), "light gray"),
            "group head": (add_setting("black", "bold"), "light gray"),
            "warning": (add_setting("light red", "bold"), "black"),
            "header warning": (add_setting("light red", "bold"), "light gray"),
            "dialog title": ("black", "dark green"),
            "fixed value": ("white", "dark gray"),
            # }}}
            # {{{ source view
            "current source": ("black", "dark cyan"),
            "breakpoint source": ("white", "dark gray"),
            "line number": (add_setting("dark gray", "bold"), "black"),
            "breakpoint marker": (add_setting("light red", "bold"), "black"),
            "current line marker": ("dark cyan", "black"),
            # }}}
            # {{{ sidebar
            "sidebar two": ("dark green", "black"),
            "sidebar three": ("yellow", "black"),
            "focused sidebar three": ("dark gray", "light green"),
            # }}}
            # {{{ variables view
            "return label": ("black", "dark green"),
            "focused return label": ("black", "dark green"),
            # }}}
            # {{{ stack
            "current frame name": (add_setting("light gray", "bold"), "black"),
            # }}}
            # {{{ shell
            "command line prompt": (add_setting("white", "bold"), "black"),
            "command line output": ("yellow", "black"),
            "command line input": (add_setting("white", "bold"), "black"),
            # }}}
            # {{{ Code syntax
            "keyword": ("dark magenta", "black"),
            "keyword2": (add_setting("light magenta", "bold"), "black"),
            "pseudo": ("dark magenta", "black"),
            "function": (add_setting("light cyan", "bold"), "black"),
            "builtin": ("dark cyan", "black"),
            "literal": (add_setting("light blue", "bold"), "black"),
            "string": ("light red", "black"),
            "exception": (add_setting("light red", "bold"), "black"),
            "comment": ("dark green", "black"),
            # }}}
        }

        # }}}
    elif theme == "solarized":
        # {{{ solarized
        palette_dict = {
            # {{{ base styles
            "background": ("light green", "light gray"),
            "selectable": ("light green", "white"),
            "focused selectable": ("white", "dark blue"),
            "highlighted": ("white", "dark cyan"),
            "hotkey": (add_setting("black", "underline"), "light gray"),
            # }}}
            # {{{ general ui
            "dialog title": (add_setting("white", "bold"), "dark cyan"),
            "warning": (add_setting("light red", "bold"), "white"),
            "header warning": (add_setting("light red", "bold"), "light gray"),
            "focused sidebar": ("dark red", "light gray"),
            "group head": (add_setting("yellow", "bold"), "light gray"),
            # }}}
            # {{{ source view
            "source": ("yellow", "white"),
            "breakpoint source": ("light red", "light gray"),
            "current source": ("light gray", "light blue"),
            "line number": ("light blue", "white"),
            "current line marker": (add_setting("light blue",
                                                "bold"), "white"),
            "breakpoint marker": (add_setting("light red", "bold"), "white"),
            # }}}
            # {{{ sidebar
            "sidebar two": ("dark blue", "white"),
            "sidebar three": ("light cyan", "white"),
            "focused sidebar three": ("light gray", "dark blue"),
            # }}}
            # {{{ variables view
            "return label": ("white", "yellow"),
            "focused return label": ("white", "yellow"),
            # }}}
            # {{{ stack
            "current frame name": (add_setting("light green",
                                               "bold"), "white"),
            "focused current frame name": (add_setting("white",
                                                       "bold"), "dark blue"),
            # }}}
            # {{{ shell
            "command line output": ("light green", "white"),
            # }}}
            # {{{ Code syntax
            "namespace": ("dark red", "white"),
            "exception": ("light red", "white"),
            "keyword": ("brown", "white"),
            "keyword2": ("dark magenta", "white"),
            "function": ("dark green", "white"),
            "literal": ("dark cyan", "white"),
            "builtin": ("dark blue", "white"),
            "comment": ("light cyan", "white"),
            "pseudo": ("light cyan", "white"),
            # }}}
        }

    # }}}
    elif theme == "agr-256":
        # {{{ agr-256

        # Give the colors some comprehensible names
        black = "h235"
        blacker = "h233"
        dark_cyan = "h24"
        dark_gray = "h241"
        dark_green = "h22"
        dark_red = "h88"
        dark_teal = "h23"
        light_blue = "h111"
        light_cyan = "h80"
        light_gray = "h252"
        light_green = "h113"
        light_red = "h160"
        medium_gray = "h246"
        salmon = "h223"
        orange = "h173"
        white = "h255"
        yellow = "h192"

        link("focused breakpoint", "focused selectable")
        link("current breakpoint", "current frame name")
        link("focused current breakpoint", "focused current frame name")
        palette_dict = {
            # {{{ base styles
            "background": (black, light_gray),
            "selectable": (white, blacker),
            "focused selectable": (yellow, dark_cyan),
            "hotkey": (add_setting(black, "underline"), light_gray),
            "highlighted": (white, dark_green),
            # }}}
            # {{{ general ui
            "focused sidebar": (dark_cyan, light_gray),
            "group head": (add_setting(dark_cyan, "bold"), light_gray),
            "dialog title": (add_setting(light_gray, "bold"), black),
            "warning": (add_setting(white, "bold"), dark_red),
            "fixed value": (add_setting(white, "bold"), dark_gray),
            "button": (add_setting(white, "bold"), black),
            "focused button": (add_setting(yellow, "bold"), dark_cyan),
            # }}}
            # {{{ source view
            "line number": (dark_gray, black),
            "current line marker": (add_setting(yellow, "bold"), black),
            "breakpoint marker": (add_setting(light_red, "bold"), black),
            "source": (white, black),
            "breakpoint source": (add_setting(white, "bold"), dark_red),
            "current source": (add_setting(light_gray, "bold"), dark_teal),
            # }}}
            # {{{ sidebar
            "sidebar two": (light_blue, blacker),
            "focused sidebar two": (light_gray, dark_cyan),
            "sidebar three": (medium_gray, blacker),
            "focused sidebar three": (salmon, dark_cyan),
            # }}}
            # {{{ variables view
            "highlighted var label": (light_gray, dark_green),
            "return label": (light_green, blacker),
            "focused return label": (add_setting(light_gray,
                                                 "bold"), dark_cyan),
            # }}}
            # {{{ stack
            "current frame name": (yellow, blacker),
            "focused current frame name": (add_setting(yellow,
                                                       "bold"), dark_cyan),
            # }}}
            # {{{ shell
            "command line prompt": (add_setting(yellow, "bold"), black),
            "command line output": (light_cyan, black),
            "command line error": (light_red, black),
            # }}}
            # {{{ Code syntax
            "comment": (medium_gray, black),
            "exception": (orange, black),
            "function": (yellow, black),
            "keyword": (light_blue, black),
            "literal": (orange, black),
            "operator": (yellow, black),
            "pseudo": (medium_gray, black),
            "punctuation": (salmon, black),
            "string": (light_green, black),
            # }}}
        }
        # }}}
    elif theme == "monokai":
        # {{{ monokai
        link("current breakpoint", "current frame name")
        link("focused current breakpoint", "focused current frame name")
        palette_dict = {
            # {{{ base styles
            "background": ("black", "light gray"),
            "selectable": ("white", "black"),
            "focused selectable": ("white", "dark gray"),
            "highlighted": ("black", "dark green"),
            "hotkey": (add_setting("black", "underline"), "light gray"),
            # }}}
            # {{{ general ui
            "input": ("white", "black"),
            "button": (add_setting("white", "bold"), "black"),
            "focused button": (add_setting("white", "bold"), "dark gray"),
            "focused sidebar": ("dark blue", "light gray"),
            "warning": (add_setting("white", "bold"), "dark red"),
            "group head": (add_setting("black", "bold"), "light gray"),
            "dialog title": (add_setting("white", "bold"), "black"),
            # }}}
            # {{{ source view
            "current source": ("black", "dark cyan"),
            "breakpoint source": (add_setting("white", "bold"), "dark red"),
            "line number": ("dark gray", "black"),
            "current line marker": (add_setting("dark cyan", "bold"), "black"),
            "breakpoint marker": (add_setting("dark red", "bold"), "black"),
            # }}}
            # {{{ sidebar
            "sidebar two": ("light cyan", "black"),
            "focused sidebar two": ("light cyan", "dark gray"),
            "sidebar three": ("light magenta", "black"),
            "focused sidebar three": ("light magenta", "dark gray"),
            # }}}
            # {{{ variables view
            "return label": ("light green", "black"),
            "focused return label": ("light green", "dark gray"),
            # }}}
            # {{{ stack
            "current frame name": ("light green", "black"),
            "focused current frame name": ("light green", "dark gray"),
            # }}}
            # {{{ shell
            "command line prompt": (add_setting("yellow", "bold"), "black"),
            "command line output": ("light cyan", "black"),
            "command line error": ("yellow", "black"),
            "focused command line output": ("light cyan", "dark gray"),
            "focused command line error": (add_setting("yellow",
                                                       "bold"), "dark gray"),
            # }}}
            # {{{ Code syntax
            "literal": ("light magenta", "black"),
            "builtin": ("light cyan", "black"),
            "exception": ("light cyan", "black"),
            "keyword2": ("light cyan", "black"),
            "function": ("light green", "black"),
            "class": (add_setting("light green", "underline"), "black"),
            "keyword": ("light red", "black"),
            "operator": ("light red", "black"),
            "comment": ("dark gray", "black"),
            "docstring": ("dark gray", "black"),
            "argument": ("brown", "black"),
            "pseudo": ("brown", "black"),
            "string": ("yellow", "black"),
            # }}}
        }

        # }}}
    elif theme == "monokai-256":
        # {{{ monokai-256

        # Give the colors some comprehensible names
        black = "h236"
        blacker = "h234"
        dark_gray = "h240"
        dark_green = "h28"
        dark_red = "h124"
        dark_teal = "h30"
        dark_magenta = "h141"
        light_blue = "h111"
        light_cyan = "h51"
        light_gray = "h252"
        light_green = "h155"
        light_red = "h160"
        light_magenta = "h198"
        medium_gray = "h243"
        orange = "h208"
        white = "h255"
        yellow = "h228"
        link("current breakpoint", "current frame name")
        link("focused current breakpoint", "focused current frame name")
        palette_dict = {

            # {{{ base styles
            "background": (black, light_gray),
            "selectable": (white, blacker),
            "focused selectable": (white, dark_gray),
            "highlighted": (white, dark_green),
            "hotkey": (add_setting(black, "underline"), light_gray),
            # }}}
            # {{{ general ui
            "input": (white, black),
            "button": (add_setting(white, "bold"), black),
            "focused button": (add_setting(white, "bold"), dark_gray),
            "focused sidebar": (dark_teal, light_gray),
            "warning": (add_setting(white, "bold"), dark_red),
            "group head": (add_setting(black, "bold"), light_gray),
            "dialog title": (add_setting(white, "bold"), blacker),
            # }}}
            # {{{ source view
            "source": (white, black),
            "current source": (add_setting(light_gray, "bold"), dark_teal),
            "breakpoint source": (add_setting(white, "bold"), dark_red),
            "line number": (dark_gray, black),
            "current line marker": (add_setting(light_cyan, "bold"), black),
            "breakpoint marker": (add_setting(light_red, "bold"), black),
            # }}}
            # {{{ sidebar
            "sidebar two": (light_cyan, blacker),
            "focused sidebar two": (light_cyan, dark_gray),
            "sidebar three": (dark_magenta, blacker),
            "focused sidebar three": (dark_magenta, dark_gray),
            # }}}
            # {{{ variables view
            "highlighted var label": (light_gray, dark_green),
            "return label": (light_green, blacker),
            "focused return label": (light_green, dark_gray),
            # }}}
            # {{{ stack
            "current frame name": (light_green, blacker),
            "focused current frame name": (light_green, dark_gray),
            # }}}
            # {{{ shell
            "command line prompt": (add_setting(yellow, "bold"), black),
            "command line output": (light_cyan, black),
            "command line error": (orange, black),
            "focused command line output": (light_cyan, dark_gray),
            "focused command line error": (add_setting(orange,
                                                       "bold"), dark_gray),
            # }}}
            # {{{ Code syntax
            "literal": (dark_magenta, black),
            "builtin": (light_cyan, black),
            "exception": (light_cyan, black),
            "keyword2": (light_cyan, black),
            "function": (light_green, black),
            "class": (add_setting(light_green, "underline"), black),
            "keyword": (light_magenta, black),
            "operator": (light_magenta, black),
            "comment": (medium_gray, black),
            "docstring": (medium_gray, black),
            "argument": (orange, black),
            "pseudo": (orange, black),
            "string": (yellow, black),
            # }}}
        }
        # }}}
    elif theme == "mono":
        # {{{ mono
        palette_dict = {
            "background": ("standout", ),
            "selectable": (),
            "focused selectable": ("underline", ),
            "highlighted": ("bold", ),
            "hotkey": ("underline, standout", ),
        }
        # }}}
    else:
        # {{{ custom
        try:
            # {{{ base styles
            palette_dict = {
                "background": ("black", "light gray"),
                "hotkey": (add_setting("black", "underline"), "light gray"),
                "selectable": ("black", "dark cyan"),
                "focused selectable": ("black", "dark green"),
                "input": (add_setting("yellow", "bold"), "dark blue"),
                "warning": (add_setting("white", "bold"), "dark red"),
                "highlighted": ("white", "dark cyan"),
                "source": ("white", "dark blue"),
            }
            # }}}

            symbols = {
                "palette": palette_dict,
                "add_setting": add_setting,
                "link": link,
            }

            from os.path import expanduser, expandvars
            fname = expanduser(expandvars(theme))
            with open(fname) as inf:
                exec(compile(inf.read(), fname, "exec"), symbols)
        except FileNotFoundError:
            ui_log.error(
                "Unable to locate custom theme file {!r}".format(theme))
            return None
        except Exception:
            ui_log.exception("Error when importing theme:")
            return None
        # }}}

    # }}}

    # Apply style inheritance
    for style_name in set(INHERITANCE_MAP.keys()).union(BASE_STYLES.keys()):
        get_style(palette_dict, style_name, inheritance_overrides)

    palette_list = [
        astuple(entry) for entry in palette_dict.values()
        if isinstance(entry, PaletteEntry)
    ]

    return palette_list