forked from python-poetry/poetry-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.py
116 lines (97 loc) · 3.79 KB
/
module.py
1
2
3
4
5
6
7
8
9
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
113
114
115
116
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Mapping
from typing import Sequence
if TYPE_CHECKING:
from poetry.core.masonry.utils.include import Include
class ModuleOrPackageNotFoundError(ValueError):
pass
class Module:
def __init__(
self,
name: str,
directory: str = ".",
packages: Sequence[Mapping[str, Any]] = (),
includes: Sequence[Mapping[str, Any]] = (),
) -> None:
from poetry.core.masonry.utils.include import Include
from poetry.core.masonry.utils.package_include import PackageInclude
from poetry.core.utils.helpers import module_name
self._name = module_name(name)
self._in_src = False
self._is_package = False
self._path = Path(directory)
self._package_includes: list[PackageInclude] = []
self._explicit_includes: list[Include] = []
if not packages:
# It must exist either as a .py file or a directory, but not both
pkg_dir = Path(directory, self._name)
py_file = Path(directory, self._name + ".py")
default_package: dict[str, Any]
if pkg_dir.is_dir() and py_file.is_file():
raise ValueError(f"Both {pkg_dir} and {py_file} exist")
elif pkg_dir.is_dir():
default_package = {"include": str(pkg_dir.relative_to(self._path))}
elif py_file.is_file():
default_package = {"include": str(py_file.relative_to(self._path))}
else:
# Searching for a src module
src = Path(directory, "src")
src_pkg_dir = src / self._name
src_py_file = src / (self._name + ".py")
if src_pkg_dir.is_dir() and src_py_file.is_file():
raise ValueError(f"Both {pkg_dir} and {py_file} exist")
elif src_pkg_dir.is_dir():
default_package = {
"include": str(src_pkg_dir.relative_to(src)),
"from": str(src.relative_to(self._path)),
}
elif src_py_file.is_file():
default_package = {
"include": str(src_py_file.relative_to(src)),
"from": str(src.relative_to(self._path)),
}
else:
raise ModuleOrPackageNotFoundError(
f"No file/folder found for package {name}"
)
default_package["format"] = ["sdist", "wheel"]
packages = [default_package]
for package in packages:
self._package_includes.append(
PackageInclude(
self._path,
package["include"],
formats=package["format"],
source=package.get("from"),
target=package.get("to"),
)
)
for include in includes:
self._explicit_includes.append(
Include(self._path, include["path"], formats=include["format"])
)
@property
def name(self) -> str:
return self._name
@property
def path(self) -> Path:
return self._path
@property
def file(self) -> Path:
if self._is_package:
return self._path / "__init__.py"
else:
return self._path
@property
def includes(self) -> list[Include]:
return [*self._package_includes, *self._explicit_includes]
@property
def explicit_includes(self) -> list[Include]:
return self._explicit_includes
def is_package(self) -> bool:
return self._is_package
def is_in_src(self) -> bool:
return self._in_src