### -*-pyrex-*- ### ### Line buffering ### ### (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. LBUF_CRLF = _LBUF_CRLF LBUF_STRICTCRLF = _LBUF_STRICTCRLF cdef class LineBuffer: """ LineBuffer([lineproc = None], [eofproc = None]) Split an incoming stream into lines. """ cdef lbuf b cdef _line cdef _eof def __cinit__(me, lineproc = None, eofproc = None, *hunoz, **hukairz): lbuf_init(&me.b, _lbfunc, me) me._line = _checkcallable(lineproc, 'line proc') me._eof = _checkcallable(eofproc, 'eof proc') def __dealloc__(me): lbuf_destroy(&me.b) property activep: """LB.activep -> BOOL: is the buffer still active?""" def __get__(me): return _tobool(me.b.f & LBUF_ENABLE) property delim: """LB.delim -> CHAR | LBUF_...: line-end delimiter""" def __get__(me): if me.b.delim == _LBUF_CRLF or me.b.delim == _LBUF_STRICTCRLF: return me.b.delim else: return chr(me.b.delim) def __set__(me, d): if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF: me.b.delim = d else: me.b.delim = ord(d) property size: """LB.size -> INT: buffer size limit""" def __get__(me): return me.b.sz def __set__(me, sz): if sz <= 0: raise TypeError, 'size must be positive' lbuf_setsize(&me.b, sz) property lineproc: """LB.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: """LB.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): """LB.enable(): enable the buffer, allowing lines to be emitted""" if me.b.f & LBUF_ENABLE: raise ValueError, 'already enabled' me.b.f = me.b.f | LBUF_ENABLE me.enabled() return me def disable(me): """LB.disable(): disable the buffer, suspending line emission""" if not (me.b.f & LBUF_ENABLE): raise ValueError, 'already disabled' me.b.f = me.b.f & ~LBUF_ENABLE me.disabled() return me def close(me): """LB.close(): report the end of the input stream""" if not (me.b.f & LBUF_ENABLE): raise ValueError, 'buffer disabled' lbuf_close(&me.b) return me property free: """LB.free -> INT: amount of space remaining in buffer""" def __get__(me): cdef char *p return lbuf_free(&me.b, &p) def flush(me, str): """LB.flush(STR) -> insert STR into the buffer and emit lines""" cdef Py_ssize_t len cdef char *p cdef char *q cdef size_t n PyString_AsStringAndSize(str, &p, &len) while len > 0: n = lbuf_free(&me.b, &q) if n > len: n = len memcpy(q, p, n) p = p + n len = len - n if not (me.b.f & LBUF_ENABLE): break lbuf_flush(&me.b, q, n) return PyString_FromStringAndSize(p, len) def enabled(me): """LB.enabled(): called when buffer is enabled""" pass def disabled(me): """LB.disabled(): called when buffer is disabled""" pass def line(me, line): """LB.line(LINE): called for each completed line""" return _maybecall(me._line, (line,)) def eof(me): """LB.eof(): called at end-of-file""" return _maybecall(me._eof, ()) cdef void _lbfunc(char *s, size_t n, void *arg): cdef LineBuffer sb sb = arg if s is NULL: sb.eof() else: sb.line(PyString_FromStringAndSize(s, n)) ###----- That's all, folks --------------------------------------------------