Changeset 1085


Ignore:
Timestamp:
02/18/21 22:51:55 (4 years ago)
Author:
sz
Message:
  • more sophisticated frams.py init() so frams-test.py (and other python programs) don't have to bother with framsticks paths
  • './'+name for loading .so from the current directory in linux
Location:
framspy
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • framspy/frams-test.py

    r1083 r1085  
    22import frams
    33
    4 frams_path = sys.argv[1] # set to the directory where Framsticks binaries and libraries exist
    5 frams.init(frams_path, sys.argv[2] if len(sys.argv) > 2 else None, "-Ddata")  # "-D"+os.path.join(framspath,"data")) # not possible (maybe python windows issue) because of the need for os.chdir(). So we assume "data" is where the dll/so is
     4frams.init(*(sys.argv[1:])) #pass whatever args we have, init() is the right place to deal with different scenrios:
     5# frams.init() - should try to figure out everything (and might fail)
     6# frams.init('path/to/frams') - load the library from the specified dir and configure framsticks path as "data" inside the dir
     7# frams.init('path/to/frams','-d/tmp/workdir/data') - as above, but set the working (writable) directory somewhere else
    68
    79print('Available objects:', dir(frams))
  • framspy/frams.py

    r1083 r1085  
    248248
    249249
    250 def init(lib_path, lib_name, *args):
     250def init(*args):
    251251        """
    252252        Initializes the connection to Framsticks dll/so.
    253253
    254         :param lib_path: the path where the dll/so file can be found
    255         :param lib_name: the name of the dll/so (a complete name including file name extension) or None if you want to use the default name
    256         :param args: optional arguments to pass to Framsticks initialization (try "-h")
     254        Python programs do not have to know the framstics path but if they know, just pass the path as the first argument.
     255        Similarly '-dPATH' and '-DPATH' needed by framsticks are optional and derived from the first path, unless they are specified as args in init().
     256        '-lNAME' is the optional library name (full name including the file name extension), default is 'frams-objects.dll/.so' depending on the platform.
     257        All other arguments are passed to framsticks and not interpreted by this function.
     258
    257259        """
     260        # goals:
     261        frams_d=None
     262        frams_D=None
     263        lib_path=None
     264        lib_name='frams-objects.so' if os.name == 'posix' else 'frams-objects.dll'
     265        initargs=[]
     266        for a in args:
     267                if a[:2]=='-d':
     268                        frams_d=a
     269                elif a[:2]=='-D':
     270                        frams_D=a
     271                elif a[:2]=='-l':
     272                        lib_name=a[2:]
     273                elif lib_path==None:
     274                        lib_path=a
     275                else:
     276                        initargs.append(a)
     277        if lib_path==None:
     278                #todo: use env variable and/or the zip distribution we are in when the path is not specified in arg
     279                #for now just assume the current dir is framsticks
     280                lib_path='.'
     281
    258282        original_dir = os.getcwd()
    259         os.chdir(lib_path)  # because under Windows, frams-objects.dll requires other dll's which reside in the same directory, so we must change current dir for them to be found while loading the main dll
    260         global c_api  # access global variable to initialize it
    261         c_api = ctypes.CDLL(lib_name if lib_name is not None else 'frams-objects.so' if os.name == 'posix' else 'frams-objects.dll')
     283        os.chdir(lib_path)  # because under Windows, frams.dll requires other dll's which reside in the same directory, so we must change current dir for them to be found while loading the main dll
     284        abs_data=os.path.abspath('data') #use absolute path for -d and -D so python is free to cd anywhere without confusing the frams
     285        # for the hypothetical case without lib_path the abs_data must be obtained from somewhere else
     286        if frams_d==None:
     287                frams_d='-d'+abs_data
     288        if frams_D==None:
     289                frams_D='-D'+abs_data
     290        initargs.insert(0,frams_d)
     291        initargs.insert(0,frams_D)
     292        initargs.insert(0,'dummy.exe')
     293       
     294        global c_api  # access global variable
     295        if lib_path!=None and os.name == 'posix':
     296                lib_name = './'+lib_name #currently we always have lib_path (even if it is incorrect) but hypothetically it could work with lib_path==None and load .so from some default system path without './'
     297        c_api = ctypes.CDLL(lib_name)
    262298        os.chdir(original_dir)
    263 
     299       
    264300        c_api.init.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)]
    265301        c_api.init.restype = None
     
    301337        c_api.rootObject.restype = ctypes.c_void_p
    302338
    303         c_args = (ctypes.c_char_p * (len(args) + 1))(*([b''] + list(a.encode(ExtValue._encoding) for a in args)))
    304         c_api.init(len(args) + 1, c_args)
     339        c_args = (ctypes.c_char_p * len(initargs))(*list(a.encode(ExtValue._encoding) for a in initargs))
     340        c_api.init(len(initargs), c_args)
    305341        Root = ExtValue._rootObject()
    306342        for n in dir(Root):
Note: See TracChangeset for help on using the changeset viewer.