Commit | Line | Data |
---|---|---|
5b1830f3 MW |
1 | ### -*-pyrex-*- |
2 | ### | |
3 | ### File selectors | |
4 | ### | |
5 | ### (c) 2005 Straylight/Edgeware | |
6 | ### | |
579d0169 | 7 | |
5b1830f3 MW |
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. | |
579d0169 | 25 | |
26 | SEL_READ = _SEL_READ | |
27 | SEL_WRITE = _SEL_WRITE | |
28 | SEL_EXCEPT = _SEL_EXC | |
29 | ||
30 | cdef class SelFile: | |
addc0c37 MW |
31 | """ |
32 | SelFile(FILE, [mode = SEL_READ], [readyproc = None]) | |
33 | ||
34 | Register a file (or socket, or, ...) with the select loop. | |
35 | """ | |
579d0169 | 36 | cdef sel_file f |
37 | cdef int _activep | |
38 | cdef readonly unsigned mode | |
39 | cdef _readyfunc | |
376ad06d | 40 | def __cinit__(me, fd, int mode = SEL_READ, readyproc = None, |
579d0169 | 41 | *hunoz, **hukairz): |
42 | if (mode != _SEL_READ and | |
43 | mode != _SEL_WRITE and | |
44 | mode != _SEL_EXC): | |
45 | raise ValueError, 'bad select mode' | |
46 | sel_initfile(&_sel, &me.f, _getfd(fd), mode, _filefunc, <void *>me) | |
47 | me._activep = 0 | |
48 | me.mode = mode | |
8fdfc395 | 49 | me._readyfunc = _checkcallable(readyproc, 'ready proc') |
579d0169 | 50 | def __dealloc__(me): |
51 | if me._activep: | |
52 | sel_rmfile(&me.f) | |
53 | property fd: | |
addc0c37 | 54 | """SF.fd -> INT: the file descriptor""" |
579d0169 | 55 | def __get__(me): |
56 | return me.f.fd | |
57 | property activep: | |
addc0c37 | 58 | """SF.activep -> BOOL: is the descriptor active?""" |
579d0169 | 59 | def __get__(me): |
60 | return _tobool(me._activep) | |
61 | property readyproc: | |
addc0c37 | 62 | """SF.readyproc -> FUNC: call FUNC() when file is ready for I/O""" |
579d0169 | 63 | def __get__(me): |
8fdfc395 | 64 | return me._readyfunc |
579d0169 | 65 | def __set__(me, proc): |
8fdfc395 | 66 | me._readyfunc = _checkcallable(proc, 'ready proc') |
579d0169 | 67 | def __del__(me): |
8fdfc395 | 68 | me._readyfunc = None |
579d0169 | 69 | def enable(me): |
addc0c37 | 70 | """SF.enable(): enable waiting on file""" |
579d0169 | 71 | if me._activep: |
72 | raise ValueError, 'already enabled' | |
73 | sel_addfile(&me.f) | |
74 | me._enabled() | |
75 | return me | |
76 | def disable(me): | |
addc0c37 | 77 | """SF.disable(): disable waiting on file""" |
579d0169 | 78 | if not me._activep: |
79 | raise ValueError, 'already disabled' | |
80 | sel_rmfile(&me.f) | |
81 | me._disabled() | |
82 | return me | |
83 | def force(me): | |
addc0c37 | 84 | """SF.force(): artificially mark file as ready""" |
579d0169 | 85 | sel_force(&me.f) |
86 | return me | |
87 | cdef _enabled(me): | |
88 | me._activep = 1 | |
89 | me.enabled() | |
90 | cdef _disabled(me): | |
91 | me._activep = 0 | |
92 | me.disabled() | |
93 | def enabled(me): | |
addc0c37 | 94 | """SF.enabled(): called when file is enabled""" |
579d0169 | 95 | pass |
96 | def disabled(me): | |
addc0c37 | 97 | """SF.disabled(): called when file is disabled""" |
579d0169 | 98 | pass |
99 | def ready(me): | |
addc0c37 | 100 | """SF.ready(): called when file is ready for I/O""" |
8fdfc395 | 101 | return _maybecall(me._readyfunc, ()) |
579d0169 | 102 | |
b51b6cf0 | 103 | cdef void _filefunc(int fd, unsigned mode, void *arg): |
579d0169 | 104 | cdef SelFile sf |
105 | sf = <SelFile>arg | |
106 | sf.ready() | |
107 | ||
5b1830f3 | 108 | ###----- That's all, folks -------------------------------------------------- |