def test_multiple_runtimes(self): lua1 = lupa.LuaRuntime() function1 = lua1.eval('function() return 1 end') self.assertNotEqual(None, function1) self.assertEqual(1, function1()) lua2 = lupa.LuaRuntime() function2 = lua2.eval('function() return 1+1 end') self.assertNotEqual(None, function2) self.assertEqual(1, function1()) self.assertEqual(2, function2()) lua3 = lupa.LuaRuntime() self.assertEqual(1, function1()) self.assertEqual(2, function2()) function3 = lua3.eval('function() return 1+1+1 end') self.assertNotEqual(None, function3) del lua1, lua2, lua3 self.assertEqual(1, function1()) self.assertEqual(2, function2()) self.assertEqual(3, function3())
def create_instance(self, sandbox=True): logger.debug("Creating new lua instance") lua = lupa.LuaRuntime(unpack_returned_tuples=True) # pylint: disable=E1101 if sandbox: self.sandbox_instance(lua) return lua
def test_threading_pycallback(self): func_code = '''\ function calc(pyfunc, i) if i > 2 then return pyfunc(i) + calc(pyfunc, i-1) + calc(pyfunc, i-2) + 1 else return 1 end end return calc ''' runtimes = [lupa.LuaRuntime() for _ in range(10)] functions = [lua.execute(func_code) for lua in runtimes] results = [None] * len(runtimes) def pycallback(i): return i**2 def test(i, func, *args): results[i] = func(*args) threads = [ threading.Thread(target=test, args=(i, luafunc, pycallback, 20)) for i, luafunc in enumerate(functions) ] self._run_threads(threads) self.assertEqual(1, len(set(results))) self.assertEqual(185925, results[0])
def beacons_from_terrain(dcs_path: Path, path: Path) -> Iterable[Beacon]: # TODO: Fix case-sensitive issues. # The beacons.lua file differs by case in some terrains. Will need to be # fixed if the tool is to be run on Linux, but presumably the server # wouldn't be able to find these anyway. beacons_lua = path / "beacons.lua" with cd(dcs_path): lua = lupa.LuaRuntime() lua.execute(textwrap.dedent("""\ function module(name) end """)) bind_gettext = lua.eval(textwrap.dedent("""\ function(py_gettext) package.preload["i_18n"] = function() return { translate = py_gettext } end end """)) translator = gettext.translation( "messages", path / "l10n", languages=["en"]) def translate(message_name: str) -> str: if not message_name: return message_name return translator.gettext(message_name) bind_gettext(translate) src = beacons_lua.read_text() lua.execute(src) beacon_types_map: Dict[int, BeaconType] = {} for beacon_type in BeaconType: beacon_value = lua.eval(beacon_type.name) beacon_types_map[beacon_value] = beacon_type beacons = lua.eval("beacons") for beacon in beacons.values(): beacon_type_lua = beacon["type"] if beacon_type_lua not in beacon_types_map: raise KeyError( f"Unknown beacon type {beacon_type_lua}. Check that all " f"beacon types in {beacon_types_path} are present in " f"{BeaconType.__class__.__name__}" ) beacon_type = beacon_types_map[beacon_type_lua] yield Beacon( beacon["display_name"], beacon["callsign"], beacon_type, convert_lua_frequency(beacon["frequency"]), getattr(beacon, "channel", None) )
def get_new_runtime(**kwargs): """ Return a pre-configured LuaRuntime. """ kwargs.setdefault('register_eval', False) kwargs.setdefault('unpack_returned_tuples', True) lua = lupa.LuaRuntime(**kwargs) lua.execute("assert(os.setlocale('C'))") return lua
def cloud_presets(dcs_path: Path) -> Iterable[CloudPreset]: clouds_lua = dcs_path / "Config/Effects/clouds.lua" logging.info(f"Loading cloud presets from {clouds_lua}") with cd(dcs_path): lua = lupa.LuaRuntime() # Define the translation function to a no-op because we don't need it. lua.execute( textwrap.dedent("""\ function _(key) return key end """)) lua.execute(clouds_lua.read_text()) for name, data in lua.eval("clouds['presets']").items(): if not data["visibleInGUI"]: # Not something choosable in the ME, so skip it. continue yield CloudPreset( name, ui_name=data["readableNameShort"], description=data["readableName"], min_base=data["presetAltMin"], max_base=data["presetAltMax"], )
def test_sequential_threading(self): func_code = '''\ function calc(i) if i > 2 then return calc(i-1) + calc(i-2) + 1 else return 1 end end return calc ''' lua = lupa.LuaRuntime() functions = [lua.execute(func_code) for _ in range(10)] results = [None] * len(functions) starter = threading.Event() def test(i, func, *args): starter.wait() results[i] = func(*args) threads = [ threading.Thread(target=test, args=(i, func, 25)) for i, func in enumerate(functions) ] self._run_threads(threads, starter) self.assertEqual(1, len(set(results))) self.assertEqual(150049, results[0])
def test_min_descriptor() -> None: luart = lupa.LuaRuntime(unpack_returned_tuples=True) g = luart.globals() for k, v in _descr_d.items(): g[k] = v descriptor = lua.descriptor(luart) assert descriptor.name == _descr_d["extensionName"]
def __init__(self, clone: bool = False) -> None: self.lua = lupa.LuaRuntime(attribute_handlers=(self._getter, setattr)) globs = self.lua.globals() assert __file__.endswith('.py') self.dofile = globs.dofile # type: Callable[[str], Any] self.exec(self._code) self.loadstring = globs.loadstring # type: Callable[[Union[str, bytes]], Callable]
def json_results(blueprint): fd = StringIO.StringIO(base64.b64decode(blueprint)) with gzip.GzipFile(fileobj=fd) as gzfd: string = gzfd.read() subs = { 'basic-accumulator': 'accumulator', 'basic-armor': 'light-armor', 'basic-beacon': 'beacon', 'basic-bullet-magazine': 'firearm-magazine', 'basic-exoskeleton-equipment': 'exoskeleton-equipment', 'basic-grenade': 'grenade', 'basic-inserter': 'inserter', 'basic-laser-defense-equipment': "personal-laser-defense-equipment", 'basic-mining-drill': "electric-mining-drill", 'basic-modular-armor': "modular-armor", 'basic-splitter': "splitter", 'basic-transport-belt': "transport-belt", 'basic-transport-belt-to-ground': "underground-belt", 'express-transport-belt-to-ground': "express-underground-belt", 'fast-transport-belt-to-ground': "fast-underground-belt", 'piercing-bullet-magazine': "piercing-rounds-magazine", 'smart-chest': "steel-chest", 'smart-inserter': "filter-inserter" } string = re.sub( '(\\w|-)+', lambda m: subs[m.group(0)] if m.group(0) in subs else m.group(0), string) lua_table = lupa.LuaRuntime().execute(string) return make_table(lua_table)
def __init__(self, path, mod_path, mod_list): self.lua = lupa.LuaRuntime(unpack_returned_tuples=True) self.lua.globals().log = lambda s: None # Load the base modules add_package_path(self.lua, os.path.join(path, 'data', 'core', 'lualib')) self.lua.require('dataloader') # Get the list of mods to use. Note that the base data is itself a # module. self.mods = get_mod_list(path, mod_path, mod_list) load_order = get_load_order(self.mods) # Load a defines Lua object. self.lua.require('defines') # We need the core data for a few things, even though it's not listed as # a module itself. self.mods['core'] = os.path.join(path, 'data', 'core') self._load_mod_file('core', 'data') # Load modules. Note that the base data is also a module. for f in ('data', 'data-updates', 'data-final-fixes'): for mod in load_order: self._load_mod_file(mod, f) # Base Lua data. self._data = self.lua.globals().data.raw
def __init__(self): self._rt = lupa.LuaRuntime(unpack_returned_tuples=True, encoding='latin-1') if self.eval("type(jit) == 'table'"): raise RuntimeError("lupa must be linked against Lua, not LuaJIT.\n" "Please install lupa with `--no-luajit`.") self._setup_runtime()
def __init__(self, mod_manager, mod_settings): self.package = None self.current_path = None self.mod_manager = mod_manager self.mod_settings = mod_settings self.lua = lupa.LuaRuntime() self.lua.execute('function math.pow(x,y) return x^y end') serpent = self.lua.require('serpent') self.lua.globals().serpent = serpent self.lua.execute('function table_size(t)\n' ' local count = 0\n' ' for k,v in pairs(t) do\n' ' count = count + 1\n' ' end\n' ' return count\n' 'end') self.lua.execute('function log(s)\n' ' print(s)\n' 'end') self.lua.execute('package={loaded={}}') defines = self.lua.require('defines') self.lua.globals().defines = defines closure = self.lua.eval('function (obj) return function (f) return obj:require(f) end end') self.lua.globals().require = closure(self) self.push_mods() self.push_mod_settings() with self.mod_manager.mods['core'].get_file('lualib/dataloader.lua') as f: file = f.read() self.lua.eval('function(s) return load(s)() end')(file) self.load_mods()
def generate_emails(email): source = db.session.query(EmailSource).filter(EmailSource.id == email.source).one() badges = db.session.query(Badge).filter(Badge.event == email.event).all() L = lupa.LuaRuntime(register_eval=False) filter = L.execute(email.code) subject_template = jinja2.Template(email.subject) body_template = jinja2.Template(email.body) tables = { "HotelRoomNight": db.session.query(HotelRoomNight).filter(HotelRoomNight.event == email.event).all(), "HotelRoomRequest": db.session.query(HotelRoomRequest).join(Badge, Badge.id == HotelRoomRequest.badge).filter(Badge.event == email.event).all(), "RoomNightApproval": db.session.query(RoomNightApproval).join(RoomNightRequest, RoomNightRequest.id == RoomNightApproval.room_night).filter(RoomNightApproval.approved == True).all(), "Department": {x.id: x for x in db.session.query(Department).filter(Department.event == email.event).all()}, "HotelRoom": {x.id: x for x in db.session.query(HotelRoom).all()}, "Badge": {x.id: x for x in db.session.query(Badge).filter(Badge.event_id == email.event).all()}, "RoomNightAssignment": db.session.query(RoomNightAssignment).all(), } for badge in badges: context = get_email_context(badge, tables) if filter(context): subject = subject_template.render(**context) body = body_template.render(**context) yield [badge.id, badge.email, source.address, subject, body]
def test_lupa(): import lupa print(lupa.version) file = '/Users/joli/proj/sdk_uzone/trunk/projects/luandou/runtime/src/csb/Activity/acLimintTimeHero.lua' text = FS.read_text(file) lua = lupa.LuaRuntime() ast = lua.compile(text) print(ast)
def execute_module(module_name: str): lua = lupa.LuaRuntime() with open('Module2/mw.lua') as mw_file, \ open(module_name) as language_file: lua.execute(mw_file.read()) program = language_file.read() luatable = lua.execute(program) return luatable_to_dict(luatable)
def from_lua(input): """ Use Lupa as a parser by actually running the code :param input: :return: """ lua = lupa.LuaRuntime() lua.execute(input) return lua.globals()
def _encoding_test(self, encoding, expected_length): lua = lupa.LuaRuntime(encoding) self.assertEqual(unicode_type, type(lua.eval(self.test_string))) self.assertEqual(self.test_string[1:-1], lua.eval(self.test_string)) self.assertEqual(expected_length, lua.eval('string.len(%s)' % self.test_string))
def init(): configureLogging(True) anki.hooks.addHook("formatQA_0", formatQA) global lua lua = lupa.LuaRuntime() lua.execute(u"""BEGIN_CARD = {}""") lua.execute(u"""package.path = '""" + getLibraryDir() + u"""/?.lua'""") for library in getLibraries(): basename = re.sub(ur"\.lua$", ur"", library) lua.require(basename)
def __init__(self, name, minimum_role, arguments, code, group): self.lua = lupa.LuaRuntime( unpack_returned_tuples=True, attribute_filter=self.filter_attribute_access) self.code = code self.name = name self.group = group self.minimum_role = minimum_role self.arguments = arguments self.parser = self.make_parser(self.arguments) self.log = []
def __init__(self, value=None, base=BASE, **kwargs): self.base = base # must be set before initializer self.Lua = lupa.LuaRuntime() self.Lua.execute(base) NumberExpression.__init__(self, grammar, value=value, display="%s", **kwargs)
def __init__(self, resource): ''' Lua-based interpreter for handler files. ''' self.resource = resource self.runtime = lupa.LuaRuntime() self.api = api.API(self.document) # Provide the deje module bootstrapfunc = self.runtime.execute(bootstrap) bootstrapfunc(self.api.export()) self.runtime.execute('load_deje = nil') self.reload()
def test_threading_iter(self): values = list(range(1, 100)) lua = lupa.LuaRuntime() table = lua.eval('{%s}' % ','.join(map(str, values))) self.assertEqual(values, list(table)) lua_iter = iter(table) state_lock = threading.Lock() running = [] iterations_done = {} def sync(i): state_lock.acquire() try: status = iterations_done[i] except KeyError: status = iterations_done[i] = [0, threading.Event()] status[0] += 1 state_lock.release() event = status[1] while status[0] < len(running): event.wait(0.1) event.set() l = [] start_event = threading.Event() def extract(n, append=l.append): running.append(n) if len(running) < len(threads): start_event.wait() else: start_event.set() # all running, let's go for i, item in enumerate(lua_iter): append(item) sync(i) running.remove(n) threads = [ threading.Thread(target=extract, args=(i, )) for i in range(6) ] self._run_threads(threads) orig = l[:] l.sort() self.assertEqual(values, l)
def load_lua_data(self, path): with open(path, encoding="utf-8") as fin: raw_data_text = fin.read() escaped_data_text = re.sub(r"_\((\".+?\")\)", r"\1", raw_data_text) ext_package_paths_str = ";".join(self.ext_package_paths).replace( "\\", "\\\\") print(ext_package_paths_str) lua = lupa.LuaRuntime(unpack_returned_tuples=True) lua.execute("\n".join([ 'package.path = package.path .. ";' + ext_package_paths_str + '"', escaped_data_text ])) return lua.globals().data()
def setup_runtime(self): """ Creates a new lua runtime and initializes all globals. Used by start_script(), should not be called directly. """ # Create new lua instance self.runtime = lupa.LuaRuntime(unpack_returned_tuples=True) # Reset keys and button states self.ui._keys = {} self.ui._buttons = {} # Reset rising/falling edge dict self._rising_dict = {} self._falling_dict = {} # Set up API g = self.runtime.globals() g["Sound"] = Sound g["Expression"] = Expression g["Robot"] = Robot g["Hardware"] = LuaHardware(self.runtime) g["Animate"] = LuaAnimate g["AnimatePeriodic"] = LuaAnimatePeriodic g["Twitter"] = Twitter g["UI"] = self.ui g["print"] = callback(self.on_print) g["sleep"] = self._sleep g["rising_edge"] = self._rising_edge g["falling_edge"] = self._falling_edge g["seconds"] = time.time # Basic Arduino functions g["delay"] = lambda t: self._sleep(t / 1000) g["min"] = min g["max"] = max g["abs"] = abs g["constrain"] = lambda x, a, b: max(a, min(x, b)) g["map"] = lambda x, in_min, in_max, out_min, out_max: (x - in_min) * ( out_max - out_min) / (in_max - in_min) + out_min g["millis"] = lambda: time.time() * 1000.0
def ping_outstation(outstation, outstn_name): L = lupa.LuaRuntime() lua_script = open(outstn_name + ".lua").read() L_func = L.execute(lua_script) g = L.globals() #print(g.generate_data) lua_pymod_dtype_map = { 'Discretes Input': 'd', 'Coils': 'c', 'Input Registers': 'i', 'Holding Registers': 'h' } while not outstation.outstation_stop.is_set(): time.sleep(1) unit_id = outstation.node.name_id_map[outstn_name] try: slave = outstation.context[unit_id] except ParameterException as err: outstation.logger.debug("context.py returned:%s" % err) continue data_count = outstation.node.datastore[unit_id] data = g.generate_data() if not data: continue for d_type in lua_pymod_dtype_map.keys(): if d_type not in data or not data[d_type]: continue pymodbus_d_type = lua_pymod_dtype_map[d_type] values = list(data[d_type].values()) if len(values) == 0 or len(values) != data_count: continue dtype_datablock = slave.store.get(pymodbus_d_type) outstation.logger.debug("Updating Outstation:" + outstn_name + ", datacount:" + str(data_count) + " dtype:" + d_type + ", new values : " + str(values)) dtype_datablock.setValues(start_address, values) outstation.logger.info("stopping server %s" % outstn_name) outstation.server.shutdown() outstation.server.server_close() if hasattr(outstation.node, 'vnic'): node.deallocate(outstation.node.vnic)
def test_attribute_filter(self): def attr_filter(obj, name, setting): if isinstance(name, unicode_type): if not name.startswith('_'): return name + '1' raise AttributeError('denied') lua = lupa.LuaRuntime(attribute_filter=attr_filter) function = lua.eval('function(obj) return obj.__name__ end') class X(object): a = 0 a1 = 1 _a = 2 __a = 3 x = X() function = self.lua.eval('function(obj) return obj.a end') self.assertEquals(function(x), 0) function = lua.eval('function(obj) return obj.a end') self.assertEquals(function(x), 1) function = self.lua.eval('function(obj) return obj.__class__ end') self.assertEquals(function(x), X) function = lua.eval('function(obj) return obj.__class__ end') self.assertRaises(AttributeError, function, x) function = self.lua.eval('function(obj) return obj._a end') self.assertEquals(function(x), 2) function = lua.eval('function(obj) return obj._a end') self.assertRaises(AttributeError, function, x) function = self.lua.eval('function(obj) return obj._X__a end') self.assertEquals(function(x), 3) function = lua.eval('function(obj) return obj._X__a end') self.assertRaises(AttributeError, function, x) function = self.lua.eval('function(obj) return obj.a end') self.assertEquals(function(x), 0) function = lua.eval('function(obj) return obj.a end') self.assertEquals(function(x), 1)
def __init__(self, channel, bot, settings=None, data=None, logger=None, chat=None): self.channel = channel self.bot = bot if chat: self.chat = chat else: self.chat = Chat(self.bot, self.channel) self.settings = settings self.logger = logger self.commands = {} self.timers = [] self.datasource = DataSource(channel, bot, data) self.commands_last_executed = {} self.lua = lupa.LuaRuntime(unpack_returned_tuples=False) self._inject_globals()
def _create_lua_runtime(self): # noinspection PyArgumentList lua = lupa.LuaRuntime( register_eval=False, register_builtins=False, unpack_returned_tuples=True, ) # Ensure the local is consistent and ignore system Lua paths lua.execute("assert(os.setlocale('C'))") lua.globals().package.path = ';'.join([ os.path.join(os.path.dirname(__file__), "?.lua"), "/opt/hunter2/share/lua/5.2/?.lua", ]) # TODO: Support cross platform libraries lua.globals().package.cpath = ';'.join([ "/opt/hunter2/lib/lua/5.2/?.so", ]) return lua
def __init__(self, table: SkillTable.SkillTable): self.table = table self.lua = lupa.LuaRuntime(register_eval=True, unpack_returned_tuples=True) self.funcalccaption = self.lua.eval( "function (self,captionname,clsid,level) " + "return CalcCaption(self,captionname,clsid,level);" + "end") self.funcalcoh = self.lua.eval("function (self,clsid,level) " + "return CalcOH(self,clsid,level);" + "end") self.funcalcvalue = self.lua.eval( "function (self,prop,clsid,level) " + "return CalcValue(self,prop,clsid,level);" "end") for fp in glob.glob("./data/shared.ipf/script/*.lua", recursive=True): with open(fp, "r", encoding="utf-8-sig", errors='ignore') as f: s = f.read() print(fp) self.lua.execute(s) with open("./sfrbase.lua", "r", encoding="utf-8") as f: s = f.read() self.lua.execute(s)