Skip to content

Commit

Permalink
Update __main__.py
Browse files Browse the repository at this point in the history
Added option to select Lua version and fixed compatibility issues with C libraries
  • Loading branch information
IRMilad authored Jul 24, 2024
1 parent 7e42eaf commit 0b413dc
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/__main__.py
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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__':
Expand Down

0 comments on commit 0b413dc

Please sign in to comment.