1 # Copyright (C) 2012 Red Hat, Inc. All rights reserved.
4 # This copyrighted material is made available to anyone wishing to use,
5 # modify, copy, or redistribute it subject to the terms and conditions
6 # of the GNU Lesser General Public License v.2.1.
8 # You should have received a copy of the GNU Lesser General Public License
9 # along with this program; if not, write to the Free Software Foundation,
10 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12 cimport libc.errno as _errno
15 from error import KmodError as _KmodError
22 cdef class Module (object):
23 "Wrap a struct kmod_module* item"
27 def __dealloc__(self):
31 if self.module is not NULL:
32 _libkmod_h.kmod_module_unref(self.module)
35 cpdef from_mod_list_item(self, _list.ModListItem item):
37 self.module = _libkmod_h.kmod_module_get_module(item.list)
40 return _util.char_ptr_to_str(
41 _libkmod_h.kmod_module_get_name(self.module))
42 name = property(fget=_name_get)
45 return _util.char_ptr_to_str(
46 _libkmod_h.kmod_module_get_path(self.module))
47 path = property(fget=_path_get)
49 def _options_get(self):
50 return _util.char_ptr_to_str(
51 _libkmod_h.kmod_module_get_options(self.module))
52 options = property(fget=_options_get)
54 def _install_commands_get(self):
55 return _util.char_ptr_to_str(
56 _libkmod_h.kmod_module_get_install_commands(self.module))
57 install_commands = property(fget=_install_commands_get)
59 def _remove_commands_get(self):
60 return _util.char_ptr_to_str(
61 _libkmod_h.kmod_module_get_remove_commands(self.module))
62 remove_commands = property(fget=_remove_commands_get)
64 def _refcnt_get(self):
65 return _libkmod_h.kmod_module_get_refcnt(self.module)
66 refcnt = property(fget=_refcnt_get)
69 return _libkmod_h.kmod_module_get_size(self.module)
70 size = property(fget=_size_get)
73 cdef _list.ModList ml = _list.ModList()
74 cdef _list.ModListItem mli
75 err = _libkmod_h.kmod_module_get_info(self.module, &ml.list)
77 raise _KmodError('Could not get versions')
81 mli = <_list.ModListItem> item
82 key = _util.char_ptr_to_str(
83 _libkmod_h.kmod_module_info_get_key(mli.list))
84 value = _util.char_ptr_to_str(
85 _libkmod_h.kmod_module_info_get_value(mli.list))
88 _libkmod_h.kmod_module_info_free_list(ml.list)
91 info = property(fget=_info_get)
93 def _versions_get(self):
94 cdef _list.ModList ml = _list.ModList()
95 cdef _list.ModListItem mli
96 err = _libkmod_h.kmod_module_get_versions(self.module, &ml.list)
98 raise _KmodError('Could not get versions')
101 mli = <_list.ModListItem> item
102 symbol = _util.char_ptr_to_str(
103 _libkmod_h.kmod_module_version_get_symbol(mli.list))
104 crc = _libkmod_h.kmod_module_version_get_crc(mli.list)
105 yield {'symbol': symbol, 'crc': crc}
107 _libkmod_h.kmod_module_versions_free_list(ml.list)
109 versions = property(fget=_versions_get)
111 def insert(self, flags=0, extra_options=None, install_callback=None,
112 data=None, print_action_callback=None):
113 cdef char *opt = NULL
114 #cdef _libkmod_h.install_callback_t install = NULL
116 _libkmod_h.kmod_module *, _libkmod_h.const_char_ptr, void *)
119 #cdef _libkmod_h.print_action_callback_t print_action = NULL
120 cdef void (*print_action)(
121 _libkmod_h.kmod_module *, _libkmod_h.bool,
122 _libkmod_h.const_char_ptr)
126 # TODO: convert callbacks and data from Python object to C types
127 err = _libkmod_h.kmod_module_probe_insert_module(
128 self.module, flags, opt, install, d, print_action)
129 if err == -_errno.EEXIST:
130 raise _KmodError('Module already loaded')
132 raise _KmodError('Could not load module')
134 def remove(self, flags=0):
135 err = _libkmod_h.kmod_module_remove_module(self.module, flags)
137 raise _KmodError('Could not remove module')