-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
from typing import Type | ||
|
||
ENV_FILE_ENCODING = "utf-8" | ||
|
||
|
||
class BaseENVSettings(BaseSettings): | ||
""" | ||
This class acts as the BaseClass which can be used to define custom | ||
ENV-Variables which can be used across the ComputeBlock & for entrypoints | ||
This definition, and pydantic, will then take care of validating the envs | ||
""" | ||
|
||
model_config = SettingsConfigDict( | ||
env_file_encoding=ENV_FILE_ENCODING, | ||
case_sensitive=True, | ||
extra="ignore" | ||
) | ||
|
||
@classmethod | ||
def load_settings( | ||
cls: Type["BaseENVSettings"], | ||
env_file: str = ".env" | ||
) -> "BaseENVSettings": | ||
""" | ||
load_settings loads the env file. The name of the env_file can be | ||
passed as an argument. | ||
Returns the parsed ENVs | ||
""" | ||
return cls(_env_file=env_file, _env_file_encoding=ENV_FILE_ENCODING) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
import os | ||
from scystream.sdk.core import entrypoint | ||
from scystream.sdk.env.settings import BaseENVSettings | ||
|
||
|
||
class WithDefaultSettings(BaseENVSettings): | ||
DUMMY_SETTING: str = "this is a dummy setting" | ||
|
||
|
||
class NoDefaultSetting(BaseENVSettings): | ||
DUMMY_SETTING: str | ||
|
||
|
||
class TestSettings(unittest.TestCase): | ||
def test_entrypoint_with_setting_default(self): | ||
@entrypoint(WithDefaultSettings) | ||
def with_default_settings(settings): | ||
return settings.DUMMY_SETTING | ||
|
||
result = with_default_settings() | ||
self.assertEqual(result, "this is a dummy setting") | ||
|
||
""" | ||
environment is set | ||
""" | ||
os.environ["DUMMY_SETTING"] = "overridden setting" | ||
result = with_default_settings() | ||
self.assertEqual(result, "overridden setting") | ||
del os.environ["DUMMY_SETTING"] | ||
|
||
def test_entrypoint_with_no_setting_default(self): | ||
@entrypoint(NoDefaultSetting) | ||
def with_no_default_settings(settings): | ||
return settings.DUMMY_SETTING | ||
|
||
with self.assertRaises(ValueError): | ||
with_no_default_settings() | ||
|
||
""" | ||
environemnt is set | ||
""" | ||
os.environ["DUMMY_SETTING"] = "required setting" | ||
result = with_no_default_settings() | ||
self.assertEqual(result, "required setting") | ||
del os.environ["DUMMY_SETTING"] | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |