2
2
The central delegation of the CPPython project
3
3
"""
4
4
5
+ import logging
5
6
from dataclasses import dataclass
6
7
from importlib import metadata
7
- from pathlib import Path
8
8
from typing import Any , Type , TypeVar
9
- from xmlrpc .client import Boolean
10
9
11
10
from cppython_core .schema import (
12
11
API ,
13
12
CPPythonData ,
14
13
Generator ,
14
+ GeneratorConfiguration ,
15
15
Interface ,
16
16
Plugin ,
17
17
PyProject ,
@@ -26,7 +26,21 @@ class ProjectConfiguration:
26
26
TODO
27
27
"""
28
28
29
- verbose : Boolean = False
29
+ _verbosity : int = 0
30
+
31
+ @property
32
+ def verbosity (self ) -> int :
33
+ """
34
+ TODO
35
+ """
36
+ return self ._verbosity
37
+
38
+ @verbosity .setter
39
+ def verbosity (self , value : int ) -> None :
40
+ """
41
+ TODO
42
+ """
43
+ self ._verbosity = min (max (value , 0 ), 2 )
30
44
31
45
32
46
class ProjectBuilder :
@@ -79,13 +93,15 @@ def generate_model(self, plugins: list[Type[Generator]]) -> Type[PyProject]:
79
93
__base__ = PyProject ,
80
94
)
81
95
82
- def create_generators (self , plugins : list [Type [Generator ]], pyproject : PyProject ) -> list [Generator ]:
96
+ def create_generators (
97
+ self , plugins : list [Type [Generator ]], configuration : GeneratorConfiguration , pyproject : PyProject
98
+ ) -> list [Generator ]:
83
99
"""
84
100
TODO
85
101
"""
86
102
_generators = []
87
103
for plugin_type in plugins :
88
- _generators .append (plugin_type (pyproject ))
104
+ _generators .append (plugin_type (configuration , pyproject ))
89
105
90
106
return _generators
91
107
@@ -100,39 +116,69 @@ def __init__(
100
116
) -> None :
101
117
102
118
self ._enabled = False
103
- self .configuration = configuration
119
+ self ._configuration = configuration
120
+ self ._pyproject = None
121
+
122
+ levels = [logging .WARNING , logging .INFO , logging .DEBUG ]
123
+
124
+ self ._logger = logging .getLogger ("cppython" )
125
+ self ._logger .setLevel (levels [configuration .verbosity ])
104
126
105
- if self .configuration .verbose :
106
- interface .print ("Starting CPPython project initialization" )
127
+ interface .register_logger (self ._logger )
128
+
129
+ self ._logger .info ("Initializing project" )
107
130
108
131
builder = ProjectBuilder (self .configuration )
109
132
plugins = builder .gather_plugins (Generator )
110
133
111
134
if not plugins :
112
- if self .configuration .verbose :
113
- interface .print ("No generator plugin was found." )
135
+ self ._logger .info ("No generator plugin was found" )
114
136
return
115
137
116
138
extended_pyproject_type = builder .generate_model (plugins )
117
- self .pyproject = extended_pyproject_type (** pyproject_data )
139
+ self ._pyproject = extended_pyproject_type (** pyproject_data )
140
+
141
+ if self .pyproject is None :
142
+ self ._logger .info ("Data is not defined" )
143
+ return
118
144
119
145
if self .pyproject .tool is None :
120
- if self .configuration .verbose :
121
- interface .print ("Table [tool] is not defined" )
146
+ self ._logger .info ("Table [tool] is not defined" )
122
147
return
123
148
124
149
if self .pyproject .tool .cppython is None :
125
- if self .configuration .verbose :
126
- interface .print ("Table [tool.cppython] is not defined" )
150
+ self ._logger .info ("Table [tool.cppython] is not defined" )
127
151
return
128
152
129
153
self ._enabled = True
130
154
131
155
self ._interface = interface
132
- self ._generators = builder .create_generators (plugins , self .pyproject )
133
156
134
- if self .configuration .verbose :
135
- interface .print ("CPPython project initialized" )
157
+ generator_configuration = GeneratorConfiguration (self ._logger )
158
+ self ._generators = builder .create_generators (plugins , generator_configuration , self .pyproject )
159
+
160
+ self ._logger .info ("Initialized project" )
161
+
162
+ @property
163
+ def enabled (self ) -> bool :
164
+ """
165
+ TODO
166
+ """
167
+ return self ._enabled
168
+
169
+ @property
170
+ def configuration (self ) -> ProjectConfiguration :
171
+ """
172
+ TODO
173
+ """
174
+ return self ._configuration
175
+
176
+ @property
177
+ def pyproject (self ) -> PyProject | None :
178
+ """
179
+ TODO
180
+ """
181
+ return self ._pyproject
136
182
137
183
def download (self ):
138
184
"""
@@ -148,35 +194,32 @@ def download(self):
148
194
path .mkdir (parents = True , exist_ok = True )
149
195
150
196
if not generator .generator_downloaded (path ):
151
- self ._interface . print (f"Downloading the { generator .name ()} tool" )
197
+ self ._logger . info (f"Downloading the { generator .name ()} tool" )
152
198
153
199
# TODO: Make async with progress bar
154
200
generator .download_generator (path )
155
- self ._interface . print ("Download complete" )
201
+ self ._logger . info ("Download complete" )
156
202
157
203
# API Contract
158
204
159
205
def install (self ) -> None :
160
206
if self ._enabled :
161
- if self .configuration .verbose :
162
- self ._interface .print ("CPPython: Installing..." )
207
+ self ._logger .info ("Installing project" )
163
208
self .download ()
164
209
165
210
for generator in self ._generators :
166
211
generator .install ()
167
212
168
213
def update (self ) -> None :
169
214
if self ._enabled :
170
- if self .configuration .verbose :
171
- self ._interface .print ("CPPython: Updating..." )
215
+ self ._logger .info ("Updating project" )
172
216
173
217
for generator in self ._generators :
174
218
generator .update ()
175
219
176
220
def build (self ) -> None :
177
221
if self ._enabled :
178
- if self .configuration .verbose :
179
- self ._interface .print ("CPPython: Building..." )
222
+ self ._logger .info ("Building project" )
180
223
181
224
for generator in self ._generators :
182
225
generator .build ()
0 commit comments