Skip to content

Integrate Initial Logger Support #43

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 1 commit into from
Apr 17, 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
11 changes: 5 additions & 6 deletions cppython/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
A click CLI for CPPython interfacing
"""

from logging import Logger
from pathlib import Path
from typing import Any, Type
from xmlrpc.client import Boolean

import click
import tomlkit
Expand Down Expand Up @@ -54,13 +54,13 @@ def create_project(self) -> Project:


@click.group()
@click.option("-v", "--verbose", is_flag=True, help="Print additional output")
@click.option("-v", "--verbose", count=True, help="Print additional output")
@pass_config
def cli(config, verbose: Boolean):
def cli(config, verbose: int):
"""
entry_point group for the CLI commands
"""
config.configuration.verbose = verbose
config.configuration.verbosity = verbose


@cli.command()
Expand Down Expand Up @@ -118,8 +118,7 @@ def write_pyproject(self) -> None:
Write output
"""

def print(self, string: str) -> None:
def register_logger(self, logger: Logger) -> None:
"""
TODO
"""
click.echo(string)
95 changes: 69 additions & 26 deletions cppython/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
The central delegation of the CPPython project
"""

import logging
from dataclasses import dataclass
from importlib import metadata
from pathlib import Path
from typing import Any, Type, TypeVar
from xmlrpc.client import Boolean

from cppython_core.schema import (
API,
CPPythonData,
Generator,
GeneratorConfiguration,
Interface,
Plugin,
PyProject,
Expand All @@ -26,7 +26,21 @@ class ProjectConfiguration:
TODO
"""

verbose: Boolean = False
_verbosity: int = 0

@property
def verbosity(self) -> int:
"""
TODO
"""
return self._verbosity

@verbosity.setter
def verbosity(self, value: int) -> None:
"""
TODO
"""
self._verbosity = min(max(value, 0), 2)


class ProjectBuilder:
Expand Down Expand Up @@ -79,13 +93,15 @@ def generate_model(self, plugins: list[Type[Generator]]) -> Type[PyProject]:
__base__=PyProject,
)

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

return _generators

Expand All @@ -100,39 +116,69 @@ def __init__(
) -> None:

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

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

self._logger = logging.getLogger("cppython")
self._logger.setLevel(levels[configuration.verbosity])

if self.configuration.verbose:
interface.print("Starting CPPython project initialization")
interface.register_logger(self._logger)

self._logger.info("Initializing project")

builder = ProjectBuilder(self.configuration)
plugins = builder.gather_plugins(Generator)

if not plugins:
if self.configuration.verbose:
interface.print("No generator plugin was found.")
self._logger.info("No generator plugin was found")
return

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

if self.pyproject is None:
self._logger.info("Data is not defined")
return

if self.pyproject.tool is None:
if self.configuration.verbose:
interface.print("Table [tool] is not defined")
self._logger.info("Table [tool] is not defined")
return

if self.pyproject.tool.cppython is None:
if self.configuration.verbose:
interface.print("Table [tool.cppython] is not defined")
self._logger.info("Table [tool.cppython] is not defined")
return

self._enabled = True

self._interface = interface
self._generators = builder.create_generators(plugins, self.pyproject)

if self.configuration.verbose:
interface.print("CPPython project initialized")
generator_configuration = GeneratorConfiguration(self._logger)
self._generators = builder.create_generators(plugins, generator_configuration, self.pyproject)

self._logger.info("Initialized project")

@property
def enabled(self) -> bool:
"""
TODO
"""
return self._enabled

@property
def configuration(self) -> ProjectConfiguration:
"""
TODO
"""
return self._configuration

@property
def pyproject(self) -> PyProject | None:
"""
TODO
"""
return self._pyproject

def download(self):
"""
Expand All @@ -148,35 +194,32 @@ def download(self):
path.mkdir(parents=True, exist_ok=True)

if not generator.generator_downloaded(path):
self._interface.print(f"Downloading the {generator.name()} tool")
self._logger.info(f"Downloading the {generator.name()} tool")

# TODO: Make async with progress bar
generator.download_generator(path)
self._interface.print("Download complete")
self._logger.info("Download complete")

# API Contract

def install(self) -> None:
if self._enabled:
if self.configuration.verbose:
self._interface.print("CPPython: Installing...")
self._logger.info("Installing project")
self.download()

for generator in self._generators:
generator.install()

def update(self) -> None:
if self._enabled:
if self.configuration.verbose:
self._interface.print("CPPython: Updating...")
self._logger.info("Updating project")

for generator in self._generators:
generator.update()

def build(self) -> None:
if self._enabled:
if self.configuration.verbose:
self._interface.print("CPPython: Building...")
self._logger.info("Building project")

for generator in self._generators:
generator.build()
21 changes: 11 additions & 10 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ requires-python = ">=3.10"
dependencies = [
"click>=8.1.2",
"tomlkit>=0.10.1",
"cppython-core>=0.2.1",
"cppython-core>=0.3.2",
"pydantic>=1.9.0",
]

Expand All @@ -40,7 +40,7 @@ test = [
"pytest>=7.1.1",
"pytest-cov>=3.0.0",
"pytest-mock>=3.7.0",
"pytest-cppython>=0.1.4",
"pytest-cppython>=0.1.6",
]

[project.scripts]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ def test_verbosity(self):

assert result.exit_code == 0

assert config.configuration.verbose
assert config.configuration.verbosity
17 changes: 12 additions & 5 deletions tests/unit/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
Test the functions related to the internal interface implementation and the 'Interface' interface itself
"""

from pathlib import Path

from cppython_core.schema import Generator, GeneratorData, PyProject
import logging

from cppython_core.schema import (
Generator,
GeneratorConfiguration,
GeneratorData,
PyProject,
)
from pytest_mock import MockerFixture

from cppython.data import default_pyproject
Expand Down Expand Up @@ -85,11 +90,13 @@ def test_generator_creation(self, mocker: MockerFixture):

configuration = ProjectConfiguration()
builder = ProjectBuilder(configuration)
generators = builder.create_generators([], default_pyproject)

generator_configuration = GeneratorConfiguration(logging.getLogger(__name__))
generators = builder.create_generators([], generator_configuration, default_pyproject)

assert not generators

generator = mocker.Mock(spec=Generator)
generators = builder.create_generators([generator], default_pyproject)
generators = builder.create_generators([generator], generator_configuration, default_pyproject)

assert len(generators) == 1