Source code for asyncutils.tools

 1import asyncutils as A, asyncutils._internal as I, shlex as s
 2get_cmd_help, __all__ = I.parsed.p.format_help, I.submodules.tools_all
[docs] 3def loadf(p, e=None, /, l=I.unparsed.l, _=I.helpers.fullname): 4 if not ((f := getattr(p, '__fspath__', None)) is None or isinstance(p := f(), (str, bytes))): raise TypeError(f'asyncutils.tools.loadf: __fspath__ method returned {_(p)} instead of str or bytes') 5 if not (isinstance(p, int) or e is None): raise TypeError('asyncutils.tools.loadf: did not expect extension') 6 if e == '': raise ValueError('asyncutils.tools.loadf: empty extension') 7 return l(p.decode() if isinstance(p, bytes) else p, e)
[docs] 8def json_to_argv(p, /, d='.', D=(('quiet', 'q'), ('basic_repl', 'b'), ('load_all', 'p'), ('debug', 'd'), ('pdb', 'P')), g=A.raise_exc, a=('context', 'next_config'), *, strict=True): 9 f = (R := []).append 10 if s := (p := (m := loadf(p)).pop)('executor', l := 'thread') != l: f('-c' if d in s else '-e'); f(s) 11 if (l := p('max_memerrs', None)) != 3: f('-m'); f(str(l)) # noqa: PLR2004 12 if (s := p('seed', None)) is not None: f('-s'); f(repr(s)) 13 if (l := p('logging_to', s := 'STDERR')) == 'NULL' or p('no_log', False): f('-n') 14 elif l != s: 15 f('-l') 16 if l != 'MAKE': f(l) 17 if (r := f'-{"".join(c for k, c in D if p(k, False))}{("Q"*-l if (l := p("V", 0)-p("Q", 0)) < 0 else "V"*l)}') != '-': 18 if R: R[0] = r+R[0][1:] 19 else: f(r) 20 for k in a: p(k, None) 21 if strict and m: g(ValueError, 'unknown keys in config file', notes=m) 22 return R
[docs] 23def json_to_argstr(p, /, *, join=s.join, strict=True): return join(json_to_argv(p, strict=strict))
[docs] 24def argv_to_json(a, p, /, *, dump=__import__('json').dump, _=I.parsed.p.parse_args): 25 with open(p, 'w') as f: dump(_(a).__dict__, f)
[docs] 26def argstr_to_json(a, p, /, *, split=s.split, **k): argv_to_json(split(a), p, **k)
[docs] 27def find_help_url(o=None, /, _=frozenset((None, 'asyncutils', A)), g=I.initialize.Module, h=I.helpers, m=frozenset(('__hexversion__', '__version__', 'console_preloaded_submodules', 'preloaded_submodules', 'time_since_boot', 'submodules_map')), M=A.submodules_map): 28 if o in _: return 'https://asyncutils.readthedocs.io/en/stable/index.html' 29 s = None 30 if isinstance(o, str): 31 if (o := o.removeprefix('asyncutils.')) == '__init__': return 'https://asyncutils.readthedocs.io/en/stable/top-level.html' 32 s, _, o = o.partition('.') 33 if s in m: s, o = 'asyncutils', s 34 elif _: o = getattr(g(s), o) 35 else: o = g(s) 36 if isinstance(o, (classmethod, staticmethod)): o = o.__func__ 37 elif isinstance(o, property): o = o.fget 38 if h.ismodule(o): s, o = o.__name__, None # ty: ignore[unresolved-attribute] 39 elif callable(o): s, o = o.__module__, o.__qualname__ 40 elif s is None: raise TypeError(f'asyncutils.tools.find_help_url: expected a string, module, property, classmethod, staticmethod or callable; got {h.fullname(o)}') 41 s = s.removeprefix('asyncutils.'); return (f'https://asyncutils.readthedocs.io/en/stable/api/asyncutils/{o}/index.html' if o in M else 'https://asyncutils.readthedocs.io/en/stable/top-level.html') if s == 'asyncutils' else f'https://asyncutils.readthedocs.io/en/stable/api/asyncutils/{s}/index.html#{f"module-asyncutils.{s}" if o is None else f"asyncutils.{s}.{o}"}'
[docs] 42def open_help(o=None, /): return __import__('webbrowser').open(find_help_url(o))
[docs] 43def get_cfg_json_format(_=('',)): return __import__('importlib.resources', fromlist=_).files('asyncutils').joinpath('format.json5').read_text()
46I.patch.patch_function_signatures((find_help_url, 'o=None, /'), (loadf, "path, ext='json', /"), (json_to_argv, 'path, /'), (json_to_argstr, 'path, /, *, join={}'), (argv_to_json, 'argv, path, /, *, dump={}'), (argstr_to_json, 'argstr, path, /, *, dump={0}, split={0}'), (get_cfg_json_format, '')) 47del A, I, s