| 1 | ### -*-pyrex-*- |
| 2 | ### |
| 3 | ### Selecting line-buffers |
| 4 | ### |
| 5 | ### (c) 2005 Straylight/Edgeware |
| 6 | ### |
| 7 | |
| 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. |
| 25 | |
| 26 | cdef class SelLineBuffer: |
| 27 | """ |
| 28 | SelLineBuffer(FILE, [lineproc = None], [eofproc = None]) |
| 29 | |
| 30 | Split an asynchronous stream into lines. |
| 31 | """ |
| 32 | cdef selbuf b |
| 33 | cdef _line |
| 34 | cdef _eof |
| 35 | def __cinit__(me, fd, lineproc = None, eofproc = None, *hunoz, **hukairz): |
| 36 | selbuf_init(&me.b, &_sel, _getfd(fd), _selbfunc, <void *>me) |
| 37 | selbuf_disable(&me.b) |
| 38 | me._line = _checkcallable(lineproc, 'line proc') |
| 39 | me._eof = _checkcallable(eofproc, 'eof proc') |
| 40 | def __dealloc__(me): |
| 41 | selbuf_destroy(&me.b) |
| 42 | property activep: |
| 43 | """SLB.activep -> BOOL: is the buffer still active?""" |
| 44 | def __get__(me): |
| 45 | return _tobool(me.b.b.f & LBUF_ENABLE) |
| 46 | property fd: |
| 47 | """SLB.fd -> INT: the file descriptor""" |
| 48 | def __get__(me): |
| 49 | return me.b.reader.fd |
| 50 | property delim: |
| 51 | """SLB.delim -> CHAR | LBUF_...: line-end delimiter""" |
| 52 | def __get__(me): |
| 53 | if me.b.b.delim == _LBUF_CRLF or me.b.b.delim == _LBUF_STRICTCRLF: |
| 54 | return me.b.b.delim |
| 55 | else: |
| 56 | return chr(me.b.b.delim) |
| 57 | def __set__(me, d): |
| 58 | if d == _LBUF_CRLF or d == _LBUF_STRICTCRLF: |
| 59 | me.b.b.delim = d |
| 60 | else: |
| 61 | me.b.b.delim = ord(d) |
| 62 | property size: |
| 63 | """SLB.size -> INT: buffer size limit""" |
| 64 | def __get__(me): |
| 65 | return me.b.b.sz |
| 66 | def __set__(me, sz): |
| 67 | if sz <= 0: |
| 68 | raise TypeError, 'size must be positive' |
| 69 | selbuf_setsize(&me.b, sz) |
| 70 | property lineproc: |
| 71 | """SLB.lineproc -> FUNC: call FUNC(LINE) on each line""" |
| 72 | def __get__(me): |
| 73 | return me._line |
| 74 | def __set__(me, proc): |
| 75 | me._line = _checkcallable(proc, 'line proc') |
| 76 | def __del__(me): |
| 77 | me._line = None |
| 78 | property eofproc: |
| 79 | """SLB.eofproc -> FUNC: call FUNC() at end-of-file""" |
| 80 | def __get__(me): |
| 81 | return me._eof |
| 82 | def __set__(me, proc): |
| 83 | me._eof = _checkcallable(proc, 'eof proc') |
| 84 | def __del__(me): |
| 85 | me._eof = None |
| 86 | def enable(me): |
| 87 | """SLB.enable(): enable the buffer, allowing lines to be emitted""" |
| 88 | if me.b.b.f & LBUF_ENABLE: |
| 89 | raise ValueError, 'already enabled' |
| 90 | selbuf_enable(&me.b) |
| 91 | me.enabled() |
| 92 | return me |
| 93 | def disable(me): |
| 94 | """SLB.disable(): disable the buffer, suspending line emission""" |
| 95 | if not (me.b.b.f & LBUF_ENABLE): |
| 96 | raise ValueError, 'already disabled' |
| 97 | selbuf_disable(&me.b) |
| 98 | me.disabled() |
| 99 | return me |
| 100 | def enabled(me): |
| 101 | """SLB.enabled(): called when buffer is enabled""" |
| 102 | pass |
| 103 | def disabled(me): |
| 104 | """SLB.disabled(): called when buffer is disabled""" |
| 105 | pass |
| 106 | def line(me, line): |
| 107 | """SLB.line(LINE): called for each completed line""" |
| 108 | return _maybecall(me._line, (line,)) |
| 109 | def eof(me): |
| 110 | """SLB.eof(): called at end-of-file""" |
| 111 | return _maybecall(me._eof, ()) |
| 112 | |
| 113 | cdef void _selbfunc(char *s, size_t n, void *arg): |
| 114 | cdef SelLineBuffer sb |
| 115 | sb = <SelLineBuffer>arg |
| 116 | if s is NULL: |
| 117 | sb.eof() |
| 118 | else: |
| 119 | sb.line(PyString_FromStringAndSize(s, n)) |
| 120 | |
| 121 | ###----- That's all, folks -------------------------------------------------- |