6.17. foundations.library

library.py

Platform:
Windows, Linux, Mac Os X.
Description:
Provides objects for C / C++ libraries binding.

Others:

6.17.1. Module Attributes

foundations.library.LOGGER

6.17.2. Classes

class foundations.library.LibraryHook(**kwargs)[source]

Bases: foundations.dataStructures.Structure

Defines a library hook used by the Library class to bind target library functions.

Initializes the class.

Usage:

LibraryHook(name="FreeImage_GetVersion", argumentsTypes=None, returnValue=ctypes.c_char_p)
Parameters:
  • name (unicode) – Name of the target library function to bind.
  • argumentsTypes – Required function arguments type (Refer to Python ctypes - 15.17.1.7 module for more informations). ( List )
  • returnValue – Function return type (Refer to Python ctypes - 15.17.1.8 module for more informations). ( Object )
class foundations.library.Library(path, functions=None, bindLibrary=True)[source]

Bases: object

Defines methods to bind a C / C++ Library.
The class is a singleton and will bind only one time a given library. Each unique library instance is stored in Library.instances attribute and get returned if the library is requested again through a new instantiation.

Initializes the class.

Usage:

>>> import ctypes 
>>> path = "FreeImage.dll"
>>> functions = (LibraryHook(name="FreeImage_GetVersion", argumentsTypes=None, returnValue=ctypes.c_char_p),)
>>> library = Library(path, functions)
>>> library.FreeImage_GetVersion()
'3.15.1'
Parameters:
  • path (unicode) – Library path.
  • functions (tuple) – Binding functions list.
  • bindLibrary (bool) – Library will be binded on initialization.
callback
Parameters:callback (ctypes.CFUNCTYPE) – callback.

alias of CFunctionType

instances[source]

Property for self.__instances attribute.

Returns:self.__instances.
Return type:WeakValueDictionary
initialized[source]

Property for self.__initialized attribute.

Returns:self.__initialized.
Return type:unicode
path[source]

Property for self.__path attribute.

Returns:self.__path.
Return type:unicode
functions[source]

Property for self.__functions attribute.

Returns:self.__functions.
Return type:tuple
library[source]

Property for self.__library attribute.

Returns:self.__library.
Return type:object
bindFunction(function)[source]

Binds given function to a class object attribute.

Usage:

>>> import ctypes 
>>> path = "FreeImage.dll"
>>> function = LibraryHook(name="FreeImage_GetVersion", argumentsTypes=None, returnValue=ctypes.c_char_p)
>>> library = Library(path, bindLibrary=False)
>>> library.bindFunction(function)
True
>>> library.FreeImage_GetVersion()
'3.15.1'
Parameters:function (LibraryHook) – Function to bind.
Returns:Method success.
Return type:bool
bindLibrary()[source]

Binds the Library using functions registered in the self.__functions attribute.

Usage:

>>> import ctypes 
>>> path = "FreeImage.dll"
>>> functions = (LibraryHook(name="FreeImage_GetVersion", argumentsTypes=None, returnValue=ctypes.c_char_p),)
>>> library = Library(path, functions, bindLibrary=False)
>>> library.bindLibrary()
True
>>> library.FreeImage_GetVersion()
'3.15.1'
Returns:Method success.
Return type:bool