Skip to content

Commit

Permalink
Reorganize imports to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Feb 5, 2021
1 parent 908bb3a commit 26509f9
Show file tree
Hide file tree
Showing 53 changed files with 763 additions and 720 deletions.
23 changes: 15 additions & 8 deletions poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
import logging

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Union
from warnings import warn

from .json import validate_object
from .packages.dependency import Dependency
from .packages.project_package import ProjectPackage
from .poetry import Poetry
from .pyproject import PyProjectTOML
from .spdx import license_by_id

if TYPE_CHECKING:
from .packages.types import DependencyTypes
from .poetry import Poetry

logger = logging.getLogger(__name__)

Expand All @@ -29,7 +27,13 @@ class Factory(object):

def create_poetry(
self, cwd: Optional[Path] = None, with_dev: bool = True
) -> Poetry:
) -> "Poetry":
from .packages.dependency import Dependency
from .packages.project_package import ProjectPackage
from .poetry import Poetry
from .pyproject.toml import PyProjectTOML
from .spdx.helpers import license_by_id

poetry_file = self.locate(cwd)
local_config = PyProjectTOML(path=poetry_file).poetry_config

Expand Down Expand Up @@ -168,8 +172,9 @@ def create_dependency(
constraint: Union[str, Dict[str, Any]],
category: str = "main",
root_dir: Optional[Path] = None,
) -> Dependency:
) -> "DependencyTypes":
from .packages.constraints import parse_constraint as parse_generic_constraint
from .packages.dependency import Dependency
from .packages.directory_dependency import DirectoryDependency
from .packages.file_dependency import FileDependency
from .packages.url_dependency import URLDependency
Expand Down Expand Up @@ -307,6 +312,8 @@ def validate(cls, config: dict, strict: bool = False) -> Dict[str, List[str]]:
"""
Checks the validity of a configuration
"""
from .json import validate_object

result = {"errors": [], "warnings": []}
# Schema validation errors
validation_errors = validate_object(config, "poetry-schema")
Expand Down
4 changes: 2 additions & 2 deletions poetry/core/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from io import open
from typing import List

from jsonschema import Draft7Validator


SCHEMA_DIR = os.path.join(os.path.dirname(__file__), "schemas")

Expand All @@ -24,6 +22,8 @@ def validate_object(obj: dict, schema_name: str) -> List[str]:
with open(schema, encoding="utf-8") as f:
schema = json.loads(f.read())

from jsonschema import Draft7Validator

validator = Draft7Validator(schema)
validation_errors = sorted(validator.iter_errors(obj), key=lambda e: e.path)

Expand Down
2 changes: 0 additions & 2 deletions poetry/core/masonry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
`flit <https://github.com/takluyver/flit>`__ and adapted
to work with the poetry codebase, so kudos to them for showing the way.
"""

from .builder import Builder
22 changes: 11 additions & 11 deletions poetry/core/masonry/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@
from typing import Optional
from typing import Union

from .builders.sdist import SdistBuilder
from .builders.wheel import WheelBuilder


if TYPE_CHECKING:
from poetry.core.poetry import Poetry # noqa


class Builder:
_FORMATS = {
"sdist": SdistBuilder,
"wheel": WheelBuilder,
}

def __init__(self, poetry: "Poetry") -> None:
from .builders.sdist import SdistBuilder
from .builders.wheel import WheelBuilder

self._poetry = poetry

self._formats = {
"sdist": SdistBuilder,
"wheel": WheelBuilder,
}

def build(self, fmt: str, executable: Optional[Union[str, Path]] = None) -> None:
if fmt in self._FORMATS:
builders = [self._FORMATS[fmt]]
if fmt in self._formats:
builders = [self._formats[fmt]]
elif fmt == "all":
builders = self._FORMATS.values()
builders = self._formats.values()
else:
raise ValueError("Invalid format: {}".format(fmt))

Expand Down
2 changes: 0 additions & 2 deletions poetry/core/masonry/builders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
from .sdist import SdistBuilder
from .wheel import WheelBuilder
13 changes: 7 additions & 6 deletions poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
from typing import Set
from typing import Union

from poetry.core.vcs import get_vcs

from ..metadata import Metadata
from ..utils.module import Module
from ..utils.package_include import PackageInclude


if TYPE_CHECKING:
from poetry.core.poetry import Poetry # noqa
Expand All @@ -48,6 +42,9 @@ def __init__(
ignore_packages_formats: bool = False,
executable: Optional[Union[Path, str]] = None,
) -> None:
from poetry.core.masonry.metadata import Metadata
from poetry.core.masonry.utils.module import Module

self._poetry = poetry
self._package = poetry.package
self._path = poetry.file.parent
Expand Down Expand Up @@ -102,6 +99,8 @@ def build(self) -> None:

def find_excluded_files(self) -> Set[str]:
if self._excluded_files is None:
from poetry.core.vcs import get_vcs

# Checking VCS
vcs = get_vcs(self._path)
if not vcs:
Expand Down Expand Up @@ -154,6 +153,8 @@ def find_files_to_add(self, exclude_build: bool = True) -> Set["BuildIncludeFile
"""
Finds all files to add to the tarball
"""
from poetry.core.masonry.utils.package_include import PackageInclude

to_add = set()

for include in self._module.includes:
Expand Down
9 changes: 6 additions & 3 deletions poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
from typing import Set
from typing import Tuple

from ..utils.helpers import normalize_file_permissions
from ..utils.package_include import PackageInclude
from .builder import Builder
from .builder import BuildIncludeFile


if TYPE_CHECKING:
from poetry.core.masonry.utils.package_include import PackageInclude # noqa
from poetry.core.packages import Dependency # noqa
from poetry.core.packages import ProjectPackage # noqa

Expand Down Expand Up @@ -115,6 +114,8 @@ def build(self, target_dir: Optional[Path] = None) -> Path:
return target

def build_setup(self) -> bytes:
from poetry.core.masonry.utils.package_include import PackageInclude

before, extra, after = [], [], []
package_dir = {}

Expand Down Expand Up @@ -225,7 +226,7 @@ def setup_py(self) -> ContextManager[Path]:
def build_pkg_info(self) -> bytes:
return self.get_metadata_content().encode()

def find_packages(self, include: PackageInclude) -> Tuple[str, List[str], dict]:
def find_packages(self, include: "PackageInclude") -> Tuple[str, List[str], dict]:
"""
Discover subpackages and data.
Expand Down Expand Up @@ -396,6 +397,8 @@ def clean_tarinfo(cls, tar_info: TarInfo) -> TarInfo:
- Normalise permissions to 644 or 755
- Set mtime if not None
"""
from poetry.core.masonry.utils.helpers import normalize_file_permissions

ti = copy(tar_info)
ti.uid = 0
ti.gid = 0
Expand Down
2 changes: 1 addition & 1 deletion poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from packaging.tags import sys_tags

from poetry.core import __version__
from poetry.core.semver import parse_constraint
from poetry.core.semver.helpers import parse_constraint

from ..utils.helpers import escape_name
from ..utils.helpers import escape_version
Expand Down
8 changes: 4 additions & 4 deletions poetry/core/masonry/metadata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from typing import TYPE_CHECKING

from poetry.core.utils.helpers import canonicalize_name
from poetry.core.utils.helpers import normalize_version
from poetry.core.version.helpers import format_python_constraint


if TYPE_CHECKING:
from poetry.core.packages import Package # noqa
Expand Down Expand Up @@ -46,6 +42,10 @@ class Metadata:

@classmethod
def from_package(cls, package: "Package") -> "Metadata":
from poetry.core.utils.helpers import canonicalize_name
from poetry.core.utils.helpers import normalize_version
from poetry.core.version.helpers import format_python_constraint

meta = cls()

meta.name = canonicalize_name(package.name)
Expand Down
10 changes: 5 additions & 5 deletions poetry/core/masonry/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
from typing import List
from typing import Optional

from poetry.core.utils.helpers import module_name

from .include import Include
from .package_include import PackageInclude


class ModuleOrPackageNotFound(ValueError):

Expand All @@ -23,6 +18,11 @@ def __init__(
packages: Optional[List[Dict[str, Any]]] = None,
includes: Optional[List[Dict[str, Any]]] = None,
) -> None:
from poetry.core.utils.helpers import module_name

from .include import Include
from .package_include import PackageInclude

self._name = module_name(name)
self._in_src = False
self._is_package = False
Expand Down
Loading

0 comments on commit 26509f9

Please sign in to comment.