-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_loader.py
78 lines (68 loc) · 2.7 KB
/
config_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import yaml
from pydantic import BaseModel, ValidationError, constr, conlist, condecimal
from typing import Optional
import pathlib
import logging
logger = logging.getLogger(__name__)
# Config schema
class AddressEntry(BaseModel):
address: constr(min_length=1)
name: constr(min_length=1)
minimum_balance: condecimal(ge=0)
class TransactionConfig(BaseModel):
enabled: bool
private_key_path: Optional[str] = None
interval: str = "60s"
class ConfigSchema(BaseModel):
rpc_url: constr(min_length=1)
graph_node_url: Optional[constr(min_length=1)] = None
graph_node_nft_url: Optional[constr(min_length=1)] = None
addresses: Optional[conlist(AddressEntry, min_length=1)] = None
providers: Optional[conlist(constr(min_length=1), min_length=1)] = None
transaction: Optional[TransactionConfig] = None
port: Optional[int] = 8001
diamond_address: Optional[constr(min_length=1)] = None
def load_config(config_file_path: str) -> ConfigSchema:
"""Load and validate the configuration file."""
try:
with open(config_file_path, 'r') as file:
config_data = yaml.safe_load(file)
config = ConfigSchema(**config_data)
logger.debug(
f"Successfully loaded and validated config from {config_file_path}")
return config
except FileNotFoundError:
logger.error(
f"Config file not found: {config_file_path}. Please provide a valid file path.")
exit(1)
except yaml.YAMLError as e:
logger.error(
f"Error parsing the YAML file: {config_file_path}. Error: {e}")
exit(1)
except ValidationError as e:
logger.error(f"Configuration validation error: {e.json()}")
exit(1)
except Exception as e:
logger.error(f"Unexpected error while loading config: {e}")
exit(1)
def load_private_key(private_key_path: Optional[str]) -> Optional[str]:
"""Load a private key from the specified file path."""
if private_key_path:
key_path = pathlib.Path(private_key_path)
if key_path.exists() and key_path.is_file():
try:
with open(key_path, 'r') as key_file:
private_key = key_file.read().strip()
logger.debug(
f"Successfully loaded private key from {private_key_path}")
return private_key
except Exception as e:
logger.error(
f"Error reading private key from {private_key_path}: {e}")
raise
else:
logger.error(
f"Private key file does not exist or is not a file: {private_key_path}")
return None
else:
return None