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,23 +83,21 @@ 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
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, yaml_path: Union[str, Path], secrets_path: Optional[Union[str, Path]] = None, dir_config: Optional['Configuration'] = None) -> 'Configuration':
|
def load(cls, yaml_path: Union[str, Path], secrets_path: Optional[Union[str, Path]] = None, dir_config: Optional['Configuration'] = None) -> 'Configuration':
|
||||||
yaml_path = cls._resolve_path(yaml_path, 'config')
|
yaml_path = cls._resolve_path(yaml_path, 'config')
|
||||||
if secrets_path:
|
if secrets_path:
|
||||||
secrets_path = cls._resolve_path(secrets_path, 'config')
|
secrets_path = cls._resolve_path(secrets_path, 'config')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with yaml_path.open('r') as file:
|
with yaml_path.open('r') as file:
|
||||||
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,43 +106,39 @@ 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
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
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"
|
||||||
elif not path.is_absolute():
|
elif not path.is_absolute():
|
||||||
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,17 +147,16 @@ 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)
|
||||||
|
|
||||||
for match in matches:
|
for match in matches:
|
||||||
parts = match.split('.')
|
parts = match.split('.')
|
||||||
if len(parts) == 1: # Internal reference
|
if len(parts) == 1: # Internal reference
|
||||||
|
@ -172,21 +165,16 @@ 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
|
||||||
|
|
||||||
value = value.replace('{{' + match + '}}', str(replacement))
|
value = value.replace('{{' + match + '}}', str(replacement))
|
||||||
|
|
||||||
# Convert to Path if it looks like a file path
|
# Convert to Path if it looks like a file path
|
||||||
if isinstance(value, str) and (value.startswith(('/', '~')) or (':' in value and value[1] == ':')):
|
if isinstance(value, str) and (value.startswith(('/', '~')) or (':' in value and value[1] == ':')):
|
||||||
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():
|
||||||
|
@ -194,47 +182,45 @@ class Configuration(BaseModel):
|
||||||
data[key] = cls.create_dynamic_model(**value)
|
data[key] = cls.create_dynamic_model(**value)
|
||||||
elif isinstance(value, list) and all(isinstance(item, dict) for item in value):
|
elif isinstance(value, list) and all(isinstance(item, dict) for item in value):
|
||||||
data[key] = [cls.create_dynamic_model(**item) for item in value]
|
data[key] = [cls.create_dynamic_model(**item) for item in value]
|
||||||
|
|
||||||
DynamicModel = create_model(
|
DynamicModel = create_model(
|
||||||
f'Dynamic{cls.__name__}',
|
f'Dynamic{cls.__name__}',
|
||||||
__base__=cls,
|
__base__=cls,
|
||||||
**{k: (Any, v) for k, v in data.items()}
|
**{k: (Any, v) for k, v in data.items()}
|
||||||
)
|
)
|
||||||
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()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, yaml_path: Union[str, Path]) -> 'DirConfig':
|
def load(cls, yaml_path: Union[str, Path]) -> 'DirConfig':
|
||||||
yaml_path = cls._resolve_path(yaml_path, 'config')
|
yaml_path = cls._resolve_path(yaml_path, 'config')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with yaml_path.open('r') as file:
|
with yaml_path.open('r') as file:
|
||||||
config_data = yaml.safe_load(file)
|
config_data = yaml.safe_load(file)
|
||||||
|
|
||||||
print(f"Loaded configuration data from {yaml_path}")
|
print(f"Loaded configuration data from {yaml_path}")
|
||||||
|
|
||||||
# Ensure HOME is set
|
# Ensure HOME is set
|
||||||
if 'HOME' not in config_data:
|
if 'HOME' not in config_data:
|
||||||
config_data['HOME'] = str(Path.home())
|
config_data['HOME'] = str(Path.home())
|
||||||
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:
|
||||||
print(f"Error loading configuration: {str(e)}")
|
print(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
|
||||||
|
@ -244,10 +230,10 @@ class DirConfig(BaseModel):
|
||||||
elif not path.is_absolute():
|
elif not path.is_absolute():
|
||||||
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,39 +242,28 @@ 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))
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
|
value = value.replace('{{' + match + '}}', replacement)
|
||||||
|
|
||||||
|
return Path(value).expanduser()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_dynamic_model(cls, **data):
|
def create_dynamic_model(cls, **data):
|
||||||
DynamicModel = create_model(
|
DynamicModel = create_model(
|
||||||
|
@ -297,7 +272,7 @@ class DirConfig(BaseModel):
|
||||||
**{k: (Path, v) for k, v in data.items()}
|
**{k: (Path, v) for k, v in data.items()}
|
||||||
)
|
)
|
||||||
return DynamicModel(**data)
|
return DynamicModel(**data)
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
arbitrary_types_allowed = True
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue