From dce0ba79a478d8021338599ea90c9a489197216c Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 18 Sep 2023 13:51:22 -0400 Subject: [PATCH 1/8] feat: make ports confirgurable --- app/config.py | 12 +++++++----- config.yaml | 4 ++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/config.py b/app/config.py index b58c6c3..858ddcd 100644 --- a/app/config.py +++ b/app/config.py @@ -54,17 +54,19 @@ class Settings(BaseSettings): @root_validator def configure_port(cls, values): + settings = get_settings() + if values["MODE"] == ExecutionMode.REGISTRY: - values["SERVER_PORT"] = ServerPort.CLIMATE_TOKEN_REGISTRY.value + values["SERVER_PORT"] = settings.CLIMATE_TOKEN_REGISTRY_PORT or ServerPort.CLIMATE_TOKEN_REGISTRY.value elif values["MODE"] == ExecutionMode.CLIENT: - values["SERVER_PORT"] = ServerPort.CLIMATE_TOKEN_CLIENT.value + values["SERVER_PORT"] = settings.CLIMATE_TOKEN_CLIENT_PORT or ServerPort.CLIMATE_TOKEN_CLIENT.value elif values["MODE"] == ExecutionMode.EXPLORER: - values["SERVER_PORT"] = ServerPort.CLIMATE_EXPLORER.value + values["SERVER_PORT"] = settings.CLIMATE_EXPLORER_PORT or ServerPort.CLIMATE_EXPLORER.value elif values["MODE"] == ExecutionMode.DEV: - values["SERVER_PORT"] = ServerPort.DEV.value + values["SERVER_PORT"] = settings.DEV_PORT or ServerPort.DEV.value else: raise ValueError(f"Invalid mode {values['MODE']}!") - + return values @validator("CHIA_ROOT", pre=True) diff --git a/config.yaml b/config.yaml index a01c79b..6e77c57 100644 --- a/config.yaml +++ b/config.yaml @@ -10,3 +10,7 @@ CHIA_HOSTNAME: localhost CHIA_FULL_NODE_RPC_PORT: 8555 CHIA_WALLET_RPC_PORT: 9256 DEFAULT_FEE: 1000000000 # mojos +CLIMATE_TOKEN_REGISTRY_PORT: 31312 +CLIMATE_EXPLORER_PORT: 31313 +CLIMATE_TOKEN_CLIENT_PORT: 31314 +DEV_PORT: 31999 From fb5d3612cbabeda3de01b6b52ac53513887f42d1 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 18 Sep 2023 14:28:16 -0400 Subject: [PATCH 2/8] feat: use settings in module --- app/config.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/config.py b/app/config.py index 858ddcd..0c00bda 100644 --- a/app/config.py +++ b/app/config.py @@ -7,6 +7,7 @@ import yaml from pydantic import BaseSettings, root_validator, validator +settings = None class ExecutionMode(enum.Enum): DEV = "dev" @@ -14,7 +15,6 @@ class ExecutionMode(enum.Enum): CLIENT = "client" EXPLORER = "explorer" - class ServerPort(enum.Enum): DEV = 31999 CLIMATE_WAREHOUSE = 31310 @@ -54,19 +54,21 @@ class Settings(BaseSettings): @root_validator def configure_port(cls, values): - settings = get_settings() + global settings + if settings is None: + return values if values["MODE"] == ExecutionMode.REGISTRY: - values["SERVER_PORT"] = settings.CLIMATE_TOKEN_REGISTRY_PORT or ServerPort.CLIMATE_TOKEN_REGISTRY.value + values["SERVER_PORT"] = getattr(settings, 'CLIMATE_TOKEN_REGISTRY_PORT', None) or ServerPort.CLIMATE_TOKEN_REGISTRY.value elif values["MODE"] == ExecutionMode.CLIENT: - values["SERVER_PORT"] = settings.CLIMATE_TOKEN_CLIENT_PORT or ServerPort.CLIMATE_TOKEN_CLIENT.value + values["SERVER_PORT"] = getattr(settings, 'CLIMATE_TOKEN_CLIENT_PORT', None) or ServerPort.CLIMATE_TOKEN_CLIENT.value elif values["MODE"] == ExecutionMode.EXPLORER: - values["SERVER_PORT"] = settings.CLIMATE_EXPLORER_PORT or ServerPort.CLIMATE_EXPLORER.value + values["SERVER_PORT"] = getattr(settings, 'CLIMATE_EXPLORER_PORT', None) or ServerPort.CLIMATE_EXPLORER.value elif values["MODE"] == ExecutionMode.DEV: - values["SERVER_PORT"] = settings.DEV_PORT or ServerPort.DEV.value + values["SERVER_PORT"] = getattr(settings, 'DEV_PORT', None) or ServerPort.DEV.value else: raise ValueError(f"Invalid mode {values['MODE']}!") - + return values @validator("CHIA_ROOT", pre=True) From 3cd26106c02922c3c2303dc0bcf1310a175734b9 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 18 Sep 2023 14:46:57 -0400 Subject: [PATCH 3/8] fix: add fields to settings class --- app/config.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/config.py b/app/config.py index 0c00bda..2604aa3 100644 --- a/app/config.py +++ b/app/config.py @@ -51,6 +51,10 @@ class Settings(BaseSettings): CHIA_HOSTNAME: str = "localhost" CHIA_FULL_NODE_RPC_PORT: int = 8555 CHIA_WALLET_RPC_PORT: int = 9256 + CLIMATE_EXPLORER_PORT: Optional[int] = None + CLIMATE_TOKEN_CLIENT_PORT: Optional[int] = None + CLIMATE_TOKEN_REGISTRY_PORT: Optional[int] = None + DEV_PORT: Optional[int] = None @root_validator def configure_port(cls, values): From 99f7b9c12ab7d5008d071fcf7d1980fa8bf12608 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 18 Sep 2023 15:03:36 -0400 Subject: [PATCH 4/8] fix: cast as it --- app/config.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/config.py b/app/config.py index 2604aa3..04aa979 100644 --- a/app/config.py +++ b/app/config.py @@ -63,13 +63,13 @@ def configure_port(cls, values): return values if values["MODE"] == ExecutionMode.REGISTRY: - values["SERVER_PORT"] = getattr(settings, 'CLIMATE_TOKEN_REGISTRY_PORT', None) or ServerPort.CLIMATE_TOKEN_REGISTRY.value + values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_REGISTRY_PORT', None) or ServerPort.CLIMATE_TOKEN_REGISTRY.value) elif values["MODE"] == ExecutionMode.CLIENT: - values["SERVER_PORT"] = getattr(settings, 'CLIMATE_TOKEN_CLIENT_PORT', None) or ServerPort.CLIMATE_TOKEN_CLIENT.value + values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_CLIENT_PORT', None) or ServerPort.CLIMATE_TOKEN_CLIENT.value) elif values["MODE"] == ExecutionMode.EXPLORER: - values["SERVER_PORT"] = getattr(settings, 'CLIMATE_EXPLORER_PORT', None) or ServerPort.CLIMATE_EXPLORER.value + values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_EXPLORER_PORT', None) or ServerPort.CLIMATE_EXPLORER.value) elif values["MODE"] == ExecutionMode.DEV: - values["SERVER_PORT"] = getattr(settings, 'DEV_PORT', None) or ServerPort.DEV.value + values["SERVER_PORT"] = int(getattr(settings, 'DEV_PORT', None) or ServerPort.DEV.value) else: raise ValueError(f"Invalid mode {values['MODE']}!") From dd4a5e6107d4e1f4c8c01045dea9b6441856da50 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 18 Sep 2023 15:40:17 -0400 Subject: [PATCH 5/8] fix: cast to none --- app/config.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/config.py b/app/config.py index 04aa979..d8ba3a0 100644 --- a/app/config.py +++ b/app/config.py @@ -60,19 +60,23 @@ class Settings(BaseSettings): def configure_port(cls, values): global settings if settings is None: + print("Settings is None.") return values + print(f"MODE: {values['MODE']}, Settings: {settings.__dict__}") + if values["MODE"] == ExecutionMode.REGISTRY: - values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_REGISTRY_PORT', None) or ServerPort.CLIMATE_TOKEN_REGISTRY.value) + values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_REGISTRY_PORT', ServerPort.CLIMATE_TOKEN_REGISTRY.value)) elif values["MODE"] == ExecutionMode.CLIENT: - values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_CLIENT_PORT', None) or ServerPort.CLIMATE_TOKEN_CLIENT.value) + values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_CLIENT_PORT', ServerPort.CLIMATE_TOKEN_CLIENT.value)) elif values["MODE"] == ExecutionMode.EXPLORER: - values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_EXPLORER_PORT', None) or ServerPort.CLIMATE_EXPLORER.value) + values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_EXPLORER_PORT', ServerPort.CLIMATE_EXPLORER.value)) elif values["MODE"] == ExecutionMode.DEV: - values["SERVER_PORT"] = int(getattr(settings, 'DEV_PORT', None) or ServerPort.DEV.value) + values["SERVER_PORT"] = int(getattr(settings, 'DEV_PORT', ServerPort.DEV.value)) else: raise ValueError(f"Invalid mode {values['MODE']}!") - + + print(f"Set SERVER_PORT to {values['SERVER_PORT']}") return values @validator("CHIA_ROOT", pre=True) From b19ca1b1411c4377a546c2e6a482c35d1da5af03 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 18 Sep 2023 16:48:13 -0400 Subject: [PATCH 6/8] fix: instance --- app/config.py | 56 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/app/config.py b/app/config.py index d8ba3a0..6aa2ebd 100644 --- a/app/config.py +++ b/app/config.py @@ -25,6 +25,7 @@ class ServerPort(enum.Enum): class Settings(BaseSettings): + _instance = None _HIDDEN_FIELDS = ["MODE", "CHIA_ROOT", "CONFIG_PATH", "SERVER_PORT"] # Hidden configs: not exposed in config.yaml @@ -56,9 +57,16 @@ class Settings(BaseSettings): CLIMATE_TOKEN_REGISTRY_PORT: Optional[int] = None DEV_PORT: Optional[int] = None + @classmethod + def get_instance(cls): + if cls._instance is None: + cls._instance = get_settings() + return cls._instance + @root_validator def configure_port(cls, values): - global settings + settings = cls.get_instance() + if settings is None: print("Settings is None.") return values @@ -92,33 +100,35 @@ def prepend_root(cls, v, values): def get_settings() -> Settings: - in_pyinstaller: bool = getattr(sys, "frozen", False) - - default_env_file: Path - default_config_file: Path - if in_pyinstaller: - default_env_file = Path(sys._MEIPASS) / ".env" - default_config_file = Path(sys._MEIPASS) / "config.yaml" - else: - default_env_file = Path(".env") - default_config_file = Path("config.yaml") + if Settings._instance is None: + in_pyinstaller: bool = getattr(sys, "frozen", False) + + default_env_file: Path + default_config_file: Path + if in_pyinstaller: + default_env_file = Path(sys._MEIPASS) / ".env" + default_config_file = Path(sys._MEIPASS) / "config.yaml" + else: + default_env_file = Path(".env") + default_config_file = Path("config.yaml") - default_settings = Settings(_env_file=default_env_file) - config_file: Path = default_settings.CONFIG_PATH + default_settings = Settings(_env_file=default_env_file) + config_file: Path = default_settings.CONFIG_PATH - settings: Settings - settings_dict: Dict - if not config_file.is_file(): - config_file.parent.mkdir(parents=True, exist_ok=True) - shutil.copy(default_config_file, config_file) + settings: Settings + settings_dict: Dict + 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, "r") as f: - settings_dict = yaml.safe_load(f) + with open(config_file, "r") as f: + settings_dict = yaml.safe_load(f) - settings_dict = default_settings.dict() | (settings_dict or {}) - settings = Settings(**settings_dict) + settings_dict = default_settings.dict() | (settings_dict or {}) + settings = Settings(**settings_dict) + Settings._instance = Settings(**settings_dict) - return settings + return Settings._instance settings = get_settings() From e6663e4bf218b906e35001aea5ea9fcb0af8950a Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Mon, 18 Sep 2023 22:02:49 -0400 Subject: [PATCH 7/8] fix: configre_ports --- app/config.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/app/config.py b/app/config.py index 6aa2ebd..ecbc701 100644 --- a/app/config.py +++ b/app/config.py @@ -65,22 +65,14 @@ def get_instance(cls): @root_validator def configure_port(cls, values): - settings = cls.get_instance() - - if settings is None: - print("Settings is None.") - return values - - print(f"MODE: {values['MODE']}, Settings: {settings.__dict__}") - if values["MODE"] == ExecutionMode.REGISTRY: - values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_REGISTRY_PORT', ServerPort.CLIMATE_TOKEN_REGISTRY.value)) + values["SERVER_PORT"] = values.get('CLIMATE_TOKEN_REGISTRY_PORT', ServerPort.CLIMATE_TOKEN_REGISTRY.value) elif values["MODE"] == ExecutionMode.CLIENT: - values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_TOKEN_CLIENT_PORT', ServerPort.CLIMATE_TOKEN_CLIENT.value)) + values["SERVER_PORT"] = values.get('CLIMATE_TOKEN_CLIENT_PORT', ServerPort.CLIMATE_TOKEN_CLIENT.value) elif values["MODE"] == ExecutionMode.EXPLORER: - values["SERVER_PORT"] = int(getattr(settings, 'CLIMATE_EXPLORER_PORT', ServerPort.CLIMATE_EXPLORER.value)) + values["SERVER_PORT"] = values.get('CLIMATE_EXPLORER_PORT', ServerPort.CLIMATE_EXPLORER.value) elif values["MODE"] == ExecutionMode.DEV: - values["SERVER_PORT"] = int(getattr(settings, 'DEV_PORT', ServerPort.DEV.value)) + values["SERVER_PORT"] = values.get('DEV_PORT', ServerPort.DEV.value) else: raise ValueError(f"Invalid mode {values['MODE']}!") From 210c3d14b7cf4a88fd395715cbfc8b8fec62ef0c Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Tue, 19 Sep 2023 10:46:50 -0400 Subject: [PATCH 8/8] chore: bump to 1.0.24 --- app/config.py | 56 +++++++++++++++++++++++--------------------------- pyproject.toml | 2 +- 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/app/config.py b/app/config.py index ecbc701..b4ecedc 100644 --- a/app/config.py +++ b/app/config.py @@ -7,8 +7,6 @@ import yaml from pydantic import BaseSettings, root_validator, validator -settings = None - class ExecutionMode(enum.Enum): DEV = "dev" REGISTRY = "registry" @@ -25,7 +23,6 @@ class ServerPort(enum.Enum): class Settings(BaseSettings): - _instance = None _HIDDEN_FIELDS = ["MODE", "CHIA_ROOT", "CONFIG_PATH", "SERVER_PORT"] # Hidden configs: not exposed in config.yaml @@ -92,33 +89,32 @@ def prepend_root(cls, v, values): def get_settings() -> Settings: - if Settings._instance is None: - in_pyinstaller: bool = getattr(sys, "frozen", False) - - default_env_file: Path - default_config_file: Path - if in_pyinstaller: - default_env_file = Path(sys._MEIPASS) / ".env" - default_config_file = Path(sys._MEIPASS) / "config.yaml" - else: - default_env_file = Path(".env") - default_config_file = Path("config.yaml") - - default_settings = Settings(_env_file=default_env_file) - config_file: Path = default_settings.CONFIG_PATH - - settings: Settings - settings_dict: Dict - 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, "r") as f: - settings_dict = yaml.safe_load(f) - - settings_dict = default_settings.dict() | (settings_dict or {}) - settings = Settings(**settings_dict) - Settings._instance = Settings(**settings_dict) + in_pyinstaller: bool = getattr(sys, "frozen", False) + + default_env_file: Path + default_config_file: Path + if in_pyinstaller: + default_env_file = Path(sys._MEIPASS) / ".env" + default_config_file = Path(sys._MEIPASS) / "config.yaml" + else: + default_env_file = Path(".env") + default_config_file = Path("config.yaml") + + default_settings = Settings(_env_file=default_env_file) + config_file: Path = default_settings.CONFIG_PATH + + settings: Settings + settings_dict: Dict + 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, "r") as f: + settings_dict = yaml.safe_load(f) + + settings_dict = default_settings.dict() | (settings_dict or {}) + settings = Settings(**settings_dict) + Settings._instance = Settings(**settings_dict) return Settings._instance diff --git a/pyproject.toml b/pyproject.toml index ade567a..4d96ce7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "Chia Climate Token Driver" -version = "1.0.23" +version = "1.0.24" description = "https://github.com/Chia-Network/climate-token-driver" authors = ["Harry Hsu ", "Chia Network Inc "]