Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
_entry_points.py89 linesDownload Raw Back to setuptools
1import functools2import operator3import itertools4 5from .errors import OptionError6from .extern.jaraco.text import yield_lines7from .extern.jaraco.functools import pass_none8from ._importlib import metadata9from ._itertools import ensure_unique10from .extern.more_itertools import consume11 12 13def ensure_valid(ep):14    """15    Exercise one of the dynamic properties to trigger16    the pattern match.17    """18    try:19        ep.extras20    except AttributeError as ex:21        msg = (22            f"Problems to parse {ep}.\nPlease ensure entry-point follows the spec: "23            "https://packaging.python.org/en/latest/specifications/entry-points/"24        )25        raise OptionError(msg) from ex26 27 28def load_group(value, group):29    """30    Given a value of an entry point or series of entry points,31    return each as an EntryPoint.32    """33    # normalize to a single sequence of lines34    lines = yield_lines(value)35    text = f'[{group}]\n' + '\n'.join(lines)36    return metadata.EntryPoints._from_text(text)37 38 39def by_group_and_name(ep):40    return ep.group, ep.name41 42 43def validate(eps: metadata.EntryPoints):44    """45    Ensure entry points are unique by group and name and validate each.46    """47    consume(map(ensure_valid, ensure_unique(eps, key=by_group_and_name)))48    return eps49 50 51@functools.singledispatch52def load(eps):53    """54    Given a Distribution.entry_points, produce EntryPoints.55    """56    groups = itertools.chain.from_iterable(57        load_group(value, group) for group, value in eps.items()58    )59    return validate(metadata.EntryPoints(groups))60 61 62@load.register(str)63def _(eps):64    r"""65    >>> ep, = load('[console_scripts]\nfoo=bar')66    >>> ep.group67    'console_scripts'68    >>> ep.name69    'foo'70    >>> ep.value71    'bar'72    """73    return validate(metadata.EntryPoints(metadata.EntryPoints._from_text(eps)))74 75 76load.register(type(None), lambda x: x)77 78 79@pass_none80def render(eps: metadata.EntryPoints):81    by_group = operator.attrgetter('group')82    groups = itertools.groupby(sorted(eps, key=by_group), by_group)83 84    return '\n'.join(f'[{group}]\n{render_items(items)}\n' for group, items in groups)85 86 87def render_items(eps):88    return '\n'.join(f'{ep.name} = {ep.value}' for ep in sorted(eps))89 
codekingpro/portable-devtools · Team Ai