From: W. Trevor King Date: Fri, 19 Oct 2012 03:57:30 +0000 (-0400) Subject: Add additional out Module attributes (path, refcnt, ...). X-Git-Tag: v0.9~1^2~12 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5d93cfe93d30b951e6aa0371e53ffb6c6e1b8cc7;p=python-kmod.git Add additional out Module attributes (path, refcnt, ...). Signed-off-by: W. Trevor King --- diff --git a/kmod/_libkmod_h.pxd b/kmod/_libkmod_h.pxd index 975dfa3..9282cd2 100644 --- a/kmod/_libkmod_h.pxd +++ b/kmod/_libkmod_h.pxd @@ -74,4 +74,12 @@ cdef extern from 'libkmod.h': ) const_char_ptr kmod_module_get_name(const_kmod_module_ptr mod) + const_char_ptr kmod_module_get_path(const_kmod_module_ptr mod) + const_char_ptr kmod_module_get_options(const_kmod_module_ptr mod) + const_char_ptr kmod_module_get_install_commands(const_kmod_module_ptr mod) + const_char_ptr kmod_module_get_remove_commands(const_kmod_module_ptr mod) + + # Information regarding "live information" from module's state, as + # returned by kernel + int kmod_module_get_refcnt(const_kmod_module_ptr mod) long kmod_module_get_size(const_kmod_module_ptr mod) diff --git a/kmod/_util.pxd b/kmod/_util.pxd new file mode 100644 index 0000000..3251d93 --- /dev/null +++ b/kmod/_util.pxd @@ -0,0 +1,4 @@ +cimport _libkmod_h + + +cdef object char_ptr_to_str(_libkmod_h.const_char_ptr bytes) diff --git a/kmod/_util.pyx b/kmod/_util.pyx new file mode 100644 index 0000000..7c527fc --- /dev/null +++ b/kmod/_util.pyx @@ -0,0 +1,12 @@ +import sys as _sys + +cimport _libkmod_h + + +cdef object char_ptr_to_str(_libkmod_h.const_char_ptr char_ptr): + if char_ptr is NULL: + return None + if _sys.version_info >= (3,): # Python 3 + return str(char_ptr, 'ascii') + # Python 2 + return unicode(char_ptr, 'ascii') diff --git a/kmod/module.pyx b/kmod/module.pyx index 1e66772..e10c5f3 100644 --- a/kmod/module.pyx +++ b/kmod/module.pyx @@ -1,9 +1,9 @@ -import sys as _sys - cimport _libkmod_h from error import KmodError as _KmodError cimport list as _list import list as _list +cimport _util +import _util cdef class Module (object): @@ -24,14 +24,34 @@ cdef class Module (object): self.module = _libkmod_h.kmod_module_get_module(item.list) def _name_get(self): - name = _libkmod_h.kmod_module_get_name(self.module) - if _sys.version_info >= (3,): # Python 3 - name = str(name, 'ascii') - else: # Python 2 - name = unicode(name, 'ascii') - return name + return _util.char_ptr_to_str( + _libkmod_h.kmod_module_get_name(self.module)) name = property(fget=_name_get) + def _path_get(self): + return _util.char_ptr_to_str( + _libkmod_h.kmod_module_get_path(self.module)) + path = property(fget=_path_get) + + def _options_get(self): + return _util.char_ptr_to_str( + _libkmod_h.kmod_module_get_options(self.module)) + options = property(fget=_options_get) + + def _install_commands_get(self): + return _util.char_ptr_to_str( + _libkmod_h.kmod_module_get_install_commands(self.module)) + install_commands = property(fget=_install_commands_get) + + def _remove_commands_get(self): + return _util.char_ptr_to_str( + _libkmod_h.kmod_module_get_remove_commands(self.module)) + remove_commands = property(fget=_remove_commands_get) + + def _refcnt_get(self): + return _libkmod_h.kmod_module_get_refcnt(self.module) + refcnt = property(fget=_refcnt_get) + def _size_get(self): return _libkmod_h.kmod_module_get_size(self.module) size = property(fget=_size_get)