stream.py
4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#
# GNU Mailutils -- a suite of utilities for electronic mail
# Copyright (C) 2009 Free Software Foundation, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with this library; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
from mailutils.c_api import stream
from mailutils.error import StreamError
MU_STREAM_READ = 0x00000001
MU_STREAM_WRITE = 0x00000002
MU_STREAM_RDWR = 0x00000004
MU_STREAM_APPEND = 0x00000008
MU_STREAM_CREAT = 0x00000010
MU_STREAM_NONBLOCK = 0x00000020
MU_STREAM_NO_CHECK = 0x00000040
MU_STREAM_SEEKABLE = 0x00000080
MU_STREAM_NO_CLOSE = 0x00000100
MU_STREAM_ALLOW_LINKS = 0x00000200
MU_STREAM_NONLOCK = 0x00000400
MU_STREAM_QACCESS = 0x00000800
MU_STREAM_IRGRP = 0x00001000
MU_STREAM_IWGRP = 0x00002000
MU_STREAM_IROTH = 0x00004000
MU_STREAM_IWOTH = 0x00008000
MU_STREAM_IMASK = 0x0000F000
class Stream:
__refcount = 0
def __init__ (self, stm = None):
if isinstance (stm, stream.StreamType):
self.stm = stm
else:
self.stm = stream.StreamType ()
self.__reference ()
self.read_count = 0
self.write_count = 0
def __del__ (self):
if self.__dereference ():
stream.destroy (self.stm)
del self.stm
def __reference (self):
self.__refcount += 1
def __dereference (self):
self.__refcount -= 1
return self.__refcount == 0
def open (self):
status = stream.open (self.stm)
if status:
raise StreamError (status)
def close (self):
status = stream.close (self.stm)
if status:
raise StreamError (status)
def flush (self):
status = stream.flush (self.stm)
if status:
raise StreamError (status)
def wait (self, wflags):
status = stream.wait (self.stm, wflags)
if status:
raise StreamError (status)
def read (self, offset = 0):
status, rbuf, self.read_count = stream.read (self.stm, offset)
if status:
raise StreamError (status)
return rbuf
def write (self, wbuf, offset = 0):
status, self.write_count = stream.write (self.stm, wbuf, offset)
if status:
raise StreamError (status)
def readline (self, offset = 0):
status, rbuf, self.read_count = stream.readline (self.stm, offset)
if status:
raise StreamError (status)
return rbuf
def sequential_readline (self):
status, rbuf, self.read_count = stream.readline (self.stm)
if status:
raise StreamError (status)
return rbuf
def sequential_write (self, wbuf, size = None):
if size == None:
size = len (wbuf)
status = stream.sequential_write (self.stm, wbuf, size)
if status:
raise StreamError (status)
class TcpStream (Stream):
def __init__ (self, host, port, flags = MU_STREAM_READ):
Stream.__init__ (self)
status = stream.tcp_stream_create (self.stm, host, port, flags)
if status:
raise StreamError (status)
class FileStream (Stream):
def __init__ (self, filename, flags = MU_STREAM_READ):
Stream.__init__ (self)
status = stream.file_stream_create (self.stm, filename, flags)
if status:
raise StreamError (status)
class StdioStream (Stream):
def __init__ (self, file, flags = MU_STREAM_READ):
Stream.__init__ (self)
status = stream.stdio_stream_create (self.stm, file, flags)
if status:
raise StreamError (status)
class ProgStream (Stream):
def __init__ (self, progname, flags = MU_STREAM_READ):
Stream.__init__ (self)
status = stream.prog_stream_create (self.stm, progname, flags)
if status:
raise StreamError (status)