3 ### Selecting line-buffers
5 ### (c) 2005 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to mLib.
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.
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.
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.
26 cdef class SelLineBuffer:
28 SelLineBuffer(FILE, [lineproc = None], [eofproc = None])
30 Split an asynchronous stream into lines.
35 def __cinit__(me, fd, lineproc = None, eofproc = None, *hunoz, **hukairz):
36 selbuf_init(&me.b, &_sel, _getfd(fd), _selbfunc, <void *>me)
38 me._line = _checkcallable(lineproc, 'line proc')
39 me._eof = _checkcallable(eofproc, 'eof proc')
43 """SLB.activep -> BOOL: is the buffer still active?"""
45 return _tobool(me.b.b.f & LBUF_ENABLE)
47 """SLB.fd -> INT: the file descriptor"""
51 """SLB.delim -> CHAR | LBUF_...: line-end delimiter"""
53 if me.b.b.delim == _LBUF_CRLF or me.b.b.delim == _LBUF_STRICTCRLF:
56 return chr(me.b.b.delim)
58 if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF:
63 """SLB.size -> INT: buffer size limit"""
68 raise TypeError, 'size must be positive'
69 selbuf_setsize(&me.b, sz)
71 """SLB.lineproc -> FUNC: call FUNC(LINE) on each line"""
74 def __set__(me, proc):
75 me._line = _checkcallable(proc, 'line proc')
79 """SLB.eofproc -> FUNC: call FUNC() at end-of-file"""
82 def __set__(me, proc):
83 me._eof = _checkcallable(proc, 'eof proc')
87 """SLB.enable(): enable the buffer, allowing lines to be emitted"""
88 if me.b.b.f & LBUF_ENABLE:
89 raise ValueError, 'already enabled'
94 """SLB.disable(): disable the buffer, suspending line emission"""
95 if not (me.b.b.f & LBUF_ENABLE):
96 raise ValueError, 'already disabled'
101 """SLB.enabled(): called when buffer is enabled"""
104 """SLB.disabled(): called when buffer is disabled"""
107 """SLB.line(LINE): called for each completed line"""
108 return _maybecall(me._line, (line,))
110 """SLB.eof(): called at end-of-file"""
111 return _maybecall(me._eof, ())
113 cdef void _selbfunc(char *s, size_t n, void *arg):
114 cdef SelLineBuffer sb
115 sb = <SelLineBuffer>arg
119 sb.line(PyString_FromStringAndSize(s, n))
121 ###----- That's all, folks --------------------------------------------------