Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions qualibrate/q_runnnable.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
def file_is_calibration_instance(file: Path, klass: str) -> bool:
if not file.is_file() or file.suffix != ".py":
return False

contents = file.read_text()
try:
contents = file.read_text()
except UnicodeDecodeError:
contents = file.read_text(encoding="utf-8")
return f"{klass}(" in contents or f"{klass}[" in contents


Expand Down
25 changes: 25 additions & 0 deletions qualibrate/utils/read_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@
from pathlib import Path
from types import ModuleType

__all__ = [
"get_module_name",
"import_from_path",
"import_from_path_exec",
"import_from_path_importlib",
]


def get_module_name(file_path: Path) -> str:
"""Create module name from file path."""
return f"_node_{file_path.stem}"


def import_from_path(module_name: str, file_path: Path) -> ModuleType:
try:
return import_from_path_importlib(module_name, file_path)
except UnicodeError:
return import_from_path_exec(module_name, file_path)


def import_from_path_importlib(module_name: str, file_path: Path) -> ModuleType:
"""Import a module given its name and file path."""
spec = spec_from_file_location(module_name, file_path)
if spec is None:
Expand All @@ -18,3 +32,14 @@ def import_from_path(module_name: str, file_path: Path) -> ModuleType:
raise RuntimeError(f"Can't get loader from spec of file {file_path}")
spec.loader.exec_module(module)
return module


def import_from_path_exec(
module_name: str, file_path: Path, encoding: str = "utf-8"
) -> ModuleType:
"""Import a module from a file with enforced UTF-8 encoding."""
source = file_path.read_text(encoding=encoding)
module = ModuleType(module_name)
module.__file__ = str(file_path)
exec(compile(source, str(file_path), "exec"), module.__dict__)
return module