Auto-update: Thu Aug 8 21:28:32 PDT 2024
This commit is contained in:
parent
69a657910d
commit
c9c36b4c42
1 changed files with 19 additions and 10 deletions
|
@ -232,26 +232,30 @@ class DirConfig(BaseModel):
|
||||||
@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)
|
# Create a temporary instance to resolve placeholders
|
||||||
resolved_data = instance.resolve_placeholders(config_data)
|
temp_instance = cls.create_dynamic_model(**config_data)
|
||||||
|
resolved_data = temp_instance.resolve_placeholders(config_data)
|
||||||
|
|
||||||
|
# Create the final instance with resolved 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
|
||||||
|
@ -282,19 +286,24 @@ class DirConfig(BaseModel):
|
||||||
def resolve_string_placeholders(self, value: str) -> Path:
|
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:
|
||||||
if match == 'HOME':
|
if match == 'HOME':
|
||||||
replacement = str(self.HOME)
|
replacement = str(self.HOME)
|
||||||
|
elif match == 'BASE':
|
||||||
|
replacement = str(Path(__file__).parent.parent)
|
||||||
|
elif match == 'DATA':
|
||||||
|
replacement = str(Path(__file__).parent.parent / "data")
|
||||||
elif hasattr(self, match):
|
elif hasattr(self, match):
|
||||||
replacement = str(getattr(self, match))
|
replacement = str(getattr(self, match))
|
||||||
else:
|
else:
|
||||||
replacement = value
|
replacement = value
|
||||||
|
|
||||||
value = value.replace('{{' + match + '}}', replacement)
|
value = value.replace('{{' + match + '}}', replacement)
|
||||||
|
|
||||||
return Path(value).expanduser()
|
return Path(value).expanduser()
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_dynamic_model(cls, **data):
|
def create_dynamic_model(cls, **data):
|
||||||
DynamicModel = create_model(
|
DynamicModel = create_model(
|
||||||
|
|
Loading…
Reference in a new issue