Auto-update: Thu Aug 8 18:27:32 PDT 2024
This commit is contained in:
parent
1b4b242ac8
commit
15af9e8a6b
1 changed files with 48 additions and 73 deletions
|
@ -83,7 +83,6 @@ TS_ID = os.environ.get('TS_ID')
|
||||||
T = TypeVar('T', bound='Configuration')
|
T = TypeVar('T', bound='Configuration')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Configuration(BaseModel):
|
class Configuration(BaseModel):
|
||||||
HOME: Path = Path.home()
|
HOME: Path = Path.home()
|
||||||
_dir_config: Optional['Configuration'] = None
|
_dir_config: Optional['Configuration'] = None
|
||||||
|
@ -99,7 +98,6 @@ class Configuration(BaseModel):
|
||||||
config_data = yaml.safe_load(file)
|
config_data = yaml.safe_load(file)
|
||||||
|
|
||||||
debug(f"Loaded configuration data from {yaml_path}")
|
debug(f"Loaded configuration data from {yaml_path}")
|
||||||
secrets_data = {}
|
|
||||||
if secrets_path:
|
if secrets_path:
|
||||||
with secrets_path.open('r') as file:
|
with secrets_path.open('r') as file:
|
||||||
secrets_data = yaml.safe_load(file)
|
secrets_data = yaml.safe_load(file)
|
||||||
|
@ -108,20 +106,18 @@ class Configuration(BaseModel):
|
||||||
for item in config_data:
|
for item in config_data:
|
||||||
if isinstance(item, dict):
|
if isinstance(item, dict):
|
||||||
item.update(secrets_data)
|
item.update(secrets_data)
|
||||||
elif isinstance(config_data, dict):
|
else:
|
||||||
config_data.update(secrets_data)
|
config_data.update(secrets_data)
|
||||||
|
|
||||||
if isinstance(config_data, list):
|
if isinstance(config_data, list):
|
||||||
config_data = {"configurations": config_data}
|
config_data = {"configurations": config_data}
|
||||||
if not isinstance(config_data, dict) or config_data.get('HOME') is None:
|
if config_data.get('HOME') is None:
|
||||||
config_data = config_data if isinstance(config_data, dict) else {}
|
|
||||||
config_data['HOME'] = str(Path.home())
|
config_data['HOME'] = str(Path.home())
|
||||||
debug(f"HOME was not in config, set to default: {config_data['HOME']}")
|
warn(f"HOME was None in config, set to default: {config_data['HOME']}")
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
instance = cls.create_dynamic_model(**config_data)
|
instance = cls.create_dynamic_model(**config_data)
|
||||||
instance._dir_config = dir_config or instance
|
instance._dir_config = dir_config or instance
|
||||||
resolved_data = instance.resolve_placeholders(config_data, secrets_data)
|
resolved_data = instance.resolve_placeholders(config_data)
|
||||||
instance = cls.create_dynamic_model(**resolved_data)
|
instance = cls.create_dynamic_model(**resolved_data)
|
||||||
instance._dir_config = dir_config or instance
|
instance._dir_config = dir_config or instance
|
||||||
return instance
|
return instance
|
||||||
|
@ -130,10 +126,9 @@ class Configuration(BaseModel):
|
||||||
err(f"Error loading configuration: {str(e)}")
|
err(f"Error loading configuration: {str(e)}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _resolve_path(cls, path: Union[str, Path], default_dir: str) -> Path:
|
def _resolve_path(cls, path: Union[str, Path], default_dir: str) -> Path:
|
||||||
base_path = Path(__file__).parent.parent
|
base_path = Path(__file__).parent.parent # This will be two levels up from this file
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
if not path.suffix:
|
if not path.suffix:
|
||||||
path = base_path / 'sijapi' / default_dir / f"{path.name}.yaml"
|
path = base_path / 'sijapi' / default_dir / f"{path.name}.yaml"
|
||||||
|
@ -141,10 +136,9 @@ class Configuration(BaseModel):
|
||||||
path = base_path / path
|
path = base_path / path
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
def resolve_placeholders(self, data: Any) -> Any:
|
||||||
def resolve_placeholders(self, data: Any, secrets_data: Dict[str, Any]) -> Any:
|
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
resolved_data = {k: self.resolve_placeholders(v, secrets_data) for k, v in data.items()}
|
resolved_data = {k: self.resolve_placeholders(v) for k, v in data.items()}
|
||||||
home_dir = Path(resolved_data.get('HOME', self.HOME)).expanduser()
|
home_dir = Path(resolved_data.get('HOME', self.HOME)).expanduser()
|
||||||
base_dir = Path(__file__).parent.parent
|
base_dir = Path(__file__).parent.parent
|
||||||
data_dir = base_dir / "data"
|
data_dir = base_dir / "data"
|
||||||
|
@ -153,13 +147,12 @@ class Configuration(BaseModel):
|
||||||
resolved_data['DATA'] = str(data_dir)
|
resolved_data['DATA'] = str(data_dir)
|
||||||
return resolved_data
|
return resolved_data
|
||||||
elif isinstance(data, list):
|
elif isinstance(data, list):
|
||||||
return [self.resolve_placeholders(v, secrets_data) for v in data]
|
return [self.resolve_placeholders(v) for v in data]
|
||||||
elif isinstance(data, str):
|
elif isinstance(data, str):
|
||||||
return self.resolve_string_placeholders(data, secrets_data, Path(self.HOME))
|
return self.resolve_string_placeholders(data)
|
||||||
else:
|
else:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def resolve_string_placeholders(self, value: str) -> Any:
|
def resolve_string_placeholders(self, value: str) -> Any:
|
||||||
pattern = r'\{\{\s*([^}]+)\s*\}\}'
|
pattern = r'\{\{\s*([^}]+)\s*\}\}'
|
||||||
matches = re.findall(pattern, value)
|
matches = re.findall(pattern, value)
|
||||||
|
@ -172,10 +165,6 @@ class Configuration(BaseModel):
|
||||||
replacement = getattr(self, parts[1], str(Path.home() / parts[1].lower()))
|
replacement = getattr(self, parts[1], str(Path.home() / parts[1].lower()))
|
||||||
elif len(parts) == 2 and parts[0] == 'ENV':
|
elif len(parts) == 2 and parts[0] == 'ENV':
|
||||||
replacement = os.getenv(parts[1], '')
|
replacement = os.getenv(parts[1], '')
|
||||||
elif len(parts) == 2 and parts[0] == 'SECRET':
|
|
||||||
replacement = getattr(self, parts[1].strip(), '')
|
|
||||||
if not replacement:
|
|
||||||
warn(f"Secret '{parts[1].strip()}' not found in secrets file")
|
|
||||||
else:
|
else:
|
||||||
replacement = value
|
replacement = value
|
||||||
|
|
||||||
|
@ -186,7 +175,6 @@ class Configuration(BaseModel):
|
||||||
return Path(value).expanduser()
|
return Path(value).expanduser()
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_dynamic_model(cls, **data):
|
def create_dynamic_model(cls, **data):
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
|
@ -202,13 +190,11 @@ class Configuration(BaseModel):
|
||||||
)
|
)
|
||||||
return DynamicModel(**data)
|
return DynamicModel(**data)
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
extra = "allow"
|
extra = "allow"
|
||||||
arbitrary_types_allowed = True
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class DirConfig(BaseModel):
|
class DirConfig(BaseModel):
|
||||||
HOME: Path = Path.home()
|
HOME: Path = Path.home()
|
||||||
|
|
||||||
|
@ -228,7 +214,7 @@ class DirConfig(BaseModel):
|
||||||
print(f"HOME was not in config, set to default: {config_data['HOME']}")
|
print(f"HOME was not in config, set to default: {config_data['HOME']}")
|
||||||
|
|
||||||
instance = cls.create_dynamic_model(**config_data)
|
instance = cls.create_dynamic_model(**config_data)
|
||||||
resolved_data = instance.resolve_placeholders(config_data, {})
|
resolved_data = instance.resolve_placeholders(config_data)
|
||||||
return cls.create_dynamic_model(**resolved_data)
|
return cls.create_dynamic_model(**resolved_data)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -245,9 +231,9 @@ class DirConfig(BaseModel):
|
||||||
path = base_path / path
|
path = base_path / path
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def resolve_placeholders(self, data: Any, secrets_data: Dict[str, Any]) -> Any:
|
def resolve_placeholders(self, data: Any) -> Any:
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
resolved_data = {k: self.resolve_placeholders(v, secrets_data) for k, v in data.items()}
|
resolved_data = {k: self.resolve_placeholders(v) for k, v in data.items()}
|
||||||
home_dir = Path(resolved_data.get('HOME', self.HOME)).expanduser()
|
home_dir = Path(resolved_data.get('HOME', self.HOME)).expanduser()
|
||||||
base_dir = Path(__file__).parent.parent
|
base_dir = Path(__file__).parent.parent
|
||||||
data_dir = base_dir / "data"
|
data_dir = base_dir / "data"
|
||||||
|
@ -256,38 +242,27 @@ class DirConfig(BaseModel):
|
||||||
resolved_data['DATA'] = str(data_dir)
|
resolved_data['DATA'] = str(data_dir)
|
||||||
return resolved_data
|
return resolved_data
|
||||||
elif isinstance(data, list):
|
elif isinstance(data, list):
|
||||||
return [self.resolve_placeholders(v, secrets_data) for v in data]
|
return [self.resolve_placeholders(v) for v in data]
|
||||||
elif isinstance(data, str):
|
elif isinstance(data, str):
|
||||||
return self.resolve_string_placeholders(data, secrets_data)
|
return self.resolve_string_placeholders(data)
|
||||||
else:
|
else:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def resolve_string_placeholders(self, value: str, secrets_data: Dict[str, Any]) -> Any:
|
def resolve_string_placeholders(self, value: str) -> Path:
|
||||||
pattern = r'\{\{\s*([^}]+)\s*\}\}'
|
pattern = r'\{\{\s*([^}]+)\s*\}\}'
|
||||||
matches = re.findall(pattern, value)
|
matches = re.findall(pattern, value)
|
||||||
|
|
||||||
for match in matches:
|
for match in matches:
|
||||||
parts = match.split('.')
|
if match == 'HOME':
|
||||||
if len(parts) == 1: # Internal reference
|
replacement = str(self.HOME)
|
||||||
replacement = getattr(self, parts[0], str(Path(self.HOME) / parts[0].lower()))
|
elif hasattr(self, match):
|
||||||
elif len(parts) == 2 and parts[0] == 'Dir':
|
replacement = str(getattr(self, match))
|
||||||
replacement = getattr(self, parts[1], str(Path(self.HOME) / parts[1].lower()))
|
|
||||||
elif len(parts) == 2 and parts[0] == 'ENV':
|
|
||||||
replacement = os.getenv(parts[1], '')
|
|
||||||
elif len(parts) == 2 and parts[0] == 'SECRET':
|
|
||||||
replacement = secrets_data.get(parts[1].strip(), '')
|
|
||||||
if not replacement:
|
|
||||||
warn(f"Secret '{parts[1].strip()}' not found in secrets file")
|
|
||||||
else:
|
else:
|
||||||
replacement = value
|
replacement = value
|
||||||
|
|
||||||
value = value.replace('{{' + match + '}}', str(replacement))
|
value = value.replace('{{' + match + '}}', replacement)
|
||||||
|
|
||||||
# Convert to Path if it looks like a file path
|
|
||||||
if isinstance(value, str) and (value.startswith(('/', '~')) or (':' in value and value[1] == ':')):
|
|
||||||
return Path(value).expanduser()
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
return Path(value).expanduser()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_dynamic_model(cls, **data):
|
def create_dynamic_model(cls, **data):
|
||||||
|
|
Loading…
Reference in a new issue