def __getattr__(self, name): info = FORMATTERS.get(name) if info: _load_formatters(info[0]) cls = _formatter_cache[info[1]] setattr(self, name, cls) return cls raise AttributeError(name)
def get_all_formatters(): """Return a generator for all formatter classes.""" # NB: this returns formatter classes, not info like get_all_lexers(). for info in FORMATTERS.values(): if info[1] not in _formatter_cache: _load_formatters(info[0]) yield _formatter_cache[info[1]] for _, formatter in find_plugin_formatters(): yield formatter
def find_formatter_class(alias): """Lookup a formatter by alias. Returns None if not found. """ for module_name, name, aliases, _, _ in FORMATTERS.values(): if alias in aliases: if name not in _formatter_cache: _load_formatters(module_name) return _formatter_cache[name] for _, cls in find_plugin_formatters(): if alias in cls.aliases: return cls
def get_formatter_for_filename(fn, **options): """Lookup and instantiate a formatter by filename pattern. Raises ClassNotFound if not found. """ fn = basename(fn) for modname, name, _, filenames, _ in FORMATTERS.values(): for filename in filenames: if _fn_matches(fn, filename): if name not in _formatter_cache: _load_formatters(modname) return _formatter_cache[name](**options) for cls in find_plugin_formatters(): for filename in cls.filenames: if _fn_matches(fn, filename): return cls(**options) raise ClassNotFound("no formatter found for file name %r" % fn)
def _arg_parse() -> Namespace: parser = ArgumentParser() parser.add_argument("name", nargs="?", type=PurePath) parser.add_argument("-", "--stdin", action="store_true") themes = sorted(get_all_styles()) parser.add_argument("-t", "--theme", choices=themes, default=DEFAULT_STYLE) formatters = sorted(name for _, _, names, *_ in FORMATTERS.values() for name in names) parser.add_argument("-f", "--formatter", choices=formatters, default=DEFAULT_FORMATTER) args = parser.parse_args() return args