chiark / gitweb /
debian/control: Add Build-Depends for `dh-python'.
[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   """
32   SelFile(FILE, [mode = SEL_READ], [readyproc = None])
33
34   Register a file (or socket, or, ...) with the select loop.
35   """
36   cdef sel_file f
37   cdef int _activep
38   cdef readonly unsigned mode
39   cdef _readyfunc
40   def __cinit__(me, fd, int mode = SEL_READ, readyproc = None,
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
49     me._readyfunc = _checkcallable(readyproc, 'ready proc')
50   def __dealloc__(me):
51     if me._activep:
52       sel_rmfile(&me.f)
53   property fd:
54     """SF.fd -> INT: the file descriptor"""
55     def __get__(me):
56       return me.f.fd
57   property activep:
58     """SF.activep -> BOOL: is the descriptor active?"""
59     def __get__(me):
60       return _tobool(me._activep)
61   property readyproc:
62     """SF.readyproc -> FUNC: call FUNC() when file is ready for I/O"""
63     def __get__(me):
64       return me._readyfunc
65     def __set__(me, proc):
66       me._readyfunc = _checkcallable(proc, 'ready proc')
67     def __del__(me):
68       me._readyfunc = None
69   def enable(me):
70     """SF.enable(): enable waiting on file"""
71     if me._activep:
72       raise ValueError, 'already enabled'
73     sel_addfile(&me.f)
74     me._enabled()
75     return me
76   def disable(me):
77     """SF.disable(): disable waiting on file"""
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):
84     """SF.force(): artificially mark file as ready"""
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):
94     """SF.enabled(): called when file is enabled"""
95     pass
96   def disabled(me):
97     """SF.disabled(): called when file is disabled"""
98     pass
99   def ready(me):
100     """SF.ready(): called when file is ready for I/O"""
101     return _maybecall(me._readyfunc, ())
102
103 cdef void _filefunc(int fd, unsigned mode, void *arg):
104   cdef SelFile sf
105   sf = <SelFile>arg
106   sf.ready()
107
108 ###----- That's all, folks --------------------------------------------------