utils class final

utils

python source

# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

import re
from typing import FrozenSet, NewType, Tuple, Union, cast

from .tags import Tag, parse_tag
from .version import InvalidVersion, Version

BuildTag = Union[Tuple[()], Tuple[int, str]]
NormalizedName = NewType("NormalizedName", str)


class InvalidWheelFilename(ValueError):
    """
    An invalid wheel filename was found, users should refer to PEP 427.
    """


class InvalidSdistFilename(ValueError):
    """
    An invalid sdist filename was found, users should refer to the packaging user guide.
    """


_canonicalize_regex = re.compile(r"[-_.]+")
# PEP 427: The build number must start with a digit.
_build_tag_regex = re.compile(r"(\d+)(.*)")


def canonicalize_name(name: str) -> NormalizedName:
    # This is taken from PEP 503.
    value = _canonicalize_regex.sub("-", name).lower()
    return cast(NormalizedName, value)


def canonicalize_version(
    version: Union[Version, str], *, strip_trailing_zero: bool = True
) -> str:
    """
    This is very similar to Version.__str__, but has one subtle difference
    with the way it handles the release segment.
    """
    if isinstance(version, str):
        try:
            parsed = Version(version)
        except InvalidVersion:
            # Legacy versions cannot be normalized
            return version
    else:
        parsed = version

    parts = []

    # Epoch
    if parsed.epoch != 0:
        parts.append(f"{parsed.epoch}!")

    # Release segment
    release_segment = ".".join(str(x) for x in parsed.release)
    if strip_trailing_zero:
        # NB: This strips trailing '.0's to normalize
        release_segment = re.sub(r"(\.0)+$", "", release_segment)
    parts.append(release_segment)

    # Pre-release
    if parsed.pre is not None:
        parts.append("".join(str(x) for x in parsed.pre))

    # Post-release
    if parsed.post is not None:
        parts.append(f".post{parsed.post}")

    # Development release
    if parsed.dev is not None:
        parts.append(f".dev{parsed.dev}")

    # Local version segment
    if parsed.local is not None:
        parts.append(f"+{parsed.local}")

    return "".join(parts)


def parse_wheel_filename(
    filename: str,
) -> Tuple[NormalizedName, Version, BuildTag, FrozenSet[Tag]]:
    if not filename.endswith(".whl"):
        raise InvalidWheelFilename(
            f"Invalid wheel filename (extension must be '.whl'): {filename}"
        )

    filename = filename[:-4]
    dashes = filename.count("-")
    if dashes not in (4, 5):
        raise InvalidWheelFilename(
            f"Invalid wheel filename (wrong number of parts): {filename}"
        )

    parts = filename.split("-", dashes - 2)
    name_part = parts[0]
    # See PEP 427 for the rules on escaping the project name
    if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None:
        raise InvalidWheelFilename(f"Invalid project name: {filename}")
    name = canonicalize_name(name_part)
    version = Version(parts[1])
    if dashes == 5:
        build_part = parts[2]
        build_match = _build_tag_regex.match(build_part)
        if build_match is None:
            raise InvalidWheelFilename(
                f"Invalid build number: {build_part} in '{filename}'"
            )
        build = cast(BuildTag, (int(build_match.group(1)), build_match.group(2)))
    else:
        build = ()
    tags = parse_tag(parts[-1])
    return (name, version, build, tags)


def parse_sdist_filename(filename: str) -> Tuple[NormalizedName, Version]:
    if filename.endswith(".tar.gz"):
        file_stem = filename[: -len(".tar.gz")]
    elif filename.endswith(".zip"):
        file_stem = filename[: -len(".zip")]
    else:
        raise InvalidSdistFilename(
            f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
            f" {filename}"
        )

    # We are requiring a PEP 440 version, which cannot contain dashes,
    # so we split on the last dash.
    name_part, sep, version_part = file_stem.rpartition("-")
    if not sep:
        raise InvalidSdistFilename(f"Invalid sdist filename: {filename}")

    name = canonicalize_name(name_part)
    version = Version(version_part)
    return (name, version)
Inheritance
  • Object
  • PythonObjectInterface<PythonFfiDelegate<Object?>, Object?>
  • PythonObject
  • PythonModule
  • utils

Constructors

utils.from(PythonModuleInterface<PythonFfiDelegate<Object?>, Object?> pythonModule)

Properties

BuildTag Object?
BuildTag (getter)
getter/setter pair
finalizer Finalizer<(PythonFfiDelegate<Object?>, Object?)>
The finalizer for the python object. Gets invoked when the object is no longer accessible to the program.
finalinherited
FrozenSet Object?
FrozenSet (getter)
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
initializer → Initializer<PythonFfiDelegate<Object?>, Object?>
The initializer for the python object. Gets invoked at the start of the constructor.
finalinherited
NormalizedName Object?
NormalizedName (getter)
getter/setter pair
platform → PythonFfiDelegate<Object?>
Gets the platform that this object is associated with.
no setterinherited
reference Object?
Gets the reference to the python object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
Tuple Object?
Tuple (getter)
getter/setter pair
Union Object?
Union (getter)
getter/setter pair

Methods

canonicalize_name({required String name}) Object?
canonicalize_name
canonicalize_version({required Object? version, bool strip_trailing_zero = true}) String
canonicalize_version
debugDump() → void
Dumps the python object to the console.
inherited
getAttribute<T extends Object?>(String attributeName) → T
Gets the attribute with the given name.
inherited
getAttributeOrNull<T extends Object?>(String attributeName) → T?
Gets the attribute with the given name or null if it does not exist.
inherited
getAttributeRaw<T extends PythonObjectInterface<PythonFfiDelegate<Object?>, Object?>>(String attributeName) → T
Gets the attribute with the given name.
inherited
getClass(String className, List<Object?> args, [Map<String, Object?>? kwargs]) → PythonClassInterface<PythonFfiDelegate<Object?>, Object?>
Gets a class from the module.
inherited
getFunction(String name) → PythonFunctionInterface<PythonFfiDelegate<Object?>, Object?>
Gets the function with the given name.
inherited
hasAttribute(String attributeName) bool
Checks if the python object has the given attribute.
inherited
noSuchMethod(Invocation invocation) Object?
Invoked when a nonexistent method or property is accessed.
inherited
parse_sdist_filename({required String filename}) Object?
parse_sdist_filename
parse_tag({required String tag}) Object?
parse_tag
parse_wheel_filename({required String filename}) Object?
parse_wheel_filename
setAttribute<T extends Object?>(String attributeName, T value) → void
Sets the attribute with the given name.
inherited
setAttributeRaw<T extends PythonObjectInterface<PythonFfiDelegate<Object?>, Object?>>(String attributeName, T value) → void
Sets the attribute with the given name.
inherited
toDartObject() Object?
Converts the python object to a Dart object.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

import() utils
Imports a Python module.
override