codekingpro/portable-devtools
114k
1from functools import lru_cache2from typing import Callable, Iterable, Iterator, TypeVar, Union, overload3 4import setuptools.extern.jaraco.text as text5from setuptools.extern.packaging.requirements import Requirement6 7_T = TypeVar("_T")8_StrOrIter = Union[str, Iterable[str]]9 10 11parse_req: Callable[[str], Requirement] = lru_cache()(Requirement)12# Setuptools parses the same requirement many times13# (e.g. first for validation than for normalisation),14# so it might be worth to cache.15 16 17def parse_strings(strs: _StrOrIter) -> Iterator[str]:18 """19 Yield requirement strings for each specification in `strs`.20 21 `strs` must be a string, or a (possibly-nested) iterable thereof.22 """23 return text.join_continuation(map(text.drop_comment, text.yield_lines(strs)))24 25 26@overload27def parse(strs: _StrOrIter) -> Iterator[Requirement]: ...28 29 30@overload31def parse(strs: _StrOrIter, parser: Callable[[str], _T]) -> Iterator[_T]: ...32 33 34def parse(strs, parser=parse_req):35 """36 Replacement for ``pkg_resources.parse_requirements`` that uses ``packaging``.37 """38 return map(parser, parse_strings(strs))39 