Skip to content

Commit 6970baa

Browse files
authored
Simplify Implementation + Minor Document (#18)
Simplified the internal and plugin facing code.
1 parent 3f4f784 commit 6970baa

File tree

19 files changed

+624
-481
lines changed

19 files changed

+624
-481
lines changed

.vscode/settings.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
"python.testing.pytestArgs": [
66
"tests"
77
],
8+
"python.formatting.provider": "black",
9+
"python.formatting.blackPath": "${workspaceRoot}/__pypackages__/3.10/Scripts/black",
810
"python.testing.unittestEnabled": false,
911
"python.testing.pytestEnabled": true,
10-
"python.autoComplete.extraPaths": ["__pypackages__/<major.minor>/lib"],
11-
"python.analysis.extraPaths": ["__pypackages__/<major.minor>/lib"]
12+
"python.autoComplete.extraPaths": [
13+
"__pypackages__/3.10/lib"
14+
],
15+
"python.analysis.extraPaths": [
16+
"__pypackages__/3.10/lib"
17+
],
18+
"editor.formatOnSave": true,
19+
"editor.codeActionsOnSave": {
20+
"source.organizeImports": true
21+
},
22+
"python.analysis.typeCheckingMode": "basic"
1223
}

cppython/exceptions.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44

55

66
class ConfigError(Exception):
7-
def __init__(self, error: Exception) -> None:
7+
"""
8+
Raised when there is a configuration error
9+
"""
10+
11+
def __init__(self, error: str) -> None:
812
self._error = error
913

10-
super().__init__(str(error))
14+
super().__init__(error)
1115

1216
@property
13-
def error(self) -> Exception:
17+
def error(self) -> str:
18+
"""
19+
Returns the underlying error
20+
21+
Returns:
22+
str -- The underlying error
23+
"""
1424
return self._error

cppython/plugins/generator/cmake.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
from cppython.schema import Generator, Metadata
1+
"""
2+
The default generator implementation for CPPython
3+
"""
4+
from typing import Type
25

6+
from cppython.schema import Generator, GeneratorData, PyProject
37

4-
class CMakeGenerator(Generator):
8+
9+
class CMakeData(GeneratorData):
510
"""
6-
A CPPython generator implementing a CMake backend
11+
The data schema required for the CMake tooling
712
"""
813

9-
def __init__(self) -> None:
10-
pass
1114

15+
class CMakeGenerator(Generator):
1216
"""
13-
Plugin Contract
17+
A CPPython generator implementing a CMake backend
1418
"""
1519

20+
def __init__(self, pyproject: PyProject, cmake_data: CMakeData) -> None:
21+
super().__init__(pyproject, cmake_data)
22+
1623
@staticmethod
1724
def name() -> str:
1825
"""
1926
The name of the generator
2027
"""
2128
return "cmake"
2229

23-
"""
24-
Generator Contract
25-
"""
26-
27-
def populate_metadata(self, data: dict):
30+
@staticmethod
31+
def data_type() -> Type[GeneratorData]:
2832
"""
29-
data - The CPPoetry data taken from pyproject.toml
33+
Returns the pydantic type to cast the generator configuration data to
3034
"""
31-
pass
35+
return CMakeData
3236

33-
def populate_plugin(self, data: dict):
37+
def install_generator(self) -> bool:
3438
"""
35-
data - The data taken from pyproject.toml that belongs to this generator
39+
Installs the external tooling required by the generator if necessary
40+
Returns whether anything was installed or not
3641
"""
37-
pass
38-
39-
"""
40-
API Contract
41-
"""
42+
return False
4243

4344
def install(self) -> None:
4445
raise NotImplementedError()

cppython/plugins/interface/console.py

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,112 @@
1-
from cppython.project import Project
2-
from cppython.schema import Interface, PEP621
1+
"""
2+
A click CLI for CPPython interfacing
3+
"""
4+
35
from pathlib import Path
6+
from typing import Type
47

58
import click
69
import tomlkit
710

11+
from cppython.project import Project
12+
from cppython.schema import GeneratorData, GeneratorDataType, Interface, PyProject
13+
14+
15+
def _create_pyproject():
816

9-
def _read_data():
17+
# Search for a path upward
1018
path = Path.cwd()
1119

1220
while not path.glob("pyproject.toml"):
1321
if path.is_absolute():
1422
assert (
15-
"This is not a valid project. No pyproject.toml found in the current directory or any of its parents."
16-
)
23+
False
24+
), "This is not a valid project. No pyproject.toml found in the current directory or any of its parents."
1725

18-
return tomlkit.loads(Path(path / "pyproject.toml").read_text(encoding="utf-8"))
26+
path = Path(path / "pyproject.toml")
1927

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

21-
class Config(object):
31+
# Interpret and validate data
32+
return PyProject(**data)
33+
34+
35+
class Config:
2236
"""
2337
The data object that will be expanded alongside 'pass_obj'
2438
"""
2539

2640
def __init__(self):
27-
28-
data = _read_data()
41+
pyproject = _create_pyproject()
2942

3043
# Initialize the object hook into CPPython
31-
interface = ConsoleInterface(data)
44+
interface = ConsoleInterface(pyproject)
3245

3346
# Initialize the CPPython context
3447
self.project = Project(interface)
3548

36-
def load(self):
37-
self.project.load()
38-
3949

4050
pass_config = click.make_pass_decorator(Config)
4151

4252

4353
@click.group()
4454
@click.pass_context
4555
def cli(context):
56+
"""
57+
entry_point group for the CLI commands
58+
"""
4659
context.ensure_object(Config)
4760

48-
# Initialize cppython
49-
context.obj.load()
50-
5161

5262
@cli.command()
5363
@pass_config
5464
def install(config):
65+
"""
66+
Fulfills the 'install' API requirement
67+
"""
5568
config.project.install()
5669

5770

5871
@cli.command()
5972
@pass_config
6073
def update(config):
74+
"""
75+
Fulfills the 'update' API requirement
76+
"""
6177
config.project.update()
6278

6379

64-
@cli.result_callback()
80+
@cli.command()
6581
@pass_config
66-
def cleanup(config, result):
67-
pass
68-
69-
70-
class ConsoleInterface(Interface):
82+
def build(config):
7183
"""
72-
TODO: Description
84+
Fulfills the 'build' API requirement
7385
"""
86+
config.project.build()
7487

75-
def __init__(self, data: dict) -> None:
76-
self._data = data
7788

89+
@cli.result_callback()
90+
@pass_config
91+
def cleanup(config, result):
7892
"""
79-
Plugin Contract
93+
Post-command cleanup
8094
"""
8195

82-
@staticmethod
83-
def name() -> str:
84-
"""
85-
The name of the generator
86-
"""
87-
return "console"
8896

97+
class ConsoleInterface(Interface):
8998
"""
90-
Interface Contract
99+
Interface implementation to pass to the project
91100
"""
92101

93-
@staticmethod
94-
def external_config() -> bool:
95-
"""
96-
True if the plugin can read its own configuration.
97-
False otherwise
98-
"""
99-
100-
return False
101-
102-
@staticmethod
103-
def parse_pep_621(data: dict) -> PEP621:
102+
def read_generator_data(self, generator_data_type: Type[GeneratorDataType]) -> GeneratorDataType:
104103
"""
105-
Requests the plugin to read the available PEP 621 information. Only requested if the plugin is not the entrypoint
104+
Requests generator information
106105
"""
107-
raise NotImplementedError()
106+
return generator_data_type()
108107

109-
def pep_621(self) -> PEP621:
108+
def write_pyproject(self) -> None:
110109
"""
111-
Requests PEP 621 information from the pyproject
110+
Write output
112111
"""
113-
return self.parse_pep_621(self._data)
114-
115-
def write_pyproject(self) -> None:
116-
raise NotImplementedError()
117-
118-
def read_pyproject(self) -> dict:
119-
return self._data
112+
pass

cppython/plugins/test/data.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Defaulted data to help testing
3+
"""
4+
5+
from pathlib import Path
6+
7+
from cppython.schema import PEP621, CPPythonData, PyProject, TargetEnum
8+
9+
default_pep621 = PEP621(name="test_name", version="1.0")
10+
11+
# CMake is a default plugin
12+
# TODO: Provide dynamic default
13+
default_cppython_data = CPPythonData(generator="cmake", target=TargetEnum.EXE, install_path=Path())
14+
15+
default_pyproject = PyProject(pep_621=default_pep621, cppython_data=default_cppython_data)

0 commit comments

Comments
 (0)