Skip to content

Split PyProject Property #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions cppython/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
from cppython.project import Project, ProjectConfiguration


def _create_pyproject() -> dict[str, Any]:
def _find_pyproject_file() -> Path:
"""
TODO
"""

# Search for a path upward
path = Path.cwd()
Expand All @@ -25,6 +28,14 @@ def _create_pyproject() -> dict[str, Any]:

path = Path(path / "pyproject.toml")

return path


def _create_pyproject(path: Path) -> dict[str, Any]:
"""
TODO
"""

# Load file
data = tomlkit.loads(path.read_text(encoding="utf-8"))

Expand All @@ -38,11 +49,12 @@ class Config:
"""

def __init__(self):
self.pyproject_data = _create_pyproject()
path = _find_pyproject_file()
self.pyproject_data = _create_pyproject(path)

configuration = InterfaceConfiguration()
self.interface = ConsoleInterface(configuration)
self.configuration = ProjectConfiguration()
self.configuration = ProjectConfiguration(root_path=path)

def create_project(self) -> Project:
"""
Expand Down
70 changes: 53 additions & 17 deletions cppython/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

from cppython_core.core import cppython_logger
from cppython_core.schema import (
PEP621,
CPPythonData,
CPPythonDataT,
Generator,
GeneratorConfiguration,
Interface,
Expand All @@ -19,7 +21,7 @@
from pydantic import create_model

from cppython.schema import API, ProjectConfiguration
from cppython.utility import write_preset, write_presets
from cppython.utility import write_presets


class ProjectBuilder:
Expand Down Expand Up @@ -73,17 +75,41 @@ def generate_model(self, plugins: list[Type[Generator]]) -> Type[PyProject]:
)

def create_generators(
self, plugins: list[Type[Generator]], configuration: GeneratorConfiguration, pyproject: PyProject
self,
plugins: list[Type[Generator]],
configuration: GeneratorConfiguration,
project: PEP621,
cppython: CPPythonData,
) -> list[Generator]:
"""
TODO
"""
_generators = []
for plugin_type in plugins:
_generators.append(plugin_type(configuration, pyproject))
_generators.append(plugin_type(configuration, project, cppython))

return _generators

def generate_modified(self, original: CPPythonDataT) -> CPPythonDataT:
"""
Applies dynamic behaviors of the settings to itself
Returns a copy of the original with dynamic modifications
"""
modified = original.copy(deep=True)

# Add the pyproject.toml location to all relative paths

if not modified.install_path.is_absolute():
modified.install_path = self.configuration.root_path.absolute() / modified.install_path

if not modified.tool_path.is_absolute():
modified.tool_path = self.configuration.root_path.absolute() / modified.tool_path

if not modified.build_path.is_absolute():
modified.build_path = self.configuration.root_path.absolute() / modified.build_path

return modified


class Project(API):
"""
Expand All @@ -96,7 +122,6 @@ def __init__(

self._enabled = False
self._configuration = configuration
self._pyproject = None

levels = [logging.WARNING, logging.INFO, logging.DEBUG]

Expand All @@ -118,26 +143,30 @@ def __init__(
cppython_logger.warning(f"Generator plugin found: {plugin.name()}")

extended_pyproject_type = builder.generate_model(plugins)
self._pyproject = extended_pyproject_type(**pyproject_data)
pyproject = extended_pyproject_type(**pyproject_data)

if self.pyproject is None:
if pyproject is None:
cppython_logger.error("Data is not defined")
return

if self.pyproject.tool is None:
if pyproject.tool is None:
cppython_logger.error("Table [tool] is not defined")
return

if self.pyproject.tool.cppython is None:
if pyproject.tool.cppython is None:
cppython_logger.error("Table [tool.cppython] is not defined")
return

self._enabled = True

self._project = pyproject.project

self._modified_cppython_data = builder.generate_modified(pyproject.tool.cppython)

self._interface = interface

generator_configuration = GeneratorConfiguration()
self._generators = builder.create_generators(plugins, generator_configuration, self.pyproject)
self._generators = builder.create_generators(plugins, generator_configuration, self.project, self.cppython)

cppython_logger.info("Initialized project successfully")

Expand All @@ -156,11 +185,18 @@ def configuration(self) -> ProjectConfiguration:
return self._configuration

@property
def pyproject(self) -> PyProject | None:
def project(self):
"""
TODO
The pyproject project table
"""
return self._project

@property
def cppython(self):
"""
The resolved CPPython data
"""
return self._pyproject
return self._modified_cppython_data

def download(self):
"""
Expand All @@ -170,7 +206,7 @@ def download(self):
cppython_logger.info("Skipping download because the project is not enabled")
return

base_path = self.pyproject.tool.cppython.install_path
base_path = self.cppython.install_path

for generator in self._generators:

Expand Down Expand Up @@ -199,7 +235,7 @@ def install(self) -> None:
cppython_logger.info("Installing project")
self.download()

tool_path = self.pyproject.tool.cppython.tool_path
tool_path = self.cppython.tool_path
tool_path.mkdir(parents=True, exist_ok=True)

generator_output = []
Expand All @@ -209,7 +245,7 @@ def install(self) -> None:
cppython_logger.info(f"Installing {generator.name()} generator")

try:
toolchain_path = generator.install()
toolchain_path = generator.install().absolute()
generator_output.append((generator.name(), toolchain_path))
except Exception as exception:
cppython_logger.error(f"Generator {generator.name()} failed to install")
Expand All @@ -227,7 +263,7 @@ def update(self) -> None:

cppython_logger.info("Updating project")

tool_path = self.pyproject.tool.cppython.tool_path
tool_path = self.cppython.tool_path
tool_path.mkdir(parents=True, exist_ok=True)

generator_output = []
Expand All @@ -237,7 +273,7 @@ def update(self) -> None:
cppython_logger.info(f"Updating {generator.name()} generator")

try:
toolchain_path = generator.update()
toolchain_path = generator.update().absolute()
generator_output.append((generator.name(), toolchain_path))
except Exception as exception:
cppython_logger.error(f"Generator {generator.name()} failed to update")
Expand Down
11 changes: 9 additions & 2 deletions cppython/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ class ConfigurePreset(Preset):
toolchainFile: Optional[str]

@validator("toolchainFile")
def validate_path(cls, v):
return Path(v).as_posix()
def validate_path(cls, value): # pylint: disable=E0213
"""
TODO
"""
return Path(value).as_posix()


class BuildPreset(Preset):
Expand Down Expand Up @@ -79,6 +82,9 @@ class CMakePresets(BaseModel, extra=Extra.forbid):

@validator("include")
def validate_path(cls, v):
"""
TODO
"""
output = []
for value in v:
output.append(Path(value).as_posix())
Expand All @@ -91,6 +97,7 @@ class ProjectConfiguration:
TODO
"""

root_path: Path # The path where the pyproject.toml lives
_verbosity: int = 0

@property
Expand Down
Loading