Add Module.versions attribute.
[python-kmod.git] / kmod / module.pyx
1 # Copyright (C) 2012 Red Hat, Inc. All rights reserved.
2 #               2012 W. Trevor King
3 #
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.
7 #
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
11
12 cimport _libkmod_h
13 from error import KmodError as _KmodError
14 cimport list as _list
15 import list as _list
16 cimport _util
17 import _util
18
19
20 cdef class Module (object):
21     "Wrap a struct kmod_module* item"
22     def __cinit__(self):
23         self.module = NULL
24
25     def __dealloc__(self):
26         self._cleanup()
27
28     def _cleanup(self):
29         if self.module is not NULL:
30             _libkmod_h.kmod_module_unref(self.module)
31             self.module = NULL
32
33     cpdef from_mod_list_item(self, _list.ModListItem item):
34         self._cleanup()
35         self.module = _libkmod_h.kmod_module_get_module(item.list)
36
37     def _name_get(self):
38         return _util.char_ptr_to_str(
39             _libkmod_h.kmod_module_get_name(self.module))
40     name = property(fget=_name_get)
41
42     def _path_get(self):
43         return _util.char_ptr_to_str(
44             _libkmod_h.kmod_module_get_path(self.module))
45     path = property(fget=_path_get)
46
47     def _options_get(self):
48         return _util.char_ptr_to_str(
49             _libkmod_h.kmod_module_get_options(self.module))
50     options = property(fget=_options_get)
51
52     def _install_commands_get(self):
53         return _util.char_ptr_to_str(
54             _libkmod_h.kmod_module_get_install_commands(self.module))
55     install_commands = property(fget=_install_commands_get)
56
57     def _remove_commands_get(self):
58         return _util.char_ptr_to_str(
59             _libkmod_h.kmod_module_get_remove_commands(self.module))
60     remove_commands = property(fget=_remove_commands_get)
61
62     def _refcnt_get(self):
63         return _libkmod_h.kmod_module_get_refcnt(self.module)
64     refcnt = property(fget=_refcnt_get)
65
66     def _size_get(self):
67         return _libkmod_h.kmod_module_get_size(self.module)
68     size = property(fget=_size_get)
69
70     def _versions_get(self):
71         cdef _list.ModList ml = _list.ModList()
72         cdef _list.ModListItem mli
73         err = _libkmod_h.kmod_module_get_versions(self.module, &ml.list)
74         if err < 0:
75             raise _KmodError('Could not get versions')
76         try:
77             for item in ml:
78                 mli = <_list.ModListItem> item
79                 symbol = _util.char_ptr_to_str(
80                     _libkmod_h.kmod_module_version_get_symbol(mli.list))
81                 crc = _libkmod_h.kmod_module_version_get_crc(mli.list)
82                 yield {'symbol': symbol, 'crc': crc}
83         finally:
84             _libkmod_h.kmod_module_versions_free_list(ml.list)
85             ml.list = NULL
86     versions = property(fget=_versions_get)
87
88     def insert(self, flags=0, extra_options=None, install_callback=None,
89                data=None, print_action_callback=None):
90         cdef char *opt = NULL
91         #cdef _libkmod_h.install_callback_t install = NULL
92         cdef int (*install)(
93             _libkmod_h.kmod_module *, _libkmod_h.const_char_ptr, void *)
94         install = NULL
95         cdef void *d = NULL
96         #cdef _libkmod_h.print_action_callback_t print_action = NULL
97         cdef void (*print_action)(
98             _libkmod_h.kmod_module *, _libkmod_h.bool,
99             _libkmod_h.const_char_ptr)
100         print_action = NULL
101         if extra_options:
102             opt = extra_options
103         # TODO: convert callbacks and data from Python object to C types
104         err = _libkmod_h.kmod_module_probe_insert_module(
105             self.module, flags, opt, install, d, print_action)
106         if err == -_libkmod_h.EEXIST:
107             raise _KmodError('Module already loaded')
108         elif err < 0:
109             raise _KmodError('Could not load module')
110
111     def remove(self, flags=0):
112         err = _libkmod_h.kmod_module_remove_module(self.module, flags)
113         if err < 0:
114             raise _KmodError('Could not remove module')