]> git.ipfire.org Git - thirdparty/kmod.git/blame - libkmod/python/kmod/kmod.pyx
python: Convert to Cython.
[thirdparty/kmod.git] / libkmod / python / kmod / kmod.pyx
CommitLineData
f7c62154
TK
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
12cimport cython as _cython
13cimport _libkmod_h
14from error import KmodError as _KmodError
15cimport module as _module
16import module as _module
17cimport list as _list
18import list as _list
19
20
21cdef class Kmod (object):
22 def __cinit__(self):
23 self._kmod_ctx = NULL
24
25 def __dealloc__(self):
26 self._cleanup()
27
28 def __init__(self, mod_dir=None):
29 self.set_mod_dir(mod_dir=mod_dir)
30
31 def set_mod_dir(self, mod_dir=None):
32 self.mod_dir = mod_dir
33 self._setup()
34
35 def _setup(self):
36 cdef char *mod_dir = NULL
37 self._cleanup()
38 if self.mod_dir:
39 mod_dir = self.mod_dir
40 self._kmod_ctx = _libkmod_h.kmod_new(mod_dir, NULL);
41 if self._kmod_ctx is NULL:
42 raise _KmodError('Could not initialize')
43 _libkmod_h.kmod_load_resources(self._kmod_ctx)
44
45 def _cleanup(self):
46 if self._kmod_ctx is not NULL:
47 _libkmod_h.kmod_unload_resources(self._kmod_ctx);
48 self._kmod_ctx = NULL
49
50 def loaded(self):
51 "iterate through currently loaded modules"
52 cdef _list.ModList ml = _list.ModList()
53 cdef _list.ModListItem mli
54 err = _libkmod_h.kmod_module_new_from_loaded(self._kmod_ctx, &ml.list)
55 if err < 0:
56 raise _KmodError('Could not get loaded modules')
57 for item in ml:
58 mli = <_list.ModListItem> item
59 mod = _module.Module()
60 mod.from_mod_list_item(item)
61 yield mod
62
63 def lookup(self, alias_name, flags=_libkmod_h.KMOD_PROBE_APPLY_BLACKLIST):
64 "iterate through modules matching `alias_name`"
65 cdef _list.ModList ml = _list.ModList()
66 cdef _list.ModListItem mli
67 if hasattr(alias_name, 'encode'):
68 alias_name = alias_name.encode('ascii')
69 err = _libkmod_h.kmod_module_new_from_lookup(
70 self._kmod_ctx, alias_name, &ml.list)
71 if err < 0:
72 raise _KmodError('Could not modprobe')
73 for item in ml:
74 mli = <_list.ModListItem> item
75 mod = _module.Module()
76 mod.from_mod_list_item(item)
77 yield mod
78
79 @_cython.always_allow_keywords(True)
80 def module_from_name(self, name):
81 cdef _module.Module mod = _module.Module()
82 if hasattr(name, 'encode'):
83 name = name.encode('ascii')
84 err = _libkmod_h.kmod_module_new_from_name(
85 self._kmod_ctx, name, &mod.module)
86 if err < 0:
87 raise _KmodError('Could not get module')
88 return mod
89
90 def list(self):
91 "iterate through currently loaded modules and sizes"
92 for mod in self.loaded():
93 yield (mod.name, mod.size)
94
95 def modprobe(self, alias_name, *args, **kwargs):
96 for mod in self.lookup(alias_name=alias_name):
97 mod.insert(*args, **kwargs)
98
99 def rmmod(self, module_name, *args, **kwargs):
100 mod = self.module_from_name(name=module_name)
101 mod.remove(*args, **kwargs)