msl.loadlib.utils module

Common functions used by the MSL-LoadLib package.

msl.loadlib.utils.is_pythonnet_installed()[source]

Checks if Python for .NET is installed.

Returns:

bool – Whether Python for .NET is installed.

Note

For help getting Python for .NET installed on a non-Windows operating system look at the prerequisites, the Mono project and the Python for .NET documentation.

msl.loadlib.utils.is_py4j_installed()[source]

Checks if Py4J is installed.

New in version 0.4.

Returns:

bool – Whether Py4J is installed.

msl.loadlib.utils.is_comtypes_installed()[source]

Checks if comtypes is installed.

New in version 0.5.

Returns:

bool – Whether comtypes is installed.

msl.loadlib.utils.check_dot_net_config(py_exe_path)[source]

Check if the useLegacyV2RuntimeActivationPolicy property is enabled.

By default, Python for .NET works with .NET 4.0+ and therefore it cannot automatically load a shared library that was compiled with .NET <4.0. This method ensures that the useLegacyV2RuntimeActivationPolicy property exists in the <python-executable>.config file and that it is enabled.

This link provides an overview explaining why the useLegacyV2RuntimeActivationPolicy property is required.

The <python-executable>.config file should look like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" />
        <supportedRuntime version="v2.0.50727" />
    </startup>
</configuration>
Parameters:

py_exe_path (str) – The path to a Python executable.

Returns:

  • int

    One of the following values:

    • -1 – if there was a problem

    • 0 – if the .NET property was already enabled, or

    • 1 – if the property was created successfully.

  • str – A message describing the outcome.

msl.loadlib.utils.is_port_in_use(port)[source]

Checks whether the TCP port is in use.

Changed in version 0.10.0: Only check TCP ports (instead of both TCP and UDP ports). Uses the ss command instead of netstat on Linux.

Changed in version 0.7.0: Renamed from port_in_use and added support for macOS.

Parameters:

port (int) – The port number to test.

Returns:

bool – Whether the TCP port is in use.

msl.loadlib.utils.get_available_port()[source]

int: Returns a port number that is available.

msl.loadlib.utils.wait_for_server(host, port, timeout)[source]

Wait for the 32-bit server to start.

Parameters:
  • host (str) – The host address of the server, e.g., '127.0.0.1'.

  • port (int) – The port number of the server.

  • timeout (float) – The maximum number of seconds to wait to establish a connection to the server.

Raises:

ConnectionTimeoutError – If a timeout occurred.

msl.loadlib.utils.get_com_info(*additional_keys)[source]

Reads the registry for the COM libraries that are available.

This function is only supported on Windows.

New in version 0.5.

Parameters:

*additional_keys (str, optional) – The Program ID (ProgID) key is returned automatically. You can include additional keys (e.g., Version, InprocHandler32, ToolboxBitmap32, VersionIndependentProgID, …) if you also want this additional information to be returned for each Class ID.

Returns:

dict – The keys are the Class ID’s and each value is a dict of the information that was requested.

Examples

>>> from msl.loadlib import utils
>>> info = utils.get_com_info()
>>> info = utils.get_com_info('Version', 'ToolboxBitmap32')
msl.loadlib.utils.generate_com_wrapper(lib, out_dir=None)[source]

Generate a Python wrapper module around a COM library.

For more information see Accessing type libraries.

New in version 0.9.

Parameters:
  • lib

    Can be any of the following

    • a LoadLibrary object

    • the ProgID or CLSID of a registered COM library as a str

    • a COM pointer instance

    • an ITypeLib COM pointer instance

    • a path to a library file (.tlb, .exe or .dll) as a str

    • a tuple or list specifying the GUID of a library, a major and a minor version number, plus optionally an LCID number, e.g., (guid, major, minor, lcid=0)

    • an object with _reg_libid_ and _reg_version_ attributes

  • out_dir (str, optional) – The output directory to save the wrapper to. If not specified then saves it to the ../site-packages/comtypes/gen directory.

Returns:

The wrapper module that was generated.