mirror of
https://github.com/upa/mscp.git
synced 2026-05-13 13:39:35 +08:00
little cleanup for python bindings
This commit is contained in:
45
mscp/mscp.py
45
mscp/mscp.py
@@ -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_INIT = 0
|
||||||
_STATE_CONNECTED = 1
|
_STATE_CONNECTED = 1
|
||||||
@@ -27,6 +54,8 @@ class mscp:
|
|||||||
"""
|
"""
|
||||||
See src/pymscp.c:wrap_mscp_init() to determine keyword arguments.
|
See src/pymscp.c:wrap_mscp_init() to determine keyword arguments.
|
||||||
"""
|
"""
|
||||||
|
self.remote = remote
|
||||||
|
self.direction = direction
|
||||||
kwargs["remote"] = remote
|
kwargs["remote"] = remote
|
||||||
kwargs["direction"] = direction
|
kwargs["direction"] = direction
|
||||||
self.m = pymscp.mscp_init(**kwargs)
|
self.m = pymscp.mscp_init(**kwargs)
|
||||||
@@ -35,6 +64,12 @@ class mscp:
|
|||||||
self.dst_path = None
|
self.dst_path = None
|
||||||
self.state = _STATE_INIT
|
self.state = _STATE_INIT
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "mscp:{}:{}".format(self.remote, self._state2str())
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<{}>".format(str(self))
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
|
||||||
if not hasattr(self, "state"):
|
if not hasattr(self, "state"):
|
||||||
@@ -46,7 +81,7 @@ class mscp:
|
|||||||
self.join()
|
self.join()
|
||||||
|
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
self.free()
|
self.release()
|
||||||
|
|
||||||
def _state2str(self):
|
def _state2str(self):
|
||||||
return _state_str[self.state]
|
return _state_str[self.state]
|
||||||
@@ -100,16 +135,16 @@ class mscp:
|
|||||||
return pymscp.mscp_get_stats(m = self.m)
|
return pymscp.mscp_get_stats(m = self.m)
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
if self.state != _STATE_JOINED:
|
if self.state == _STATE_RUNNING:
|
||||||
raise RuntimeError("invalid mscp state: {}".format(self._state2str()))
|
raise RuntimeError("invalid mscp state: {}".format(self._state2str()))
|
||||||
pymscp.mscp_cleanup(m = self.m)
|
pymscp.mscp_cleanup(m = self.m)
|
||||||
self.state = _STATE_CLEANED
|
self.state = _STATE_CLEANED
|
||||||
|
|
||||||
def free(self):
|
def release(self):
|
||||||
if self.state != _STATE_CLEANED:
|
if self.state != _STATE_CLEANED:
|
||||||
raise RuntimeError("invalid mscp state: {}".format(self._state2str()))
|
raise RuntimeError("invalid mscp state: {}".format(self._state2str()))
|
||||||
pymscp.mscp_free(m = self.m)
|
pymscp.mscp_free(m = self.m)
|
||||||
|
self.state = _STATE_RELEASED
|
||||||
|
|
||||||
# Simple interface: mscp.copy(src, dst)
|
# Simple interface: mscp.copy(src, dst)
|
||||||
def copy(self, src, dst, nonblock = False):
|
def copy(self, src, dst, nonblock = False):
|
||||||
|
|||||||
1
setup.py
1
setup.py
@@ -14,6 +14,7 @@ setup(
|
|||||||
url = "https://github.com/upa/mscp",
|
url = "https://github.com/upa/mscp",
|
||||||
packages = find_packages("mscp"),
|
packages = find_packages("mscp"),
|
||||||
package_dir = {"": "mscp"},
|
package_dir = {"": "mscp"},
|
||||||
|
data_files = [ ("", ["build/libmscp.so"])],
|
||||||
py_modules = [ "mscp" ],
|
py_modules = [ "mscp" ],
|
||||||
ext_modules = [
|
ext_modules = [
|
||||||
Extension(
|
Extension(
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <mscp.h>
|
#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
|
* pymscp.c after libmscp was built, and setup.py installs pymscp
|
||||||
* modlue and mscp python module (mscp/mscp.py), which is a warpper
|
* modlue and mscp python module (mscp/mscp.py), which is a warpper
|
||||||
* for pymscp.
|
* for pymscp.
|
||||||
|
|||||||
Reference in New Issue
Block a user