33from traceback import print_exc
44from .topic import Topic
55from .socketwrapper import SocketWrapper
6+ from functools import partial
67from typing import Callable , NamedTuple
78from collections import namedtuple
89
@@ -337,20 +338,19 @@ async def executeLuaFunction(self, function: str, args, getReturnValue = True):
337338 else :
338339 topic .cancel ()
339340
340- async def library (self ) -> dict :
341+ async def library (self , wrapper : None | Callable = None ) -> dict :
341342 """ Get an object representing the OpenSpace lua libarary. \n
343+ :param wrapper: if set, wraps all API calls (may be used to make them synchronous)
342344 :return - The lua library, mapped to async python functions. """
343345
344- def generateAsyncSingleRetFunction (functionName ):
345- async def fun (* args ):
346- try :
347- luaTable = await self .executeLuaFunction (functionName , args )
348- if luaTable :
349- return luaTable ['1' ]
350- return None
351- except Exception as e :
352- print ("Lua exception error: \n " , e )
353- return fun
346+ async def async_lua_call (functionName , * args ):
347+ try :
348+ luaTable = await self .executeLuaFunction (functionName , args )
349+ if luaTable :
350+ return luaTable ['1' ]
351+ return None
352+ except Exception as e :
353+ print ("Lua exception error: \n " , e )
354354
355355 docs = await self .getDocumentation ('lua' )
356356
@@ -369,13 +369,18 @@ async def fun(*args):
369369 _lib = '' if subPyLibrary == pyLibrary else libraryName + '.'
370370 fullFunctionName = 'openspace.' + _lib + func ['name' ]
371371
372- subPyLibrary [func ['name' ]] = generateAsyncSingleRetFunction (fullFunctionName )
372+ lua_call = partial (async_lua_call , fullFunctionName )
373+ if wrapper is not None :
374+ lua_call = partial (wrapper , lua_call )
375+ subPyLibrary [func ['name' ]] = lua_call
373376
374377 return toNamedTuple (pyLibrary , libraryName )
375378
376- async def singleReturnLibrary (self ):
379+ async def singleReturnLibrary (self , wrapper = None ):
377380 """ Get an object representing the OpenSpace lua library. \n
381+ (deprecated. Use library() instead)
382+ :param wrapper: if set, wraps all API calls (may be used to make them synchronous)
378383 :return - The lua library, mapped to async python functions. This method only
379384 returns the first return value. """
380385
381- return await self .library (False )
386+ return await self .library (wrapper )
0 commit comments