|
| 1 | +import pathlib |
1 | 2 | import yaml
|
2 |
| - |
3 | 3 | from pydantic import BaseSettings, Field
|
4 |
| -import pathlib |
| 4 | + |
5 | 5 |
|
6 | 6 | # Default config value
|
7 | 7 | dir_path = pathlib.Path().absolute()
|
8 | 8 |
|
| 9 | + |
9 | 10 | # Port BaseModel
|
10 | 11 | class PortConfig(BaseSettings):
|
11 |
| - host: str = Field(default='localhost') |
| 12 | + host: str = Field(default="localhost") |
12 | 13 | port: int = Field(default=8000)
|
13 | 14 |
|
14 | 15 |
|
15 | 16 | # Report BaseModel
|
16 | 17 | class ReportConfig(BaseSettings):
|
17 |
| - format: str = Field(default='pdf') |
18 |
| - renderer: str = Field(default='weasyprint') |
| 18 | + format: str = Field(default="pdf") |
| 19 | + renderer: str = Field(default="weasyprint") |
19 | 20 | outputDir: str = Field(dir_path)
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | # Logging BaseModel
|
23 | 24 | class LoggingConfig(BaseSettings):
|
24 |
| - level: str = Field(default='info') |
| 25 | + level: str = Field(default="info") |
25 | 26 |
|
26 | 27 |
|
27 | 28 | # ConfigFile BaseModel
|
28 | 29 | class Config(BaseSettings):
|
29 |
| - daemon: PortConfig |
| 30 | + daemon: PortConfig |
30 | 31 | reports: ReportConfig
|
31 | 32 | logging: LoggingConfig
|
32 | 33 |
|
33 |
| -def load_config(path) -> Config: |
| 34 | + |
| 35 | +def load_config(path) -> dict: |
34 | 36 | try:
|
35 | 37 | # If Path does not exist in command, set default config value
|
36 | 38 | conf_obj = Config(
|
37 |
| - daemon=PortConfig( |
38 |
| - host='localhost', |
39 |
| - port=8000 |
40 |
| - ), |
| 39 | + daemon=PortConfig(host="localhost", port=8000), |
41 | 40 | reports=ReportConfig(
|
42 |
| - format='pdf', |
43 |
| - renderer='weasyprint', |
44 |
| - outputDir='~/.pebblo' |
| 41 | + format="pdf", renderer="weasyprint", outputDir="~/.pebblo" |
45 | 42 | ),
|
46 |
| - logging=LoggingConfig( |
47 |
| - level='info' |
48 |
| - ) |
| 43 | + logging=LoggingConfig(level="info"), |
49 | 44 | )
|
50 | 45 | if not path:
|
51 | 46 | # Setting Default config details
|
52 | 47 | return conf_obj.dict()
|
53 | 48 |
|
54 | 49 | # If Path exist, set config value
|
55 |
| - else: |
56 |
| - con_file = path |
57 |
| - try: |
58 |
| - with open(con_file, "r") as output: |
59 |
| - cred_json = yaml.safe_load(output) |
60 |
| - parsed_config = Config.parse_obj(cred_json) |
61 |
| - config_dict = parsed_config.dict() |
62 |
| - return config_dict |
63 |
| - except IOError as err: |
64 |
| - print(f"no credentials file found at {con_file}. Error : {err}") |
65 |
| - return conf_obj.dict() |
| 50 | + con_file = path |
| 51 | + try: |
| 52 | + with open(con_file, "r") as output: |
| 53 | + cred_json = yaml.safe_load(output) |
| 54 | + parsed_config = Config.parse_obj(cred_json) |
| 55 | + config_dict = parsed_config.dict() |
| 56 | + return config_dict |
| 57 | + except IOError as err: |
| 58 | + print(f"no credentials file found at {con_file}. Error : {err}") |
| 59 | + return conf_obj.dict() |
66 | 60 |
|
67 | 61 | except Exception as err:
|
68 |
| - print(f'Error while loading config details, err: {err}') |
69 |
| - |
70 |
| - |
| 62 | + print(f"Error while loading config details, err: {err}") |
0 commit comments