66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import importlib.util
|
|
import sys
|
|
import types
|
|
from pathlib import Path
|
|
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
SRC_ROOT = REPO_ROOT / "src"
|
|
BUILD_PACKAGE_ROOT = REPO_ROOT / "_build" / "src" / "cthulhu"
|
|
|
|
|
|
def _load_generated_module(module_name):
|
|
module_path = BUILD_PACKAGE_ROOT / f"{module_name}.py"
|
|
full_name = f"cthulhu.{module_name}"
|
|
|
|
if module_path.exists():
|
|
spec = importlib.util.spec_from_file_location(full_name, module_path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[full_name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
module = types.ModuleType(full_name)
|
|
if module_name == "cthulhu_i18n":
|
|
module._ = lambda text: text
|
|
module.ngettext = lambda singular, plural, count: singular if count == 1 else plural
|
|
module.cgettext = lambda text: text
|
|
module.C_ = lambda _context, text: text
|
|
module.localedir = str(REPO_ROOT / "po")
|
|
module.setModuleLocale = lambda *_args, **_kwargs: None
|
|
module.setLocaleForMessages = lambda *_args, **_kwargs: None
|
|
module.setLocaleForNames = lambda *_args, **_kwargs: None
|
|
module.setLocaleForGUI = lambda *_args, **_kwargs: None
|
|
return module
|
|
|
|
if module_name == "cthulhu_platform":
|
|
from cthulhu import cthulhuVersion
|
|
|
|
module.version = (
|
|
f"Cthulhu screen reader version "
|
|
f"{cthulhuVersion.version}-{cthulhuVersion.codeName}"
|
|
)
|
|
module.revision = ""
|
|
module.prefix = str(REPO_ROOT)
|
|
module.package = "cthulhu"
|
|
module.datadir = str(REPO_ROOT)
|
|
module.tablesdir = "/usr/share/liblouis/tables"
|
|
return module
|
|
|
|
raise ImportError(f"Unsupported generated module: {module_name}")
|
|
|
|
|
|
if str(SRC_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(SRC_ROOT))
|
|
|
|
import cthulhu # noqa: E402
|
|
|
|
|
|
for generated_module in ("cthulhu_i18n", "cthulhu_platform"):
|
|
if f"cthulhu.{generated_module}" in sys.modules:
|
|
continue
|
|
|
|
loaded_module = _load_generated_module(generated_module)
|
|
sys.modules[f"cthulhu.{generated_module}"] = loaded_module
|
|
setattr(cthulhu, generated_module, loaded_module)
|