How to use the packaging.specifiers.Specifier function in packaging

To help you get started, we’ve selected a few packaging examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pypa / packaging / tests / test_specifiers.py View on Github external
def test_specifier_prereleases_detection(self, specifier, expected):
        assert Specifier(specifier).prereleases == expected
github pypa / packaging / tests / test_specifiers.py View on Github external
def test_specifiers_str_and_repr(self, specifier, expected):
        spec = Specifier(specifier)

        assert str(spec) == expected
        assert repr(spec) == "".format(repr(expected))
github sarugaku / requirementslib / src / requirementslib / models / markers.py View on Github external
Take a specifierset string and split it into a list to join for specifier sets

    :param str specset_str: A string containing python versions, often comma separated
    :param str prefix: A prefix to use when generating the specifier set
    :return: A list of :class:`Specifier` instances generated with the provided prefix
    :rtype: Set[Specifier]
    """
    specifiers = set()
    if "," not in specset_str and " " in specset_str:
        values = [v.strip() for v in specset_str.split()]
    else:
        values = [v.strip() for v in specset_str.split(",")]
    if prefix == "!=" and any(v in values for v in DEPRECATED_VERSIONS):
        values = DEPRECATED_VERSIONS[:]
    for value in sorted(values):
        specifiers.add(Specifier("{0}{1}".format(prefix, value)))
    return specifiers
github pypa / pipenv / pipenv / vendor / requirementslib / models / markers.py View on Github external
def _get_specs(specset):
    if specset is None:
        return
    if is_instance(specset, Specifier):
        new_specset = SpecifierSet()
        specs = set()
        specs.add(specset)
        new_specset._specs = frozenset(specs)
        specset = new_specset
    if isinstance(specset, str):
        specset = SpecifierSet(specset)
    result = []
    try:
        for spec in set(specset):
            version = spec.version
            op = spec.operator
            if op in ("in", "not in"):
                versions = version.split(",")
                op = "==" if op == "in" else "!="
                for ver in versions:
github sarugaku / passa / src / passa / internals / specifiers.py View on Github external
def _get_specs(specset):
    if isinstance(specset, Specifier):
        specset = str(specset)
    if isinstance(specset, str):
        specset = SpecifierSet(specset.replace(".*", ""))
    return [
        (spec._spec[0], _tuplize_version(spec._spec[1]))
        for spec in getattr(specset, "_specs", [])
    ]
github sarugaku / requirementslib / src / requirementslib / models / requirements.py View on Github external
from six.moves.urllib.parse import SplitResult
    from .vcs import VCSRepository
    from .dependencies import AbstractDependency

    NON_STRING_ITERABLE = Union[List, Set, Tuple]
    STRING_TYPE = Union[str, bytes, Text]
    S = TypeVar("S", bytes, str, Text)
    BASE_TYPES = Union[bool, STRING_TYPE, Tuple[STRING_TYPE, ...]]
    CUSTOM_TYPES = Union[VCSRepository, RequirementType, SetupInfo, "Line"]
    CREATION_ARG_TYPES = Union[BASE_TYPES, Link, CUSTOM_TYPES]
    PIPFILE_ENTRY_TYPE = Union[STRING_TYPE, bool, Tuple[STRING_TYPE], List[STRING_TYPE]]
    PIPFILE_TYPE = Union[STRING_TYPE, Dict[STRING_TYPE, PIPFILE_ENTRY_TYPE]]
    TPIPFILE = Dict[STRING_TYPE, PIPFILE_ENTRY_TYPE]


SPECIFIERS_BY_LENGTH = sorted(list(Specifier._operators.keys()), key=len, reverse=True)


class Line(object):
    def __init__(self, line, extras=None):
        # type: (AnyStr, Optional[Union[List[S], Set[S], Tuple[S, ...]]]) -> None
        self.editable = False  # type: bool
        if line.startswith("-e "):
            line = line[len("-e ") :]
            self.editable = True
        self.extras = ()  # type: Tuple[STRING_TYPE, ...]
        if extras is not None:
            self.extras = tuple(sorted(set(extras)))
        self.line = line  # type: STRING_TYPE
        self.hashes = []  # type: List[STRING_TYPE]
        self.markers = None  # type: Optional[STRING_TYPE]
        self.vcs = None  # type: Optional[STRING_TYPE]
github pypa / pipenv / pipenv / vendor / requirementslib / models / markers.py View on Github external
def _format_pyspec(specifier):
    if isinstance(specifier, str):
        if not any(op in specifier for op in Specifier._operators.keys()):
            specifier = "=={0}".format(specifier)
        specifier = Specifier(specifier)
    version = getattr(specifier, "version", specifier)
    if version.endswith("*") and not version.endswith(".*"):
        version = "{0}.*".format(version.rstrip("*"))
    if ".*" in version:
        version = version.replace(".*", "")
        specifier = Specifier("{0}{1}".format(specifier.operator, version))
    try:
        op = REPLACE_RANGES[specifier.operator]
    except KeyError:
        print(specifier)
        return specifier
    curr_tuple = _tuplize_version(version)
    try:
        next_tuple = (curr_tuple[0], curr_tuple[1] + 1)
github pypa / pipenv / pipenv / vendor / requirementslib / models / requirements.py View on Github external
def get_specifier(self):
        # type: () -> Union[SpecifierSet, LegacySpecifier]
        try:
            return Specifier(self.specifiers)
        except InvalidSpecifier:
            return LegacySpecifier(self.specifiers)
github pypa / pipenv / pipenv / vendor / requirementslib / models / markers.py View on Github external
def _format_pyspec(specifier):
    if isinstance(specifier, str):
        if not any(op in specifier for op in Specifier._operators.keys()):
            specifier = "=={0}".format(specifier)
        specifier = Specifier(specifier)
    version = getattr(specifier, "version", specifier)
    if version.endswith("*") and not version.endswith(".*"):
        version = "{0}.*".format(version.rstrip("*"))
    if ".*" in version:
        version = version.replace(".*", "")
        specifier = Specifier("{0}{1}".format(specifier.operator, version))
    try:
        op = REPLACE_RANGES[specifier.operator]
    except KeyError:
        print(specifier)
        return specifier
    curr_tuple = _tuplize_version(version)
    try:
        next_tuple = (curr_tuple[0], curr_tuple[1] + 1)
    except IndexError:
        next_tuple = (curr_tuple[0], 1)
github sarugaku / requirementslib / src / requirementslib / models / markers.py View on Github external
def _format_pyspec(specifier):
    # type: (Union[STRING_TYPE, Specifier]) -> Specifier
    if isinstance(specifier, str):
        if not any(op in specifier for op in Specifier._operators.keys()):
            specifier = "=={0}".format(specifier)
        specifier = Specifier(specifier)
    version = getattr(specifier, "version", specifier).rstrip()
    if version and version.endswith("*"):
        if version.endswith(".*"):
            version = version[:-2]
        version = version.rstrip("*")
        specifier = Specifier("{0}{1}".format(specifier.operator, version))
    try:
        op = REPLACE_RANGES[specifier.operator]
    except KeyError:
        return specifier
    curr_tuple = _tuplize_version(version)
    try:
        next_tuple = (curr_tuple[0], curr_tuple[1] + 1)
    except IndexError: