e10c5f38dd3006489a1bac20f7486dd002214673
[python-kmod.git] / kmod / module.pyx
1 cimport _libkmod_h
2 from error import KmodError as _KmodError
3 cimport list as _list
4 import list as _list
5 cimport _util
6 import _util
7
8
9 cdef class Module (object):
10     "Wrap a struct kmod_module* item"
11     def __cinit__(self):
12         self.module = NULL
13
14     def __dealloc__(self):
15         self._cleanup()
16
17     def _cleanup(self):
18         if self.module is not NULL:
19             _libkmod_h.kmod_module_unref(self.module)
20             self.module = NULL
21
22     cpdef from_mod_list_item(self, _list.ModListItem item):
23         self._cleanup()
24         self.module = _libkmod_h.kmod_module_get_module(item.list)
25
26     def _name_get(self):
27         return _util.char_ptr_to_str(
28             _libkmod_h.kmod_module_get_name(self.module))
29     name = property(fget=_name_get)
30
31     def _path_get(self):
32         return _util.char_ptr_to_str(
33             _libkmod_h.kmod_module_get_path(self.module))
34     path = property(fget=_path_get)
35
36     def _options_get(self):
37         return _util.char_ptr_to_str(
38             _libkmod_h.kmod_module_get_options(self.module))
39     options = property(fget=_options_get)
40
41     def _install_commands_get(self):
42         return _util.char_ptr_to_str(
43             _libkmod_h.kmod_module_get_install_commands(self.module))
44     install_commands = property(fget=_install_commands_get)
45
46     def _remove_commands_get(self):
47         return _util.char_ptr_to_str(
48             _libkmod_h.kmod_module_get_remove_commands(self.module))
49     remove_commands = property(fget=_remove_commands_get)
50
51     def _refcnt_get(self):
52         return _libkmod_h.kmod_module_get_refcnt(self.module)
53     refcnt = property(fget=_refcnt_get)
54
55     def _size_get(self):
56         return _libkmod_h.kmod_module_get_size(self.module)
57     size = property(fget=_size_get)
58
59     def insert(self, flags=0, extra_options=None, install_callback=None,
60                data=None, print_action_callback=None):
61         cdef char *opt = NULL
62         #cdef _libkmod_h.install_callback_t install = NULL
63         cdef int (*install)(
64             _libkmod_h.kmod_module *, _libkmod_h.const_char_ptr, void *)
65         install = NULL
66         cdef void *d = NULL
67         #cdef _libkmod_h.print_action_callback_t print_action = NULL
68         cdef void (*print_action)(
69             _libkmod_h.kmod_module *, _libkmod_h.bool,
70             _libkmod_h.const_char_ptr)
71         print_action = NULL
72         if extra_options:
73             opt = extra_options
74         # TODO: convert callbacks and data from Python object to C types
75         err = _libkmod_h.kmod_module_probe_insert_module(
76             self.module, flags, opt, install, d, print_action)
77         if err == -_libkmod_h.EEXIST:
78             raise _KmodError('Module already loaded')
79         elif err < 0:
80             raise _KmodError('Could not load module')
81
82     def remove(self, flags=0):
83         err = _libkmod_h.kmod_module_remove_module(self.module, flags)
84         if err < 0:
85             raise _KmodError('Could not remove module')