diff --git a/src/__main__.py b/src/__main__.py index ebcc709..e94c5e9 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,36 +1,25 @@ import os +import lupa import argparse +import importlib from .luagram import LuagramClient, Params, Settings, BaseLogger, enums -try: - import lupa.luajit21 as lupa +LUA_VERSION = os.getenv('LUAGRAM_LUA_VERSION', '5.1') -except ImportError: - try: - import lupa.lua54 as lupa - - except ImportError: - try: - import lupa.lua53 as lupa - - except ImportError: - import lupa +__LUA_VERSIONS = { + '5.1': 'lupa.lua51', + '5.2': 'lupa.lua52', + '5.3': 'lupa.lua53', + '5.4': 'lupa.lua54', + 'jit': 'lupa.luajit21' +} if not os.path.isdir('.app-data'): os.makedirs('.app-data') -lua = lupa.LuaRuntime(unpack_returned_tuples=True) -variables = lua.globals() -variables.enums = enums -variables.Params = Params -variables.Settings = Settings -variables.BaseLogger = BaseLogger -variables.create_new_client = LuagramClient - - def main(): parser = argparse.ArgumentParser(description='Luagram') @@ -41,13 +30,26 @@ def main(): parser.add_argument('--script', '-s', help='Path to the Lua script file', type=argparse.FileType('r'), required=True) + + parser.add_argument('--version', '-v', + help='Lua Version', default=LUA_VERSION, choices=__LUA_VERSIONS.keys()) - variables = lua.globals() + arguments = parser.parse_args() + with lupa.allow_lua_module_loading(): + LUA = importlib.import_module(__LUA_VERSIONS[arguments.version]) + lua_runtime = LUA.LuaRuntime(unpack_returned_tuples=True) + variables = lua_runtime.globals() variables.name = arguments.name - return lua.execute(arguments.script.read()) + variables.enums = enums + variables.Params = Params + variables.Settings = Settings + variables.BaseLogger = BaseLogger + variables.create_new_client = LuagramClient + + return lua_runtime.execute(arguments.script.read()) if __name__ == '__main__':