__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Data Modules - 数据链模块包。
  5. 注意:
  6. - 这里采用延迟导入(lazy import),避免在执行 `python -m data_modules.xxx` 时,
  7. 因包级 __init__ 提前导入子模块而触发 runpy 的 RuntimeWarning。
  8. - 推荐用法永远安全:
  9. from data_modules.index_manager import IndexManager
  10. 但为了兼容历史代码,也保留:
  11. from data_modules import IndexManager
  12. """
  13. from __future__ import annotations
  14. from importlib import import_module
  15. from typing import Any
  16. __all__ = [
  17. # Config
  18. "DataModulesConfig",
  19. "get_config",
  20. "set_project_root",
  21. # API Client
  22. "ModalAPIClient",
  23. "get_client",
  24. # Entity Linker
  25. "EntityLinker",
  26. "DisambiguationResult",
  27. # State Manager
  28. "StateManager",
  29. "EntityState",
  30. "Relationship",
  31. "StateChange",
  32. # Index Manager
  33. "IndexManager",
  34. "ChapterMeta",
  35. "SceneMeta",
  36. "ReviewMetrics",
  37. "RelationshipEventMeta",
  38. # RAG Adapter
  39. "RAGAdapter",
  40. "SearchResult",
  41. "ContextManager",
  42. "ContextRanker",
  43. "SnapshotManager",
  44. "QueryRouter",
  45. # Style Sampler
  46. "StyleSampler",
  47. "StyleSample",
  48. "SceneType",
  49. ]
  50. _LAZY_EXPORTS: dict[str, tuple[str, str]] = {
  51. # Config
  52. "DataModulesConfig": (".config", "DataModulesConfig"),
  53. "get_config": (".config", "get_config"),
  54. "set_project_root": (".config", "set_project_root"),
  55. # API Client
  56. "ModalAPIClient": (".api_client", "ModalAPIClient"),
  57. "get_client": (".api_client", "get_client"),
  58. # Entity Linker
  59. "EntityLinker": (".entity_linker", "EntityLinker"),
  60. "DisambiguationResult": (".entity_linker", "DisambiguationResult"),
  61. # State Manager
  62. "StateManager": (".state_manager", "StateManager"),
  63. "EntityState": (".state_manager", "EntityState"),
  64. "Relationship": (".state_manager", "Relationship"),
  65. "StateChange": (".state_manager", "StateChange"),
  66. # Index Manager
  67. "IndexManager": (".index_manager", "IndexManager"),
  68. "ChapterMeta": (".index_manager", "ChapterMeta"),
  69. "SceneMeta": (".index_manager", "SceneMeta"),
  70. "ReviewMetrics": (".index_manager", "ReviewMetrics"),
  71. "RelationshipEventMeta": (".index_manager", "RelationshipEventMeta"),
  72. # RAG Adapter
  73. "RAGAdapter": (".rag_adapter", "RAGAdapter"),
  74. "SearchResult": (".rag_adapter", "SearchResult"),
  75. "ContextManager": (".context_manager", "ContextManager"),
  76. "ContextRanker": (".context_ranker", "ContextRanker"),
  77. "SnapshotManager": (".snapshot_manager", "SnapshotManager"),
  78. "QueryRouter": (".query_router", "QueryRouter"),
  79. # Style Sampler
  80. "StyleSampler": (".style_sampler", "StyleSampler"),
  81. "StyleSample": (".style_sampler", "StyleSample"),
  82. "SceneType": (".style_sampler", "SceneType"),
  83. }
  84. def __getattr__(name: str) -> Any: # pragma: no cover
  85. if name not in _LAZY_EXPORTS:
  86. raise AttributeError(name)
  87. module_path, attr = _LAZY_EXPORTS[name]
  88. module = import_module(module_path, __name__)
  89. value = getattr(module, attr)
  90. globals()[name] = value # cache
  91. return value
  92. def __dir__() -> list[str]: # pragma: no cover
  93. return sorted(set(list(globals().keys()) + list(_LAZY_EXPORTS.keys())))