Auto-update: Thu Aug 8 18:21:55 PDT 2024
This commit is contained in:
parent
57b54218ba
commit
ba7aa79229
1 changed files with 22 additions and 31 deletions
|
@ -228,7 +228,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 +245,9 @@ class DirConfig(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) for k, v in data.items()}
|
resolved_data = {k: self.resolve_placeholders(v, secrets_data) 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,9 +256,9 @@ 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) for v in data]
|
return [self.resolve_placeholders(v, secrets_data) for v in data]
|
||||||
elif isinstance(data, str):
|
elif isinstance(data, str):
|
||||||
return self.resolve_string_placeholders(data)
|
return self.resolve_string_placeholders(data, secrets_data, self.HOME)
|
||||||
else:
|
else:
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@ -274,22 +274,12 @@ class DirConfig(BaseModel):
|
||||||
replacement = str(home_dir / parts[1].lower())
|
replacement = str(home_dir / 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':
|
|
||||||
secret_key = parts[1].strip() # Remove any leading/trailing whitespace
|
|
||||||
replacement = secrets_data.get(secret_key)
|
|
||||||
if replacement is None:
|
|
||||||
warn(f"Secret '{secret_key}' not found in secrets file")
|
|
||||||
replacement = ''
|
|
||||||
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() if value.startswith(('/', '~')) else value
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_dynamic_model(cls, **data):
|
def create_dynamic_model(cls, **data):
|
||||||
|
@ -304,6 +294,7 @@ class DirConfig(BaseModel):
|
||||||
arbitrary_types_allowed = True
|
arbitrary_types_allowed = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Configuration class for API & Database methods.
|
# Configuration class for API & Database methods.
|
||||||
class APIConfig(BaseModel):
|
class APIConfig(BaseModel):
|
||||||
HOST: str
|
HOST: str
|
||||||
|
|
Loading…
Reference in a new issue