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