-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconfig.py
More file actions
132 lines (104 loc) · 4.36 KB
/
Copy pathconfig.py
File metadata and controls
132 lines (104 loc) · 4.36 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from __future__ import annotations
import enum
import logging
import shutil
import sys
from pathlib import Path
from typing import Any
import yaml
from pydantic import BaseSettings, root_validator, validator
logger = logging.getLogger("ClimateToken")
class ExecutionMode(enum.Enum):
DEV = "dev"
REGISTRY = "registry"
CLIENT = "client"
EXPLORER = "explorer"
class ServerPort(enum.Enum):
DEV = 31999
CLIMATE_WAREHOUSE = 31310
CLIMATE_PORTAL = 31311
CLIMATE_TOKEN_REGISTRY = 31312
CLIMATE_EXPLORER = 31313
CLIMATE_TOKEN_CLIENT = 31314
class Settings(BaseSettings):
_HIDDEN_FIELDS = ["MODE", "CHIA_ROOT", "CONFIG_PATH", "SERVER_PORT"]
# Hidden configs: not exposed in config.yaml
MODE: ExecutionMode
CHIA_ROOT: Path = Path("~/.chia/mainnet")
CONFIG_PATH: Path = Path("climate_token/config/config.yaml")
SERVER_PORT: int | None = 31999
# Visible configs: configurable through config.yaml
LOG_PATH: Path = Path("climate_token/log/debug.log")
DB_PATH: Path = Path("climate_explorer/db/climate_activity_CHALLENGE.sqlite")
CLIMATE_EXPLORER_SERVER_HOST: str = "0.0.0.0"
BLOCK_START: int = 1_500_000
BLOCK_RANGE: int = 10_000
MIN_DEPTH: int = 4
# we always look back ~36 hours since the climate warehouse waits 24 hours before
# setting metadata to be readable
LOOKBACK_DEPTH: int = 6_912
# fee is in mojos
DEFAULT_FEE: int = 1_000_000_000
CADT_API_SERVER_HOST: str = "https://observer.climateactiondata.org/api"
CADT_API_KEY: str | None = None
CHIA_HOSTNAME: str = "localhost"
CHIA_WALLET_HOSTNAME: str | None = None
CHIA_FULL_NODE_HOSTNAME: str | None = None
CHIA_FULL_NODE_RPC_PORT: int = 8555
CHIA_WALLET_RPC_PORT: int = 9256
CLIMATE_EXPLORER_PORT: int | None = None
CLIMATE_TOKEN_CLIENT_PORT: int | None = None
CLIMATE_TOKEN_REGISTRY_PORT: int | None = None
DEV_PORT: int | None = None
SCAN_ALL_ORGANIZATIONS: bool | None = False
_instance: Settings | None = None
@classmethod
def get_instance(cls) -> Settings:
if cls._instance is None:
cls._instance = get_settings()
return cls._instance
@root_validator(skip_on_failure=True)
def configure_port(cls, values: dict[str, Any]) -> dict[str, Any]:
if values["MODE"] == ExecutionMode.REGISTRY:
values["SERVER_PORT"] = values.get("CLIMATE_TOKEN_REGISTRY_PORT", ServerPort.CLIMATE_TOKEN_REGISTRY.value)
elif values["MODE"] == ExecutionMode.CLIENT:
values["SERVER_PORT"] = values.get("CLIMATE_TOKEN_CLIENT_PORT", ServerPort.CLIMATE_TOKEN_CLIENT.value)
elif values["MODE"] == ExecutionMode.EXPLORER:
values["SERVER_PORT"] = values.get("CLIMATE_EXPLORER_PORT", ServerPort.CLIMATE_EXPLORER.value)
elif values["MODE"] == ExecutionMode.DEV:
values["SERVER_PORT"] = values.get("DEV_PORT", ServerPort.DEV.value)
else:
raise ValueError(f"Invalid mode {values['MODE']}!")
return values
@validator("CHIA_ROOT", pre=True)
def expand_root(cls, v: str) -> Path:
return Path(v).expanduser()
@validator("CONFIG_PATH", "LOG_PATH", "DB_PATH")
def prepend_root(cls, v: str, values: dict[str, Any]) -> Path:
full_dir: Path = values["CHIA_ROOT"] / v
parent: Path = full_dir.parent
parent.mkdir(parents=True, exist_ok=True)
return full_dir
def get_settings() -> Settings:
in_pyinstaller: bool = getattr(sys, "frozen", False)
default_env_file: Path
default_config_file: Path
if in_pyinstaller:
base_path = getattr(sys, "_MEIPASS")
default_env_file = Path(base_path).joinpath(".env")
default_config_file = Path(base_path).joinpath("config.yaml")
else:
default_env_file = Path(".env")
default_config_file = Path("config.yaml")
default_settings = Settings(_env_file=default_env_file) # type: ignore[call-arg]
config_file: Path = default_settings.CONFIG_PATH
if not config_file.is_file():
config_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(default_config_file, config_file)
with open(config_file) as f:
settings_dict = yaml.safe_load(f)
settings_dict = default_settings.dict() | (settings_dict or {})
Settings(**settings_dict)
Settings._instance = Settings(**settings_dict)
return Settings._instance
settings = get_settings()