Add Module.info 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 libc.errno as _errno
13
14 cimport _libkmod_h
15 from error import KmodError as _KmodError
16 cimport list as _list
17 import list as _list
18 cimport _util
19 import _util
20
21
22 cdef class Module (object):
23     "Wrap a struct kmod_module* item"
24     def __cinit__(self):
25         self.module = NULL
26
27     def __dealloc__(self):
28         self._cleanup()
29
30     def _cleanup(self):
31         if self.module is not NULL:
32             _libkmod_h.kmod_module_unref(self.module)
33             self.module = NULL
34
35     cpdef from_mod_list_item(self, _list.ModListItem item):
36         self._cleanup()
37         self.module = _libkmod_h.kmod_module_get_module(item.list)
38
39     def _name_get(self):
40         return _util.char_ptr_to_str(
41             _libkmod_h.kmod_module_get_name(self.module))
42     name = property(fget=_name_get)
43
44     def _path_get(self):
45         return _util.char_ptr_to_str(
46             _libkmod_h.kmod_module_get_path(self.module))
47     path = property(fget=_path_get)
48
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)
53
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)
58
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)
63
64     def _refcnt_get(self):
65         return _libkmod_h.kmod_module_get_refcnt(self.module)
66     refcnt = property(fget=_refcnt_get)
67
68     def _size_get(self):
69         return _libkmod_h.kmod_module_get_size(self.module)
70     size = property(fget=_size_get)
71
72     def _info_get(self):
73         cdef _list.ModList ml = _list.ModList()
74         cdef _list.ModListItem mli
75         err = _libkmod_h.kmod_module_get_info(self.module, &ml.list)
76         if err < 0:
77             raise _KmodError('Could not get versions')
78         info = {}
79         try:
80             for item in ml:
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))
86                 info[key] = value
87         finally:
88             _libkmod_h.kmod_module_info_free_list(ml.list)
89             ml.list = NULL
90         return info
91     info = property(fget=_info_get)
92
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)
97         if err < 0:
98             raise _KmodError('Could not get versions')
99         try:
100             for item in ml:
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}
106         finally:
107             _libkmod_h.kmod_module_versions_free_list(ml.list)
108             ml.list = NULL
109     versions = property(fget=_versions_get)
110
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
115         cdef int (*install)(
116             _libkmod_h.kmod_module *, _libkmod_h.const_char_ptr, void *)
117         install = NULL
118         cdef void *d = NULL
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)
123         print_action = NULL
124         if extra_options:
125             opt = extra_options
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')
131         elif err < 0:
132             raise _KmodError('Could not load module')
133
134     def remove(self, flags=0):
135         err = _libkmod_h.kmod_module_remove_module(self.module, flags)
136         if err < 0:
137             raise _KmodError('Could not remove module')