### -*-pyrex-*- ### ### Selecting line-buffers ### ### (c) 2005 Straylight/Edgeware ### ###----- Licensing notice --------------------------------------------------- ### ### This file is part of the Python interface to mLib. ### ### mLib/Python is free software; you can redistribute it and/or modify ### it under the terms of the GNU General Public License as published by ### the Free Software Foundation; either version 2 of the License, or ### (at your option) any later version. ### ### mLib/Python is distributed in the hope that it will be useful, ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ### GNU General Public License for more details. ### ### You should have received a copy of the GNU General Public License ### along with mLib/Python; if not, write to the Free Software Foundation, ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cdef class SelLineBuffer: """ SelLineBuffer(FILE, [lineproc = None], [eofproc = None]) Split an asynchronous stream into lines. """ cdef selbuf b cdef _line cdef _eof def __cinit__(me, fd, lineproc = None, eofproc = None, *hunoz, **hukairz): selbuf_init(&me.b, &_sel, _getfd(fd), _selbfunc, me) selbuf_disable(&me.b) me._line = _checkcallable(lineproc, 'line proc') me._eof = _checkcallable(eofproc, 'eof proc') def __dealloc__(me): selbuf_destroy(&me.b) property activep: """SLB.activep -> BOOL: is the buffer still active?""" def __get__(me): return _tobool(me.b.b.f & LBUF_ENABLE) property fd: """SLB.fd -> INT: the file descriptor""" def __get__(me): return me.b.reader.fd property delim: """SLB.delim -> CHAR | LBUF_...: line-end delimiter""" def __get__(me): if me.b.b.delim == _LBUF_CRLF or me.b.b.delim == _LBUF_STRICTCRLF: return me.b.b.delim else: return chr(me.b.b.delim) def __set__(me, d): if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF: me.b.b.delim = d else: me.b.b.delim = ord(d) property size: """SLB.size -> INT: buffer size limit""" def __get__(me): return me.b.b.sz def __set__(me, sz): if sz <= 0: raise TypeError, 'size must be positive' selbuf_setsize(&me.b, sz) property lineproc: """SLB.lineproc -> FUNC: call FUNC(LINE) on each line""" def __get__(me): return me._line def __set__(me, proc): me._line = _checkcallable(proc, 'line proc') def __del__(me): me._line = None property eofproc: """SLB.eofproc -> FUNC: call FUNC() at end-of-file""" def __get__(me): return me._eof def __set__(me, proc): me._eof = _checkcallable(proc, 'eof proc') def __del__(me): me._eof = None def enable(me): """SLB.enable(): enable the buffer, allowing lines to be emitted""" if me.b.b.f & LBUF_ENABLE: raise ValueError, 'already enabled' selbuf_enable(&me.b) me.enabled() return me def disable(me): """SLB.disable(): disable the buffer, suspending line emission""" if not (me.b.b.f & LBUF_ENABLE): raise ValueError, 'already disabled' selbuf_disable(&me.b) me.disabled() return me def enabled(me): """SLB.enabled(): called when buffer is enabled""" pass def disabled(me): """SLB.disabled(): called when buffer is disabled""" pass def line(me, line): """SLB.line(LINE): called for each completed line""" return _maybecall(me._line, (line,)) def eof(me): """SLB.eof(): called at end-of-file""" return _maybecall(me._eof, ()) cdef void _selbfunc(char *s, size_t n, void *arg): cdef SelLineBuffer sb sb = arg if s is NULL: sb.eof() else: sb.line(PyString_FromStringAndSize(s, n)) ###----- That's all, folks --------------------------------------------------