chiark / gitweb /
mdup.pyx: New support for mLib's glorious `mdup' function.
[mLib-python] / sel-file.pyx
1 # -*-pyrex-*-
2 #
3 # $Id$
4 #
5 # File selectors
6 #
7 # (c) 2005 Straylight/Edgeware
8 #
9
10 #----- Licensing notice -----------------------------------------------------
11 #
12 # This file is part of the Python interface to mLib.
13 #
14 # mLib/Python is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # mLib/Python is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with mLib/Python; if not, write to the Free Software Foundation,
26 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28 SEL_READ = _SEL_READ
29 SEL_WRITE = _SEL_WRITE
30 SEL_EXCEPT = _SEL_EXC
31
32 cdef class SelFile:
33   cdef sel_file f
34   cdef int _activep
35   cdef readonly unsigned mode
36   cdef _readyfunc
37   def __new__(me, fd, int mode = SEL_READ, readyproc = None,
38               *hunoz, **hukairz):
39     if (mode != _SEL_READ and
40         mode != _SEL_WRITE and
41         mode != _SEL_EXC):
42       raise ValueError, 'bad select mode'
43     sel_initfile(&_sel, &me.f, _getfd(fd), mode, _filefunc, <void *>me)
44     me._activep = 0
45     me.mode = mode
46     me._readyfunc = _checkcallable(readyproc, 'ready proc')
47   def __dealloc__(me):
48     if me._activep:
49       sel_rmfile(&me.f)
50   property fd:
51     def __get__(me):
52       return me.f.fd
53   property activep:
54     def __get__(me):
55       return _tobool(me._activep)
56   property readyproc:
57     def __get__(me):
58       return me._readyfunc
59     def __set__(me, proc):
60       me._readyfunc = _checkcallable(proc, 'ready proc')
61     def __del__(me):
62       me._readyfunc = None
63   def enable(me):
64     if me._activep:
65       raise ValueError, 'already enabled'
66     sel_addfile(&me.f)
67     me._enabled()
68     return me
69   def disable(me):
70     if not me._activep:
71       raise ValueError, 'already disabled'
72     sel_rmfile(&me.f)
73     me._disabled()
74     return me
75   def force(me):
76     sel_force(&me.f)
77     return me
78   cdef _enabled(me):
79     me._activep = 1
80     me.enabled()
81   cdef _disabled(me):
82     me._activep = 0
83     me.disabled()
84   def enabled(me):
85     pass
86   def disabled(me):
87     pass
88   def ready(me):
89     return _maybecall(me._readyfunc, ())
90
91 cdef void _filefunc(int fd, unsigned mode, void *arg):
92   cdef SelFile sf
93   sf = <SelFile>arg
94   sf.ready()
95
96 #----- That's all, folks ----------------------------------------------------