Commit | Line | Data |
---|---|---|
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 | |
26 | cdef class SelLineBuffer: | |
addc0c37 MW |
27 | """ |
28 | SelLineBuffer(FILE, [lineproc = None], [eofproc = None]) | |
29 | ||
30 | Split an asynchronous stream into lines. | |
31 | """ | |
579d0169 | 32 | cdef selbuf b |
33 | cdef _line | |
34 | cdef _eof | |
376ad06d | 35 | def __cinit__(me, fd, lineproc = None, eofproc = None, *hunoz, **hukairz): |
579d0169 | 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: | |
addc0c37 | 43 | """SLB.activep -> BOOL: is the buffer still active?""" |
579d0169 | 44 | def __get__(me): |
45 | return _tobool(me.b.b.f & LBUF_ENABLE) | |
46 | property fd: | |
addc0c37 | 47 | """SLB.fd -> INT: the file descriptor""" |
579d0169 | 48 | def __get__(me): |
49 | return me.b.reader.fd | |
50 | property delim: | |
addc0c37 | 51 | """SLB.delim -> CHAR | LBUF_...: line-end delimiter""" |
579d0169 | 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: | |
addc0c37 | 63 | """SLB.size -> INT: buffer size limit""" |
579d0169 | 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: | |
addc0c37 | 71 | """SLB.lineproc -> FUNC: call FUNC(LINE) on each line""" |
579d0169 | 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: | |
addc0c37 | 79 | """SLB.eofproc -> FUNC: call FUNC() at end-of-file""" |
579d0169 | 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): | |
addc0c37 | 87 | """SLB.enable(): enable the buffer, allowing lines to be emitted""" |
579d0169 | 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): | |
addc0c37 | 94 | """SLB.disable(): disable the buffer, suspending line emission""" |
579d0169 | 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): | |
addc0c37 | 101 | """SLB.enabled(): called when buffer is enabled""" |
579d0169 | 102 | pass |
103 | def disabled(me): | |
addc0c37 | 104 | """SLB.disabled(): called when buffer is disabled""" |
579d0169 | 105 | pass |
106 | def line(me, line): | |
addc0c37 | 107 | """SLB.line(LINE): called for each completed line""" |
579d0169 | 108 | return _maybecall(me._line, (line,)) |
109 | def eof(me): | |
addc0c37 | 110 | """SLB.eof(): called at end-of-file""" |
579d0169 | 111 | return _maybecall(me._eof, ()) |
112 | ||
b51b6cf0 | 113 | cdef void _selbfunc(char *s, size_t n, void *arg): |
579d0169 | 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 | ||
5b1830f3 | 121 | ###----- That's all, folks -------------------------------------------------- |