|
| 1 | +""" |
| 2 | +This module defines a class for interacting with a compiled shared library using |
| 3 | +the ctypes library in Python. It manages loading the library, defining some |
| 4 | +expected function prototypes, and parsing exit codes returned by the library functions. |
| 5 | +
|
| 6 | +The class `PropLibCDLL` is a thin wrapper for `ctypes.CDLL` which automatically |
| 7 | +determines the appropriate shared library file based on the operating system and |
| 8 | +provides methods for checking function return codes. |
| 9 | +
|
| 10 | +Classes: |
| 11 | +-------- |
| 12 | +- PropLibCDLL: A subclass of `ctypes.CDLL` that manages loading a PropLib shared |
| 13 | + library and provides error checking for its functions. |
| 14 | +
|
| 15 | +Methods: |
| 16 | +-------- |
| 17 | +- __init__(name): |
| 18 | + Initializes the `PropLibCDLL` instance by loading the specified library and |
| 19 | + setting up the expected function prototypes. |
| 20 | +
|
| 21 | +- get_lib_name(lib_name: str) -> str: |
| 22 | + Static method that constructs the full filename of the library based on the |
| 23 | + current platform. |
| 24 | +
|
| 25 | +- err_check(rtn_code: int) -> None: |
| 26 | + Checks the return code from the library's function call and raises a RuntimeError |
| 27 | + with the associated error message if the return code indicates an error. |
| 28 | +
|
| 29 | +Usage: |
| 30 | +------ |
| 31 | +1. Create an instance of `PropLibCDLL` with the name of the shared library (without |
| 32 | + extension). |
| 33 | +2. Call functions from the library using the instance. |
| 34 | +3. Use `err_check` to handle error codes returned by those functions. |
| 35 | +
|
| 36 | +Example: |
| 37 | +-------- |
| 38 | +```python |
| 39 | +lib = PropLibCDLL("SomePropLibLibrary-1.0") |
| 40 | +return_code = lib.SomeLibraryFunction() |
| 41 | +lib.err_check(return_code) |
| 42 | +``` |
| 43 | +""" |
| 44 | +import platform |
| 45 | +from ctypes import * |
| 46 | +from pathlib import Path |
| 47 | + |
| 48 | +class PropLibCDLL(CDLL): |
| 49 | + def __init__(self, name): |
| 50 | + full_name = self.get_lib_name(name) |
| 51 | + super().__init__(full_name) |
| 52 | + # Define expected function prototypes |
| 53 | + self.GetReturnStatusCharArray.restype = POINTER(c_char_p) |
| 54 | + self.GetReturnStatusCharArray.argtypes = (c_int,) |
| 55 | + self.FreeReturnStatusCharArray.restype = None |
| 56 | + self.FreeReturnStatusCharArray.argtypes = (POINTER(c_char_p),) |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def get_lib_name(lib_name: str) -> str: |
| 60 | + """Get the full filename of the library specified by `lib_name`. |
| 61 | +
|
| 62 | + This function appends the correct file extension based on the current platform, |
| 63 | + and prepends the full absolute file path. The shared library is expected |
| 64 | + to exist in the same directory as this file. |
| 65 | + |
| 66 | + :param lib_name: The library name, with no extension or path, e.g., "P2108-1.0" |
| 67 | + :raises NotImplementedError: For platforms other than Windows, Linux, or macOS. |
| 68 | + :return: The full filename, including path and extension, of the library. |
| 69 | + """ |
| 70 | + # Load the compiled library |
| 71 | + if platform.uname()[0] == "Windows": |
| 72 | + lib_name += ".dll" |
| 73 | + elif platform.uname()[0] == "Linux": |
| 74 | + lib_name += ".so" |
| 75 | + elif platform.uname()[0] == "Darwin": |
| 76 | + lib_name += ".dylib" |
| 77 | + else: |
| 78 | + raise NotImplementedError("Your OS is not yet supported") |
| 79 | + # Library should be in the same directory as this file |
| 80 | + lib_path = Path(__file__).parent / lib_name |
| 81 | + return str(lib_path.resolve()) |
| 82 | + |
| 83 | + |
| 84 | + def err_check(self, rtn_code: int) -> None: |
| 85 | + """Parse the library's return code and raise an error if one occurred. |
| 86 | +
|
| 87 | + Returns immediately for `rtn_code == 0`, otherwise retrieves the |
| 88 | + status message string from the underlying library and raises a |
| 89 | + RuntimeError with the status message. |
| 90 | +
|
| 91 | + :param rtn_code: Integer return code from the underlying library. |
| 92 | + :raises RuntimeError: For any non-zero inputs. |
| 93 | + :return: None |
| 94 | + """ |
| 95 | + if rtn_code == 0: |
| 96 | + return |
| 97 | + else: |
| 98 | + msg = self.GetReturnStatusCharArray(c_int(rtn_code)) |
| 99 | + msg_str = cast(msg, c_char_p).value.decode("utf-8") |
| 100 | + self.FreeReturnStatusCharArray(msg) |
| 101 | + raise RuntimeError(msg_str) |
0 commit comments