chiark / gitweb /
array.h: Fix typo in declaration of da_pysetup.
[mLib-python] / selbuf.pyx
1 # -*-pyrex-*-
2 #
3 # $Id$
4 #
5 # Selecting line-buffers
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 cdef class SelLineBuffer:
29   cdef selbuf b
30   cdef _line
31   cdef _eof
32   def __new__(me, fd, lineproc = None, eofproc = None, *hunoz, **hukairz):
33     selbuf_init(&me.b, &_sel, _getfd(fd), _selbfunc, <void *>me)
34     selbuf_disable(&me.b)
35     me._line = _checkcallable(lineproc, 'line proc')
36     me._eof = _checkcallable(eofproc, 'eof proc')
37   def __dealloc__(me):
38     selbuf_destroy(&me.b)
39   property activep:
40     def __get__(me):
41       return _tobool(me.b.b.f & LBUF_ENABLE)
42   property fd:
43     def __get__(me):
44       return me.b.reader.fd
45   property delim:
46     def __get__(me):
47       if me.b.b.delim == _LBUF_CRLF or me.b.b.delim == _LBUF_STRICTCRLF:
48         return me.b.b.delim
49       else:
50         return chr(me.b.b.delim)
51     def __set__(me, d):
52       if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF:
53         me.b.b.delim = d
54       else:
55         me.b.b.delim = ord(d)
56   property size:
57     def __get__(me):
58       return me.b.b.sz
59     def __set__(me, sz):
60       if sz <= 0:
61         raise TypeError, 'size must be positive'
62       selbuf_setsize(&me.b, sz)
63   property lineproc:
64     def __get__(me):
65       return me._line
66     def __set__(me, proc):
67       me._line = _checkcallable(proc, 'line proc')
68     def __del__(me):
69       me._line = None
70   property eofproc:
71     def __get__(me):
72       return me._eof
73     def __set__(me, proc):
74       me._eof = _checkcallable(proc, 'eof proc')
75     def __del__(me):
76       me._eof = None
77   def enable(me):
78     if me.b.b.f & LBUF_ENABLE:
79       raise ValueError, 'already enabled'
80     selbuf_enable(&me.b)
81     me.enabled()
82     return me
83   def disable(me):
84     if not (me.b.b.f & LBUF_ENABLE):
85       raise ValueError, 'already disabled'
86     selbuf_disable(&me.b)
87     me.disabled()
88     return me
89   def enabled(me):
90     pass
91   def disabled(me):
92     pass
93   def line(me, line):
94     return _maybecall(me._line, (line,))
95   def eof(me):
96     return _maybecall(me._eof, ())
97
98 cdef void _selbfunc(char *s, size_t n, void *arg):
99   cdef SelLineBuffer sb
100   sb = <SelLineBuffer>arg
101   if s is NULL:
102     sb.eof()
103   else:
104     sb.line(PyString_FromStringAndSize(s, n))
105
106 #----- That's all, folks ----------------------------------------------------