little cleanup for python bindings

This commit is contained in:
Ryo Nakamura
2023-03-12 17:39:51 +09:00
parent 2416b5f182
commit 597a7a8cba
3 changed files with 42 additions and 6 deletions

View File

@@ -1,5 +1,32 @@
import pymscp
_retry_import_pymscp = False
try:
import pymscp
except ImportError:
_retry_import_pymscp = True
if _retry_import_pymscp:
"""
libmscp.so is not installed on system library paths. So retry to
import libmscp.so installed on the mscp python module directory.
"""
import os
import ctypes
mscp_dir = os.path.dirname(__file__)
ctypes.cdll.LoadLibrary("{}/libmscp.so".format(mscp_dir))
import pymscp
# inherit static values from pymscp
LOCAL2REMOTE = pymscp.LOCAL2REMOTE
REMOTE2LOCAL = pymscp.REMOTE2LOCAL
SEVERITY_NONE = pymscp.SEVERITY_NONE
SEVERITY_ERR = pymscp.SEVERITY_ERR
SEVERITY_WARN = pymscp.SEVERITY_WARN
SEVERITY_NOTICE = pymscp.SEVERITY_NOTICE
SEVERITY_INFO = pymscp.SEVERITY_INFO
SEVERITY_DEBUG = pymscp.SEVERITY_DEBUG
_STATE_INIT = 0
_STATE_CONNECTED = 1
@@ -27,6 +54,8 @@ class mscp:
"""
See src/pymscp.c:wrap_mscp_init() to determine keyword arguments.
"""
self.remote = remote
self.direction = direction
kwargs["remote"] = remote
kwargs["direction"] = direction
self.m = pymscp.mscp_init(**kwargs)
@@ -35,6 +64,12 @@ class mscp:
self.dst_path = None
self.state = _STATE_INIT
def __str__(self):
return "mscp:{}:{}".format(self.remote, self._state2str())
def __repr__(self):
return "<{}>".format(str(self))
def __del__(self):
if not hasattr(self, "state"):
@@ -46,7 +81,7 @@ class mscp:
self.join()
self.cleanup()
self.free()
self.release()
def _state2str(self):
return _state_str[self.state]
@@ -100,16 +135,16 @@ class mscp:
return pymscp.mscp_get_stats(m = self.m)
def cleanup(self):
if self.state != _STATE_JOINED:
if self.state == _STATE_RUNNING:
raise RuntimeError("invalid mscp state: {}".format(self._state2str()))
pymscp.mscp_cleanup(m = self.m)
self.state = _STATE_CLEANED
def free(self):
def release(self):
if self.state != _STATE_CLEANED:
raise RuntimeError("invalid mscp state: {}".format(self._state2str()))
pymscp.mscp_free(m = self.m)
self.state = _STATE_RELEASED
# Simple interface: mscp.copy(src, dst)
def copy(self, src, dst, nonblock = False):

View File

@@ -14,6 +14,7 @@ setup(
url = "https://github.com/upa/mscp",
packages = find_packages("mscp"),
package_dir = {"": "mscp"},
data_files = [ ("", ["build/libmscp.so"])],
py_modules = [ "mscp" ],
ext_modules = [
Extension(

View File

@@ -6,7 +6,7 @@
#include <mscp.h>
/*
* This is a wrapper for python binding of libmscp. setup.py builds
* This is a wrapper for python binding of libmscp. setup.py builds
* pymscp.c after libmscp was built, and setup.py installs pymscp
* modlue and mscp python module (mscp/mscp.py), which is a warpper
* for pymscp.