world_sync/__init__.py
2026-09-20 16:39:12 +08:00

106 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""world_sync 模块根包(对外统一导出面)。
三层接线M11b-2
- ``world_sync.world_sync``:内层实现包 —— 单事务「事件 + 实体状态」原子写入契约
(纯标准库依赖,异常族与读写入口在此 re-export
- ``world_sync.world_sync.init``:宿主挂载层 —— world_sync CRUD、M11b-1 表注册、
M11b-2 conn_factory 登记到 ServerEnv
- 本文件:把两层能力合并成模块级 APIloader 函数名统一为 ``load_world_sync``。
铁律:本包不硬编码库名(取库名走 ``ServerEnv().get_module_dbname('world_sync')``
不在导入期建立任何数据库连接(连接工厂缺失时由写入调用点 fail-closed 抛异常)。
"""
# --- M11b-2: 单事务写入契约(最先导出,保证 import 闭包零断裂) ---
from .world_sync import (
MODULE_NAME,
MODULE_VERSION,
SERVER_ENV_CONN_FACTORY_KEYS,
ConcurrentStateConflict,
ConnFactoryNotConfigured,
EventWriteError,
PblRuntimeError,
StateWriteError,
TxAbortError,
TxConfigError,
build_sqlite_conn_factory,
detect_dialect,
new_event_id,
percentile,
read_entity_state,
resolve_conn_factory,
write_event_with_state,
)
from .world_sync import load_world_sync as _mount_runtime
# --- 既有 CRUD / 表注册能力(依赖宿主基础模块,缺失时降级不阻断契约导入) ---
try:
from .world_sync.init import (
load_world_sync as _mount_crud,
list_world_syncs,
get_world_sync,
create_world_sync,
update_world_sync,
delete_world_sync,
execute_world_sync,
module_tables,
get_table_schema,
)
except Exception as _exc: # pragma: no cover - 仅在宿主基础模块缺失时触发
_mount_crud = None
_WORLD_SYNC_INIT_ERROR = _exc
list_world_syncs = get_world_sync = create_world_sync = None
update_world_sync = delete_world_sync = execute_world_sync = None
module_tables = get_table_schema = None
else:
_WORLD_SYNC_INIT_ERROR = None
__all__ = [
# 挂载入口(三处接线统一名)
"load_world_sync",
# M11b-2 写入 / 读取契约
"write_event_with_state",
"read_entity_state",
"new_event_id",
"percentile",
"detect_dialect",
"resolve_conn_factory",
"build_sqlite_conn_factory",
# 接线常量
"MODULE_NAME",
"MODULE_VERSION",
"SERVER_ENV_CONN_FACTORY_KEYS",
# 异常族fail-closed
"PblRuntimeError",
"TxConfigError",
"ConnFactoryNotConfigured",
"EventWriteError",
"StateWriteError",
"ConcurrentStateConflict",
"TxAbortError",
# 既有 CRUD 能力(宿主基础模块可用时为真函数)
"list_world_syncs",
"get_world_sync",
"create_world_sync",
"update_world_sync",
"delete_world_sync",
"execute_world_sync",
"module_tables",
"get_table_schema",
]
def load_world_sync(server_env=None, conn_factory=None):
"""统一挂载入口:既有 CRUD/表注册 + M11b-2 运行时写入契约登记。
:param server_env: 宿主 ServerEnv 实例None 时由内层实现自行取全局单例)
:param conn_factory: 宿主连接工厂;提供则登记到 ``pbl_runtime_conn_factory``
使写入契约无需知道任何部署环境凭据来源
:return: 挂载后的 ServerEnv宿主基础模块不可用时返回传入的 env
"""
env = server_env
if _mount_crud is not None:
env = _mount_crud() or env
_mount_runtime(server_env=env, conn_factory=conn_factory)
return env