config
This module contains the configuration for the devices RAP project. It includes setup for logging, directory creation, and dataset paths. It also provides a Config class to manage dataset configurations and paths.
Classes:
| Name | Description |
|---|---|
ConfigError |
Custom exception class for configuration errors. |
Config |
Configuration class for the project, managing financial month/year, dataset paths, and Amber Report Excel configuration. |
Functions:
| Name | Description |
|---|---|
config_logger |
Configures the logging for the project using loguru. |
create_directory |
Creates directories if they do not already exist. |
ConfigError
Bases: LoggedException
Custom exception class for configuration errors. This class is used to raise exceptions related to configuration issues.
Config
Configuration class for the project.
This class manages dataset configurations, paths, and provides a context manager interface for pipeline execution with robust error handling and cleanup.
The context manager ensures that: - Resources (like SQL connections) are properly cleaned up - Errors are comprehensively logged with context information - Exceptions are allowed to propagate for proper error handling - Cleanup occurs even when exceptions happen during execution
Examples:
Basic usage as a context manager:
>>> with Config(fin_month='01', fin_year='2425') as config:
... # Pipeline operations here
... datasets = load_data(config)
Source code in devices_rap/config.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
__init__(fin_month, fin_year, use_multiprocessing=True, mode='local', outputs='excel', raw_data_dir=RAW_DATA_DIR, processed_data_dir=PROCESSED_DATA_DIR, amber_report_excel_config_path=AMBER_REPORT_EXCEL_CONFIG_PATH, master_devices_csv_name=MASTER_DEVICES_CSV_NAME, exceptions_csv_name=EXCEPTIONS_CSV_NAME, provider_codes_lookup_csv_name=PROVIDER_CODES_LOOKUP_CSV_NAME, device_taxonomy_csv_name=DEVICE_TAXONOMY_CSV_NAME, sql_dir=SQL_DIR, master_device_sql_name=MASTER_DEVICES_SQL_NAME, provider_codes_lookup_sql_name=PROVIDER_CODES_LOOKUP_SQL_NAME)
Initializes the Config class with the provided parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fin_month
|
FIN_MONTHS
|
The financial month string in the format "MM" (e.g., "12") with 01 referring to April. |
required |
fin_year
|
FIN_YEARS
|
The financial year string in the format "YY1YY2" (e.g., "2425"). |
required |
raw_data_dir
|
Path
|
The directory where raw data is stored. Defaults to the RAW_DATA_DIR constant. |
RAW_DATA_DIR
|
amber_report_excel_config_path
|
Path
|
The path to the Amber Report Excel configuration file. Defaults to the AMBER_REPORT_EXCEL_CONFIG_PATH constant. |
AMBER_REPORT_EXCEL_CONFIG_PATH
|
master_devices_csv_name
|
str
|
The name of the master devices CSV file. Defaults to the MASTER_DEVICES_CSV_NAME constant. |
MASTER_DEVICES_CSV_NAME
|
exceptions_csv_name
|
str
|
The name of the exceptions CSV file. Defaults to the EXCEPTIONS_CSV_NAME constant. |
EXCEPTIONS_CSV_NAME
|
provider_codes_lookup_csv_name
|
str
|
The name of the provider codes lookup CSV file. Defaults to the PROVIDER_CODES_LOOKUP_CSV_NAME constant. |
PROVIDER_CODES_LOOKUP_CSV_NAME
|
device_taxonomy_csv_name
|
str
|
The name of the device taxonomy CSV file. Defaults to the DEVICE_TAXONOMY_CSV_NAME constant. |
DEVICE_TAXONOMY_CSV_NAME
|
Source code in devices_rap/config.py
close()
Closes the SQL Server connection if it exists.
Source code in devices_rap/config.py
__enter__()
Enter the runtime context related to this object. This method is called when the Config instance is used in a 'with' statement. It returns the Config instance itself.
__exit__(exc_type, exc_value, traceback)
Exit the runtime context related to this object. Ensures cleanup happens and allows exceptions to propagate.
Source code in devices_rap/config.py
get(key)
Get the value of a configuration key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key for which to retrieve the value. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The value associated with the key, or None if the key does not exist. |
Source code in devices_rap/config.py
create_output_directory()
Creates the output directory for the Amber Report Excel files based on the financial month and year.
Returns:
| Type | Description |
|---|---|
Path
|
The path to the created output directory. |
Source code in devices_rap/config.py
config_logger()
Set up logging configuration for the project using loguru. This function configures loguru to log messages to both the console and a file. It also ensures that the log file is created in the 'logs' directory with a timestamped filename.
Source code in devices_rap/config.py
create_directory(directory_path)
Creates directories if they do not already exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory_path
|
Path | List[Path]
|
A single Path object or a list of Path objects representing the directories to be created. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided directory_path list is empty, a warning is logged and the function returns without creating any directories. |