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