summaryrefslogtreecommitdiff
path: root/libkmod/python/kmod/module.pyx
blob: eadfbbf9b83ef837f7cddd9bc0ca61229f52c546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (C) 2012 W. Trevor King <wking@tremily.us>
#
# This file is part of python-kmod.
#
# python-kmod is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License version 2.1 as published
# by the Free Software Foundation.
#
# python-kmod is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with python-kmod.  If not, see <http://www.gnu.org/licenses/>.

import collections as _collections

cimport libc.errno as _errno

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):
    "Wrap a struct kmod_module* item"
    def __cinit__(self):
        self.module = NULL

    def __dealloc__(self):
        self._cleanup()

    def _cleanup(self):
        if self.module is not NULL:
            _libkmod_h.kmod_module_unref(self.module)
            self.module = NULL

    cpdef from_mod_list_item(self, _list.ModListItem item):
        self._cleanup()
        self.module = _libkmod_h.kmod_module_get_module(item.list)

    def _name_get(self):
        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)

    def _info_get(self):
        cdef _list.ModList ml = _list.ModList()
        cdef _list.ModListItem mli
        err = _libkmod_h.kmod_module_get_info(self.module, &ml.list)
        if err < 0:
            raise _KmodError('Could not get info')
        info = _collections.OrderedDict()
        try:
            for item in ml:
                mli = <_list.ModListItem> item
                key = _util.char_ptr_to_str(
                    _libkmod_h.kmod_module_info_get_key(mli.list))
                value = _util.char_ptr_to_str(
                    _libkmod_h.kmod_module_info_get_value(mli.list))
                info[key] = value
        finally:
            _libkmod_h.kmod_module_info_free_list(ml.list)
            ml.list = NULL
        return info
    info = property(fget=_info_get)

    def _versions_get(self):
        cdef _list.ModList ml = _list.ModList()
        cdef _list.ModListItem mli
        err = _libkmod_h.kmod_module_get_versions(self.module, &ml.list)
        if err < 0:
            raise _KmodError('Could not get versions')
        try:
            for item in ml:
                mli = <_list.ModListItem> item
                symbol = _util.char_ptr_to_str(
                    _libkmod_h.kmod_module_version_get_symbol(mli.list))
                crc = _libkmod_h.kmod_module_version_get_crc(mli.list)
                yield {'symbol': symbol, 'crc': crc}
        finally:
            _libkmod_h.kmod_module_versions_free_list(ml.list)
            ml.list = NULL
    versions = property(fget=_versions_get)

    def insert(self, flags=0, extra_options=None, install_callback=None,
               data=None, print_action_callback=None):
        cdef char *opt = NULL
        #cdef _libkmod_h.install_callback_t install = NULL
        cdef int (*install)(
            _libkmod_h.kmod_module *, _libkmod_h.const_char_ptr, void *)
        install = NULL
        cdef void *d = NULL
        #cdef _libkmod_h.print_action_callback_t print_action = NULL
        cdef void (*print_action)(
            _libkmod_h.kmod_module *, _libkmod_h.bool,
            _libkmod_h.const_char_ptr)
        print_action = NULL
        if extra_options:
            opt = extra_options
        # TODO: convert callbacks and data from Python object to C types
        err = _libkmod_h.kmod_module_probe_insert_module(
            self.module, flags, opt, install, d, print_action)
        if err == -_errno.EEXIST:
            raise _KmodError('Module already loaded')
        elif err < 0:
            raise _KmodError('Could not load module')

    def remove(self, flags=0):
        err = _libkmod_h.kmod_module_remove_module(self.module, flags)
        if err < 0:
            raise _KmodError('Could not remove module')