Commit e3ef44ef e3ef44efce74604bf62cb630f172226afa91b76c by Wojciech Polak

Update Python API and the examples.

* python/libmu_py/folder.c (api_folder_set_stream): Remove.
* python/libmu_py/message.c (api_message_set_stream): Remove.
* python/libmu_py/stream.c (api_stream_ref, api_stream_unref): Add.
(api_stream_sequential_readline, api_stream_sequential_write): Remove.
1 parent 4f9d95ae
...@@ -22,19 +22,19 @@ if len (sys.argv) != 3: ...@@ -22,19 +22,19 @@ if len (sys.argv) != 3:
22 print "usage: %s from-code to-code" % sys.argv[0] 22 print "usage: %s from-code to-code" % sys.argv[0]
23 sys.exit (0) 23 sys.exit (0)
24 24
25 sti = stream.StdioStream (sys.stdin) 25 sti = stream.StdioStream (stream.MU_STDIN_FD)
26 sti.open () 26 sti.open ()
27 27
28 cvt = filter.FilterIconvStream (sti, sys.argv[1], sys.argv[2]) 28 cvt = filter.FilterIconvStream (sti, sys.argv[1], sys.argv[2])
29 cvt.open () 29 cvt.open ()
30 30
31 out = stream.StdioStream (sys.stdout, 0) 31 out = stream.StdioStream (stream.MU_STDOUT_FD, 0)
32 out.open () 32 out.open ()
33 33
34 total = 0 34 total = 0
35 while True: 35 while True:
36 buf = cvt.read (total) 36 buf = cvt.read (total)
37 out.sequential_write (buf) 37 out.write (buf)
38 total += cvt.read_count 38 total += cvt.read_count
39 if not cvt.read_count: 39 if not cvt.read_count:
40 break 40 break
......
...@@ -77,11 +77,9 @@ def message_display_parts (msg, indent): ...@@ -77,11 +77,9 @@ def message_display_parts (msg, indent):
77 print "%*.*sBegin" % (indent, indent, '') 77 print "%*.*sBegin" % (indent, indent, '')
78 78
79 flt = filter.FilterStream (part.body.get_stream (), encoding) 79 flt = filter.FilterStream (part.body.get_stream (), encoding)
80 offset = 0
81 80
82 while True: 81 while True:
83 buf = flt.readline (offset) 82 buf = flt.readline ()
84 offset += flt.read_count
85 if not flt.read_count: 83 if not flt.read_count:
86 break 84 break
87 print "%*.*s%s" % (indent, indent, '', buf), 85 print "%*.*s%s" % (indent, indent, '', buf),
......
...@@ -59,7 +59,7 @@ if optfrom: ...@@ -59,7 +59,7 @@ if optfrom:
59 if args: 59 if args:
60 to = address.Address (args) 60 to = address.Address (args)
61 61
62 sti = stream.StdioStream (sys.stdin, stream.MU_STREAM_SEEKABLE) 62 sti = stream.StdioStream (stream.MU_STDIN_FD, stream.MU_STREAM_SEEKABLE)
63 sti.open () 63 sti.open ()
64 64
65 msg = message.Message () 65 msg = message.Message ()
......
...@@ -116,7 +116,7 @@ api_body_get_stream (PyObject *self, PyObject *args) ...@@ -116,7 +116,7 @@ api_body_get_stream (PyObject *self, PyObject *args)
116 116
117 Py_INCREF (py_stm); 117 Py_INCREF (py_stm);
118 118
119 status = mu_body_get_stream (py_body->body, &py_stm->stm); 119 status = mu_body_get_streamref (py_body->body, &py_stm->stm);
120 return status_object (status, (PyObject *)py_stm); 120 return status_object (status, (PyObject *)py_stm);
121 } 121 }
122 122
......
...@@ -140,31 +140,11 @@ api_folder_get_stream (PyObject *self, PyObject *args) ...@@ -140,31 +140,11 @@ api_folder_get_stream (PyObject *self, PyObject *args)
140 140
141 Py_INCREF (py_stm); 141 Py_INCREF (py_stm);
142 142
143 status = mu_folder_get_stream (py_folder->folder, &py_stm->stm); 143 status = mu_folder_get_streamref (py_folder->folder, &py_stm->stm);
144 return status_object (status, (PyObject *)py_stm); 144 return status_object (status, (PyObject *)py_stm);
145 } 145 }
146 146
147 static PyObject * 147 static PyObject *
148 api_folder_set_stream (PyObject *self, PyObject *args)
149 {
150 int status;
151 PyFolder *py_folder;
152 PyStream *py_stm;
153
154 if (!PyArg_ParseTuple (args, "O!O", &PyFolderType, &py_folder, &py_stm))
155 return NULL;
156
157 if (!PyStream_Check ((PyObject *)py_stm))
158 {
159 PyErr_SetString (PyExc_TypeError, "");
160 return NULL;
161 }
162
163 status = mu_folder_set_stream (py_folder->folder, py_stm->stm);
164 return _ro (PyInt_FromLong (status));
165 }
166
167 static PyObject *
168 api_folder_get_authority (PyObject *self, PyObject *args) 148 api_folder_get_authority (PyObject *self, PyObject *args)
169 { 149 {
170 int status; 150 int status;
...@@ -276,9 +256,6 @@ static PyMethodDef methods[] = { ...@@ -276,9 +256,6 @@ static PyMethodDef methods[] = {
276 { "get_stream", (PyCFunction) api_folder_get_stream, METH_VARARGS, 256 { "get_stream", (PyCFunction) api_folder_get_stream, METH_VARARGS,
277 "" }, 257 "" },
278 258
279 { "set_stream", (PyCFunction) api_folder_set_stream, METH_VARARGS,
280 "" },
281
282 { "get_authority", (PyCFunction) api_folder_get_authority, METH_VARARGS, 259 { "get_authority", (PyCFunction) api_folder_get_authority, METH_VARARGS,
283 "" }, 260 "" },
284 261
......
...@@ -331,28 +331,6 @@ api_message_unencapsulate (PyObject *self, PyObject *args) ...@@ -331,28 +331,6 @@ api_message_unencapsulate (PyObject *self, PyObject *args)
331 return status_object (status, (PyObject *)py_unen); 331 return status_object (status, (PyObject *)py_unen);
332 } 332 }
333 333
334 static PyObject *
335 api_message_set_stream (PyObject *self, PyObject *args)
336 {
337 int status;
338 PyMessage *py_msg;
339 PyStream *py_stm;
340
341 if (!PyArg_ParseTuple (args, "O!O", &PyMessageType, &py_msg, &py_stm))
342 return NULL;
343
344 if (!PyStream_Check ((PyObject *)py_stm))
345 {
346 PyErr_SetString (PyExc_TypeError, "");
347 return NULL;
348 }
349
350 status = mu_message_set_stream (py_msg->msg, py_stm->stm, NULL);
351 py_stm->stm = NULL;
352
353 return _ro (PyInt_FromLong (status));
354 }
355
356 static PyMethodDef methods[] = { 334 static PyMethodDef methods[] = {
357 { "create", (PyCFunction) api_message_create, METH_VARARGS, 335 { "create", (PyCFunction) api_message_create, METH_VARARGS,
358 "Create message." }, 336 "Create message." },
...@@ -402,9 +380,6 @@ static PyMethodDef methods[] = { ...@@ -402,9 +380,6 @@ static PyMethodDef methods[] = {
402 { "unencapsulate", (PyCFunction) api_message_unencapsulate, 380 { "unencapsulate", (PyCFunction) api_message_unencapsulate,
403 METH_VARARGS, "" }, 381 METH_VARARGS, "" },
404 382
405 { "set_stream", (PyCFunction) api_message_set_stream, METH_VARARGS,
406 "" },
407
408 { NULL, NULL, 0, NULL } 383 { NULL, NULL, 0, NULL }
409 }; 384 };
410 385
......
...@@ -115,20 +115,13 @@ api_file_stream_create (PyObject *self, PyObject *args) ...@@ -115,20 +115,13 @@ api_file_stream_create (PyObject *self, PyObject *args)
115 static PyObject * 115 static PyObject *
116 api_stdio_stream_create (PyObject *self, PyObject *args) 116 api_stdio_stream_create (PyObject *self, PyObject *args)
117 { 117 {
118 int status, flags; 118 int status, fd, flags;
119 FILE *fp;
120 PyStream *py_stm; 119 PyStream *py_stm;
121 PyFileObject *py_file;
122 120
123 if (!PyArg_ParseTuple (args, "O!O!i", 121 if (!PyArg_ParseTuple (args, "O!ii", &PyStreamType, &py_stm, &fd, &flags))
124 &PyStreamType, &py_stm,
125 &PyFile_Type, &py_file,
126 &flags))
127 return NULL; 122 return NULL;
128 123
129 fp = PyFile_AsFile ((PyObject *)py_file); 124 status = mu_stdio_stream_create (&py_stm->stm, fd, flags);
130
131 status = mu_stdio_stream_create (&py_stm->stm, fp, flags);
132 return _ro (PyInt_FromLong (status)); 125 return _ro (PyInt_FromLong (status));
133 } 126 }
134 127
...@@ -166,6 +159,30 @@ api_filter_prog_stream_create (PyObject *self, PyObject *args) ...@@ -166,6 +159,30 @@ api_filter_prog_stream_create (PyObject *self, PyObject *args)
166 } 159 }
167 160
168 static PyObject * 161 static PyObject *
162 api_stream_ref (PyObject *self, PyObject *args)
163 {
164 PyStream *py_stm;
165
166 if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
167 return NULL;
168
169 mu_stream_ref (py_stm->stm);
170 return _ro (Py_None);
171 }
172
173 static PyObject *
174 api_stream_unref (PyObject *self, PyObject *args)
175 {
176 PyStream *py_stm;
177
178 if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
179 return NULL;
180
181 mu_stream_unref (py_stm->stm);
182 return _ro (Py_None);
183 }
184
185 static PyObject *
169 api_stream_destroy (PyObject *self, PyObject *args) 186 api_stream_destroy (PyObject *self, PyObject *args)
170 { 187 {
171 PyStream *py_stm; 188 PyStream *py_stm;
...@@ -173,7 +190,7 @@ api_stream_destroy (PyObject *self, PyObject *args) ...@@ -173,7 +190,7 @@ api_stream_destroy (PyObject *self, PyObject *args)
173 if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm)) 190 if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
174 return NULL; 191 return NULL;
175 192
176 mu_stream_destroy (&py_stm->stm, NULL); 193 mu_stream_destroy (&py_stm->stm);
177 return _ro (Py_None); 194 return _ro (Py_None);
178 } 195 }
179 196
...@@ -233,7 +250,6 @@ static PyObject * ...@@ -233,7 +250,6 @@ static PyObject *
233 api_stream_read (PyObject *self, PyObject *args) 250 api_stream_read (PyObject *self, PyObject *args)
234 { 251 {
235 int status; 252 int status;
236 size_t offset;
237 size_t read_count; 253 size_t read_count;
238 char rbuf[1024]; 254 char rbuf[1024];
239 PyObject *py_ret; 255 PyObject *py_ret;
...@@ -241,11 +257,10 @@ api_stream_read (PyObject *self, PyObject *args) ...@@ -241,11 +257,10 @@ api_stream_read (PyObject *self, PyObject *args)
241 257
242 memset (rbuf, 0, sizeof (rbuf)); 258 memset (rbuf, 0, sizeof (rbuf));
243 259
244 if (!PyArg_ParseTuple (args, "O!i", &PyStreamType, &py_stm, &offset)) 260 if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
245 return NULL; 261 return NULL;
246 262
247 status = mu_stream_read (py_stm->stm, rbuf, sizeof (rbuf), offset, 263 status = mu_stream_read (py_stm->stm, rbuf, sizeof (rbuf), &read_count);
248 &read_count);
249 264
250 py_ret = PyTuple_New (3); 265 py_ret = PyTuple_New (3);
251 PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status)); 266 PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
...@@ -258,17 +273,14 @@ static PyObject * ...@@ -258,17 +273,14 @@ static PyObject *
258 api_stream_write (PyObject *self, PyObject *args) 273 api_stream_write (PyObject *self, PyObject *args)
259 { 274 {
260 int status; 275 int status;
261 size_t offset;
262 size_t write_count;
263 char *wbuf; 276 char *wbuf;
277 size_t size, write_count;
264 PyStream *py_stm; 278 PyStream *py_stm;
265 279
266 if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, 280 if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, &wbuf, &size))
267 &wbuf, &offset))
268 return NULL; 281 return NULL;
269 282
270 status = mu_stream_write (py_stm->stm, wbuf, strlen (wbuf), offset, 283 status = mu_stream_write (py_stm->stm, wbuf, size, &write_count);
271 &write_count);
272 return status_object (status, PyInt_FromLong (write_count)); 284 return status_object (status, PyInt_FromLong (write_count));
273 } 285 }
274 286
...@@ -276,31 +288,6 @@ static PyObject * ...@@ -276,31 +288,6 @@ static PyObject *
276 api_stream_readline (PyObject *self, PyObject *args) 288 api_stream_readline (PyObject *self, PyObject *args)
277 { 289 {
278 int status; 290 int status;
279 size_t offset;
280 size_t read_count;
281 char rbuf[1024];
282 PyObject *py_ret;
283 PyStream *py_stm;
284
285 memset (rbuf, 0, sizeof (rbuf));
286
287 if (!PyArg_ParseTuple (args, "O!i", &PyStreamType, &py_stm, &offset))
288 return NULL;
289
290 status = mu_stream_readline (py_stm->stm, rbuf, sizeof (rbuf), offset,
291 &read_count);
292
293 py_ret = PyTuple_New (3);
294 PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
295 PyTuple_SetItem (py_ret, 1, PyString_FromString (rbuf));
296 PyTuple_SetItem (py_ret, 2, PyInt_FromLong (read_count));
297 return _ro (py_ret);
298 }
299
300 static PyObject *
301 api_stream_sequential_readline (PyObject *self, PyObject *args)
302 {
303 int status;
304 size_t read_count; 291 size_t read_count;
305 char rbuf[1024]; 292 char rbuf[1024];
306 PyObject *py_ret; 293 PyObject *py_ret;
...@@ -311,8 +298,7 @@ api_stream_sequential_readline (PyObject *self, PyObject *args) ...@@ -311,8 +298,7 @@ api_stream_sequential_readline (PyObject *self, PyObject *args)
311 if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm)) 298 if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
312 return NULL; 299 return NULL;
313 300
314 status = mu_stream_sequential_readline (py_stm->stm, rbuf, sizeof (rbuf), 301 status = mu_stream_readline (py_stm->stm, rbuf, sizeof (rbuf), &read_count);
315 &read_count);
316 302
317 py_ret = PyTuple_New (3); 303 py_ret = PyTuple_New (3);
318 PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status)); 304 PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
...@@ -321,22 +307,6 @@ api_stream_sequential_readline (PyObject *self, PyObject *args) ...@@ -321,22 +307,6 @@ api_stream_sequential_readline (PyObject *self, PyObject *args)
321 return _ro (py_ret); 307 return _ro (py_ret);
322 } 308 }
323 309
324 static PyObject *
325 api_stream_sequential_write (PyObject *self, PyObject *args)
326 {
327 int status;
328 char *wbuf;
329 size_t size;
330 PyStream *py_stm;
331
332 if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, &wbuf,
333 &size))
334 return NULL;
335
336 status = mu_stream_sequential_write (py_stm->stm, wbuf, size);
337 return _ro (PyInt_FromLong (status));
338 }
339
340 static PyMethodDef methods[] = { 310 static PyMethodDef methods[] = {
341 { "tcp_stream_create", (PyCFunction) api_tcp_stream_create, METH_VARARGS, 311 { "tcp_stream_create", (PyCFunction) api_tcp_stream_create, METH_VARARGS,
342 "" }, 312 "" },
...@@ -354,6 +324,12 @@ static PyMethodDef methods[] = { ...@@ -354,6 +324,12 @@ static PyMethodDef methods[] = {
354 (PyCFunction) api_filter_prog_stream_create, METH_VARARGS, 324 (PyCFunction) api_filter_prog_stream_create, METH_VARARGS,
355 "" }, 325 "" },
356 326
327 { "ref", (PyCFunction) api_stream_ref, METH_VARARGS,
328 "" },
329
330 { "unref", (PyCFunction) api_stream_unref, METH_VARARGS,
331 "" },
332
357 { "destroy", (PyCFunction) api_stream_destroy, METH_VARARGS, 333 { "destroy", (PyCFunction) api_stream_destroy, METH_VARARGS,
358 "" }, 334 "" },
359 335
...@@ -378,12 +354,6 @@ static PyMethodDef methods[] = { ...@@ -378,12 +354,6 @@ static PyMethodDef methods[] = {
378 { "readline", (PyCFunction) api_stream_readline, METH_VARARGS, 354 { "readline", (PyCFunction) api_stream_readline, METH_VARARGS,
379 "" }, 355 "" },
380 356
381 { "sequential_readline", (PyCFunction) api_stream_sequential_readline,
382 METH_VARARGS, "" },
383
384 { "sequential_write", (PyCFunction) api_stream_sequential_write,
385 METH_VARARGS, "" },
386
387 { NULL, NULL, 0, NULL } 357 { NULL, NULL, 0, NULL }
388 }; 358 };
389 359
......
...@@ -36,6 +36,10 @@ MU_STREAM_IROTH = 0x00004000 ...@@ -36,6 +36,10 @@ MU_STREAM_IROTH = 0x00004000
36 MU_STREAM_IWOTH = 0x00008000 36 MU_STREAM_IWOTH = 0x00008000
37 MU_STREAM_IMASK = 0x0000F000 37 MU_STREAM_IMASK = 0x0000F000
38 38
39 MU_STDIN_FD = 0
40 MU_STDOUT_FD = 1
41 MU_STDERR_FD = 2
42
39 class Stream: 43 class Stream:
40 __refcount = 0 44 __refcount = 0
41 45
...@@ -80,36 +84,25 @@ class Stream: ...@@ -80,36 +84,25 @@ class Stream:
80 if status: 84 if status:
81 raise StreamError (status) 85 raise StreamError (status)
82 86
83 def read (self, offset = 0): 87 def read (self):
84 status, rbuf, self.read_count = stream.read (self.stm, offset) 88 status, rbuf, self.read_count = stream.read (self.stm)
85 if status: 89 if status:
86 raise StreamError (status) 90 raise StreamError (status)
87 return rbuf 91 return rbuf
88 92
89 def write (self, wbuf, offset = 0): 93 def write (self, wbuf, size=None):
90 status, self.write_count = stream.write (self.stm, wbuf, offset) 94 if size == None:
91 if status: 95 size = len (wbuf)
92 raise StreamError (status) 96 status, self.write_count = stream.write (self.stm, wbuf, size)
93
94 def readline (self, offset = 0):
95 status, rbuf, self.read_count = stream.readline (self.stm, offset)
96 if status: 97 if status:
97 raise StreamError (status) 98 raise StreamError (status)
98 return rbuf
99 99
100 def sequential_readline (self): 100 def readline (self):
101 status, rbuf, self.read_count = stream.readline (self.stm) 101 status, rbuf, self.read_count = stream.readline (self.stm)
102 if status: 102 if status:
103 raise StreamError (status) 103 raise StreamError (status)
104 return rbuf 104 return rbuf
105 105
106 def sequential_write (self, wbuf, size = None):
107 if size == None:
108 size = len (wbuf)
109 status = stream.sequential_write (self.stm, wbuf, size)
110 if status:
111 raise StreamError (status)
112
113 class TcpStream (Stream): 106 class TcpStream (Stream):
114 def __init__ (self, host, port, flags = MU_STREAM_READ): 107 def __init__ (self, host, port, flags = MU_STREAM_READ):
115 Stream.__init__ (self) 108 Stream.__init__ (self)
...@@ -125,9 +118,9 @@ class FileStream (Stream): ...@@ -125,9 +118,9 @@ class FileStream (Stream):
125 raise StreamError (status) 118 raise StreamError (status)
126 119
127 class StdioStream (Stream): 120 class StdioStream (Stream):
128 def __init__ (self, file, flags = MU_STREAM_READ): 121 def __init__ (self, fd=MU_STDIN_FD, flags=MU_STREAM_READ):
129 Stream.__init__ (self) 122 Stream.__init__ (self)
130 status = stream.stdio_stream_create (self.stm, file, flags) 123 status = stream.stdio_stream_create (self.stm, fd, flags)
131 if status: 124 if status:
132 raise StreamError (status) 125 raise StreamError (status)
133 126
......