Skip to content

finder

dependenpy finder module.

Finder ¤

Bases: object

Main package finder class.

Initialize it with a list of package finder classes (not instances).

Source code in dependenpy/finder.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
class Finder(object):
    """
    Main package finder class.

    Initialize it with a list of package finder classes (not instances).
    """

    def __init__(self, finders: List[Type] = None):
        """
        Initialization method.

        Args:
            finders: list of package finder classes (not instances) in a specific
                order. Default: [LocalPackageFinder, InstalledPackageFinder].
        """
        if finders is None:
            self.finders = [LocalPackageFinder(), InstalledPackageFinder()]
        else:
            self.finders = [finder() for finder in finders]

    def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
        """
        Find a package using package finders.

        Return the first package found.

        Args:
            package: package to find.
            **kwargs: additional keyword arguments used by finders.

        Returns:
            Package spec or None.
        """
        for finder in self.finders:
            package_spec = finder.find(package, **kwargs)
            if package_spec:
                return package_spec
        return None

__init__(finders=None) ¤

Initialization method.

Parameters:

Name Type Description Default
finders List[Type]

list of package finder classes (not instances) in a specific order. Default: [LocalPackageFinder, InstalledPackageFinder].

None
Source code in dependenpy/finder.py
156
157
158
159
160
161
162
163
164
165
166
167
def __init__(self, finders: List[Type] = None):
    """
    Initialization method.

    Args:
        finders: list of package finder classes (not instances) in a specific
            order. Default: [LocalPackageFinder, InstalledPackageFinder].
    """
    if finders is None:
        self.finders = [LocalPackageFinder(), InstalledPackageFinder()]
    else:
        self.finders = [finder() for finder in finders]

find(package, **kwargs) ¤

Find a package using package finders.

Return the first package found.

Parameters:

Name Type Description Default
package str

package to find.

required
**kwargs Any

additional keyword arguments used by finders.

{}

Returns:

Type Description
PackageSpec | None

Package spec or None.

Source code in dependenpy/finder.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
    """
    Find a package using package finders.

    Return the first package found.

    Args:
        package: package to find.
        **kwargs: additional keyword arguments used by finders.

    Returns:
        Package spec or None.
    """
    for finder in self.finders:
        package_spec = finder.find(package, **kwargs)
        if package_spec:
            return package_spec
    return None

InstalledPackageFinder ¤

Bases: PackageFinder

Finder to find installed Python packages using importlib.

Source code in dependenpy/finder.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class InstalledPackageFinder(PackageFinder):
    """Finder to find installed Python packages using importlib."""

    def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
        """
        Find method.

        Args:
            package: package to find.
            **kwargs: additional keyword arguments.

        Returns:
            Package spec or None.
        """
        spec = find_spec(package)
        if spec is None:
            return None
        if "." in package:
            package, rest = package.split(".", 1)
            limit = [rest]
            spec = find_spec(package)
        else:
            limit = []
        if spec is not None:
            if spec.submodule_search_locations:
                path = spec.submodule_search_locations[0]
            elif spec.origin and spec.origin != "built-in":
                path = spec.origin
            else:
                return None
            return PackageSpec(spec.name, path, limit)
        return None

find(package, **kwargs) ¤

Find method.

Parameters:

Name Type Description Default
package str

package to find.

required
**kwargs Any

additional keyword arguments.

{}

Returns:

Type Description
PackageSpec | None

Package spec or None.

Source code in dependenpy/finder.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
    """
    Find method.

    Args:
        package: package to find.
        **kwargs: additional keyword arguments.

    Returns:
        Package spec or None.
    """
    spec = find_spec(package)
    if spec is None:
        return None
    if "." in package:
        package, rest = package.split(".", 1)
        limit = [rest]
        spec = find_spec(package)
    else:
        limit = []
    if spec is not None:
        if spec.submodule_search_locations:
            path = spec.submodule_search_locations[0]
        elif spec.origin and spec.origin != "built-in":
            path = spec.origin
        else:
            return None
        return PackageSpec(spec.name, path, limit)
    return None

LocalPackageFinder ¤

Bases: PackageFinder

Finder to find local packages (directories on the disk).

Source code in dependenpy/finder.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
class LocalPackageFinder(PackageFinder):
    """Finder to find local packages (directories on the disk)."""

    def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
        """
        Find method.

        Args:
            package: package to find.
            **kwargs: additional keyword arguments.

        Returns:
            Package spec or None.
        """
        if not exists(package):
            return None
        name, path = None, None
        enforce_init = kwargs.pop("enforce_init", True)
        if isdir(package):
            if isfile(join(package, "__init__.py")) or not enforce_init:
                name, path = basename(package), package
        elif isfile(package) and package.endswith(".py"):
            name, path = splitext(basename(package))[0], package
        if name and path:
            return PackageSpec(name, path)
        return None

find(package, **kwargs) ¤

Find method.

Parameters:

Name Type Description Default
package str

package to find.

required
**kwargs Any

additional keyword arguments.

{}

Returns:

Type Description
PackageSpec | None

Package spec or None.

Source code in dependenpy/finder.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
    """
    Find method.

    Args:
        package: package to find.
        **kwargs: additional keyword arguments.

    Returns:
        Package spec or None.
    """
    if not exists(package):
        return None
    name, path = None, None
    enforce_init = kwargs.pop("enforce_init", True)
    if isdir(package):
        if isfile(join(package, "__init__.py")) or not enforce_init:
            name, path = basename(package), package
    elif isfile(package) and package.endswith(".py"):
        name, path = splitext(basename(package))[0], package
    if name and path:
        return PackageSpec(name, path)
    return None

PackageFinder ¤

Bases: object

Abstract package finder class.

Source code in dependenpy/finder.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class PackageFinder(object):
    """Abstract package finder class."""

    def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
        """
        Find method.

        Args:
            package: package to find.
            **kwargs: additional keyword arguments.

        Returns:
            Package spec or None.
        """  # noqa: DAR202,DAR401
        raise NotImplementedError

find(package, **kwargs) ¤

Find method.

Parameters:

Name Type Description Default
package str

package to find.

required
**kwargs Any

additional keyword arguments.

{}

Returns:

Type Description
PackageSpec | None

Package spec or None.

Source code in dependenpy/finder.py
73
74
75
76
77
78
79
80
81
82
83
84
def find(self, package: str, **kwargs: Any) -> PackageSpec | None:
    """
    Find method.

    Args:
        package: package to find.
        **kwargs: additional keyword arguments.

    Returns:
        Package spec or None.
    """  # noqa: DAR202,DAR401
    raise NotImplementedError

PackageSpec ¤

Bases: object

Holder for a package specification (given as argument to DSM).

Source code in dependenpy/finder.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class PackageSpec(object):
    """Holder for a package specification (given as argument to DSM)."""

    def __init__(self, name, path, limit_to=None):
        """
        Initialization method.

        Args:
            name (str): name of the package.
            path (str): path to the package.
            limit_to (list of str): limitations.
        """
        self.name = name
        self.path = path
        self.limit_to = limit_to or []

    def __hash__(self):
        return hash((self.name, self.path))

    @property
    def ismodule(self) -> bool:
        """
        Property to tell if the package is in fact a module (a file).

        Returns:
            Whether this package is in fact a module.
        """
        return self.path.endswith(".py")

    def add(self, spec: PackageSpec) -> None:
        """
        Add limitations of given spec to self's.

        Args:
            spec: Another spec.
        """
        for limit in spec.limit_to:
            if limit not in self.limit_to:
                self.limit_to.append(limit)

    @staticmethod  # noqa: WPS602
    def combine(specs: list[PackageSpec]) -> list[PackageSpec]:  # noqa: WPS602
        """
        Combine package specifications' limitations.

        Args:
            specs: The package specifications.

        Returns:
            The new, merged list of PackageSpec.
        """
        new_specs: dict[PackageSpec, PackageSpec] = {}
        for spec in specs:
            if new_specs.get(spec, None) is None:
                new_specs[spec] = spec
            else:
                new_specs[spec].add(spec)
        return list(new_specs.values())

__init__(name, path, limit_to=None) ¤

Initialization method.

Parameters:

Name Type Description Default
name str

name of the package.

required
path str

path to the package.

required
limit_to list of str

limitations.

None
Source code in dependenpy/finder.py
13
14
15
16
17
18
19
20
21
22
23
24
def __init__(self, name, path, limit_to=None):
    """
    Initialization method.

    Args:
        name (str): name of the package.
        path (str): path to the package.
        limit_to (list of str): limitations.
    """
    self.name = name
    self.path = path
    self.limit_to = limit_to or []

add(spec) ¤

Add limitations of given spec to self's.

Parameters:

Name Type Description Default
spec PackageSpec

Another spec.

required
Source code in dependenpy/finder.py
39
40
41
42
43
44
45
46
47
48
def add(self, spec: PackageSpec) -> None:
    """
    Add limitations of given spec to self's.

    Args:
        spec: Another spec.
    """
    for limit in spec.limit_to:
        if limit not in self.limit_to:
            self.limit_to.append(limit)

combine(specs) staticmethod ¤

Combine package specifications' limitations.

Parameters:

Name Type Description Default
specs list[PackageSpec]

The package specifications.

required

Returns:

Type Description
list[PackageSpec]

The new, merged list of PackageSpec.

Source code in dependenpy/finder.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@staticmethod  # noqa: WPS602
def combine(specs: list[PackageSpec]) -> list[PackageSpec]:  # noqa: WPS602
    """
    Combine package specifications' limitations.

    Args:
        specs: The package specifications.

    Returns:
        The new, merged list of PackageSpec.
    """
    new_specs: dict[PackageSpec, PackageSpec] = {}
    for spec in specs:
        if new_specs.get(spec, None) is None:
            new_specs[spec] = spec
        else:
            new_specs[spec].add(spec)
    return list(new_specs.values())

ismodule() property ¤

Property to tell if the package is in fact a module (a file).

Returns:

Type Description
bool

Whether this package is in fact a module.

Source code in dependenpy/finder.py
29
30
31
32
33
34
35
36
37
@property
def ismodule(self) -> bool:
    """
    Property to tell if the package is in fact a module (a file).

    Returns:
        Whether this package is in fact a module.
    """
    return self.path.endswith(".py")