Skip to content

Commit 22723fa

Browse files
authored
Download Abstraction (#25)
Added download abstraction with interface printing
1 parent f0d57d1 commit 22723fa

File tree

5 files changed

+52
-19
lines changed

5 files changed

+52
-19
lines changed

cppython/plugins/generator/cmake.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ def data_type() -> Type[GeneratorData]:
3434
"""
3535
return CMakeData
3636

37-
def install_generator(self) -> bool:
37+
def downloaded(self) -> bool:
38+
return True
39+
40+
def download(self) -> None:
3841
"""
3942
Installs the external tooling required by the generator if necessary
4043
Returns whether anything was installed or not
4144
"""
42-
return False
4345

4446
def install(self) -> None:
4547
raise NotImplementedError()

cppython/plugins/interface/console.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,6 @@ def write_pyproject(self) -> None:
109109
"""
110110
Write output
111111
"""
112-
pass
112+
113+
def print(self, string: str) -> None:
114+
click.echo(string)

cppython/plugins/test/data.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
default_pep621 = PEP621(name="test_name", version="1.0")
1010

1111
# CMake is a default plugin
12-
# TODO: Provide dynamic default
13-
default_cppython_data = CPPythonData(generator="cmake", target=TargetEnum.EXE, install_path=Path())
12+
default_cppython_data = CPPythonData(**{"generator": "cmake", "target": TargetEnum.EXE, "install-path": Path()})
1413

15-
default_pyproject = PyProject(pep_621=default_pep621, cppython_data=default_cppython_data)
14+
default_pyproject = PyProject(project=default_pep621, cppython=default_cppython_data)

cppython/project.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Project(API):
1616

1717
def __init__(self, interface: Interface, pyproject: PyProject) -> None:
1818

19+
self.enabled = pyproject.cppython != None
20+
1921
self._interface = interface
2022

2123
PluginType = TypeVar("PluginType", bound=Type[Plugin])
@@ -35,22 +37,36 @@ def find_plugin_type(plugin_type: PluginType, condition: Callable[[str], bool])
3537

3638
return None
3739

38-
plugin_type = find_plugin_type(Generator, lambda name: name == pyproject.cppython_data.generator)
40+
plugin_type = find_plugin_type(Generator, lambda name: name == pyproject.cppython.generator)
3941

4042
if plugin_type is None:
41-
raise ConfigError(f"No generator plugin with the name '{pyproject.cppython_data.generator}' was found.")
43+
raise ConfigError(f"No generator plugin with the name '{pyproject.cppython.generator}' was found.")
4244

4345
generator_data = interface.read_generator_data(plugin_type.data_type())
4446
self._generator = plugin_type(pyproject, generator_data)
45-
self._generator.install_generator()
47+
48+
def download(self):
49+
"""
50+
Download the generator tooling if required
51+
"""
52+
if not self._generator.downloaded():
53+
self._interface.print(f"Downloading the {self._generator.name()} tool")
54+
55+
# TODO: Make async with progress bar
56+
self._generator.download()
57+
self._interface.print("Download complete")
4658

4759
# API Contract
4860

4961
def install(self) -> None:
50-
self._generator.install()
62+
if self.enabled:
63+
self.download()
64+
self._generator.install()
5165

5266
def update(self) -> None:
53-
self._generator.update()
67+
if self.enabled:
68+
self._generator.update()
5469

5570
def build(self) -> None:
56-
self._generator.build()
71+
if self.enabled:
72+
self._generator.build()

cppython/schema.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from abc import ABC, abstractmethod
66
from enum import Enum
77
from pathlib import Path
8-
from typing import Type, TypeVar
8+
from typing import Optional, Type, TypeVar
99

1010
from pydantic import BaseModel
11+
from pydantic.fields import Field
1112

1213

1314
class TargetEnum(Enum):
@@ -40,16 +41,16 @@ class CPPythonData(BaseModel):
4041
generator: str
4142
target: TargetEnum
4243
dependencies: dict[str, str] = {}
43-
install_path: Path
44+
install_path: Path = Field(alias="install-path")
4445

4546

4647
class PyProject(BaseModel):
4748
"""
4849
pyproject.toml schema
4950
"""
5051

51-
pep_621: PEP621
52-
cppython_data: CPPythonData
52+
project: PEP621
53+
cppython: Optional[CPPythonData]
5354

5455

5556
class API(ABC):
@@ -131,6 +132,13 @@ def write_pyproject(self) -> None:
131132
"""
132133
raise NotImplementedError()
133134

135+
@abstractmethod
136+
def print(self, string: str) -> None:
137+
"""
138+
Prints the given string into the Interface IO
139+
"""
140+
raise NotImplementedError()
141+
134142

135143
class Generator(Plugin, API):
136144
"""
@@ -165,9 +173,15 @@ def data_type() -> Type[GeneratorData]:
165173
raise NotImplementedError()
166174

167175
@abstractmethod
168-
def install_generator(self) -> bool:
176+
def downloaded(self) -> bool:
177+
"""
178+
Returns whether the generator needs to be downloaded
179+
"""
180+
raise NotImplementedError()
181+
182+
@abstractmethod
183+
def download(self) -> None:
169184
"""
170-
Installs the external tooling required by the generator if necessary
171-
Returns whether anything was installed or not
185+
Installs the external tooling required by the generator
172186
"""
173187
raise NotImplementedError()

0 commit comments

Comments
 (0)