mirror of https://github.com/eyhc1/rendercv.git
data_models: handle SyntaxError and ImportError for custom themes
This commit is contained in:
parent
e554b0b687
commit
b61eb5f897
|
@ -1014,7 +1014,7 @@ class RenderCVDataModel(RenderCVBaseModel):
|
|||
|
||||
if path_to_init_file.exists():
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"", # this is somehow not required
|
||||
"theme",
|
||||
path_to_init_file,
|
||||
)
|
||||
if spec is None:
|
||||
|
@ -1024,7 +1024,13 @@ class RenderCVDataModel(RenderCVBaseModel):
|
|||
)
|
||||
|
||||
theme_module = importlib.util.module_from_spec(spec)
|
||||
try:
|
||||
spec.loader.exec_module(theme_module) # type: ignore
|
||||
except SyntaxError or ImportError:
|
||||
raise ValueError(
|
||||
f"The custom theme {theme_name}'s __init__.py file is not"
|
||||
" valid. Please check the file and try again.",
|
||||
)
|
||||
|
||||
ThemeDataModel = getattr(
|
||||
theme_module, f"{theme_name.title()}ThemeOptions" # type: ignore
|
||||
|
|
|
@ -514,3 +514,28 @@ def test_custom_theme_without_init_file(tmp_path, testdata_directory_path):
|
|||
)
|
||||
|
||||
assert data_model.design.theme == "dummytheme"
|
||||
|
||||
|
||||
def test_custom_theme_with_broken_init_file(tmp_path, testdata_directory_path):
|
||||
reference_custom_theme_path = (
|
||||
testdata_directory_path
|
||||
/ "test_copy_theme_files_to_output_directory_custom_theme"
|
||||
/ "dummytheme"
|
||||
)
|
||||
|
||||
# copy the directory to tmp_path:
|
||||
custom_theme_path = tmp_path / "dummytheme"
|
||||
shutil.copytree(reference_custom_theme_path, custom_theme_path, dirs_exist_ok=True)
|
||||
|
||||
# remove the __init__.py file:
|
||||
init_file = custom_theme_path / "__init__.py"
|
||||
init_file.write_text("invalid python code", encoding="utf-8")
|
||||
|
||||
os.chdir(tmp_path)
|
||||
with pytest.raises(pydantic.ValidationError):
|
||||
dm.RenderCVDataModel(
|
||||
**{ # type: ignore
|
||||
"cv": {"name": "John Doe"},
|
||||
"design": {"theme": "dummytheme"},
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue