Skip to main content

一个基于 lupa 模块的轻量级扩展库,提供了便捷的 Lua 模块加载与管理功能。

Reason this release was yanked:

未移除__init__.py文件中对已被删除模块的引用,导致模块被导入时抛出ImportError异常

Project description

MLua 使用指南

概述

一个基于 lupa 模块的轻量级扩展库,提供了便捷的 Lua 环境管理和模块加载功能。

快速开始

1. 创建 Lua 环境

from mlua import MLuaEnvironment

# 创建 Lua 运行时环境
lua_env = MLuaEnvironment()

2. 加载 Lua 模块

from mlua import MLuaModule, MLuaManager

# 加载单个 Lua 模块
module = MLuaModule("path/to/module.lua")

# 挂载模块到环境,指定secure=True以选择安全模式,默认选择
mlua_obj = module.mount(lua_env: MLuaEnvironment, [secure = True])

# 加载模块及其依赖
mlua_objects = module.mount_deeply(lua_env: MLuaEnvironment, [security=True])

# 注入模块到全局环境,指定globals_dict以指定全局环境,直接传入globals()以注入当前全局环境
module.inject(lua_env: MLuaEnvironment, globals_dict: dict[Any, Any], [security=True])

# 加载模块及其依赖,并注入到全局环境
module.inject_deeply(lua_env: MLuaEnvironment, globals_dict: dict[Any, Any], [security=True])

# 加载多个 Lua 模块,返回一个mlua_objects列表
manager = MLuaManager(module1: MLuaModule, module2: MLuaModule, ...)
mlua_objects = manager.mount_all(lua_env: MLuaEnvironment, [secure = True])

# 注入多个模块到全局环境
manager.inject_all(lua_env: MLuaEnvironment, globals_dict: dict[Any, Any], [security=True])

3. 使用模块功能

# 调用模块函数
results = mlua_obj.functions.some_function()

# 访问模块值
value = mlua_obj.values.some_variable

4. 处理模块依赖

from mlua import MLuaResolver

# 解析模块依赖
modules_to_load = MLuaResolver.requirements(module1: MLuaModule, module2: MLuaModule, ...)

# 打印依赖关系
MLuaResolver.relationship(module1: MLuaModule, module2: MLuaModule, ...)

5.打包

# 打包模块,返回字节串
results_bytes = MLuaPackager.pack(module1: MLuaModule, module2: MLuaModule, ...)

# 解包模块,返回模块列表
results_modules = MLuaPackager.unpack(data_bytes: bytes)

6.保存

# 保存模块至文件,默认仓库目录为./mlua_modules
MLuaManager.save(module1: MLuaModule, module2: MLuaModule, ..., [directory = "./mlua_modules"])

# 加载模块文件,返回模块列表
results_modules = MLuaManager.load([directory = "./mlua_modules"])

# 直接使用指定模块
results_modules = MLuaManager.use("module1", "module2", ..., [directory = "./mlua_modules"])

校验状态

from mlua import status

status()

注意事项

  1. 建议始终使用安全模式挂载模块
  2. 模块依赖关系应避免循环依赖
  3. 模块文件路径应为绝对路径或相对于工作目录的有效路径

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlua-1.0.3.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlua-1.0.3-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file mlua-1.0.3.tar.gz.

File metadata

  • Download URL: mlua-1.0.3.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for mlua-1.0.3.tar.gz
Algorithm Hash digest
SHA256 e9b816d864a4bdcb68db76fe4bef2288f0f509c6ef690ddafaa60c9152a598f6
MD5 d343a4369895fee753880b6155c14764
BLAKE2b-256 8ee8294068dc8866f532221b7d1284300e43e546a32fc2126a0061301fd3e1f5

See more details on using hashes here.

File details

Details for the file mlua-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: mlua-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for mlua-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 84e970a08700b67ac7e3ba9bbbc3f147032c169332a446e6d4bb9f8415792661
MD5 d710b81b5d173a8b1257f5333242d622
BLAKE2b-256 471ad42cff14ed1e5370c449747c7a71ed6d1270cf2e3b46d9444a11e3ed5184

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page