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:
print "usage: %s from-code to-code" % sys.argv[0]
sys.exit (0)
sti = stream.StdioStream (sys.stdin)
sti = stream.StdioStream (stream.MU_STDIN_FD)
sti.open ()
cvt = filter.FilterIconvStream (sti, sys.argv[1], sys.argv[2])
cvt.open ()
out = stream.StdioStream (sys.stdout, 0)
out = stream.StdioStream (stream.MU_STDOUT_FD, 0)
out.open ()
total = 0
while True:
buf = cvt.read (total)
out.sequential_write (buf)
out.write (buf)
total += cvt.read_count
if not cvt.read_count:
break
......
......@@ -77,11 +77,9 @@ def message_display_parts (msg, indent):
print "%*.*sBegin" % (indent, indent, '')
flt = filter.FilterStream (part.body.get_stream (), encoding)
offset = 0
while True:
buf = flt.readline (offset)
offset += flt.read_count
buf = flt.readline ()
if not flt.read_count:
break
print "%*.*s%s" % (indent, indent, '', buf),
......
......@@ -59,7 +59,7 @@ if optfrom:
if args:
to = address.Address (args)
sti = stream.StdioStream (sys.stdin, stream.MU_STREAM_SEEKABLE)
sti = stream.StdioStream (stream.MU_STDIN_FD, stream.MU_STREAM_SEEKABLE)
sti.open ()
msg = message.Message ()
......
......@@ -116,7 +116,7 @@ api_body_get_stream (PyObject *self, PyObject *args)
Py_INCREF (py_stm);
status = mu_body_get_stream (py_body->body, &py_stm->stm);
status = mu_body_get_streamref (py_body->body, &py_stm->stm);
return status_object (status, (PyObject *)py_stm);
}
......
......@@ -140,31 +140,11 @@ api_folder_get_stream (PyObject *self, PyObject *args)
Py_INCREF (py_stm);
status = mu_folder_get_stream (py_folder->folder, &py_stm->stm);
status = mu_folder_get_streamref (py_folder->folder, &py_stm->stm);
return status_object (status, (PyObject *)py_stm);
}
static PyObject *
api_folder_set_stream (PyObject *self, PyObject *args)
{
int status;
PyFolder *py_folder;
PyStream *py_stm;
if (!PyArg_ParseTuple (args, "O!O", &PyFolderType, &py_folder, &py_stm))
return NULL;
if (!PyStream_Check ((PyObject *)py_stm))
{
PyErr_SetString (PyExc_TypeError, "");
return NULL;
}
status = mu_folder_set_stream (py_folder->folder, py_stm->stm);
return _ro (PyInt_FromLong (status));
}
static PyObject *
api_folder_get_authority (PyObject *self, PyObject *args)
{
int status;
......@@ -276,9 +256,6 @@ static PyMethodDef methods[] = {
{ "get_stream", (PyCFunction) api_folder_get_stream, METH_VARARGS,
"" },
{ "set_stream", (PyCFunction) api_folder_set_stream, METH_VARARGS,
"" },
{ "get_authority", (PyCFunction) api_folder_get_authority, METH_VARARGS,
"" },
......
......@@ -331,28 +331,6 @@ api_message_unencapsulate (PyObject *self, PyObject *args)
return status_object (status, (PyObject *)py_unen);
}
static PyObject *
api_message_set_stream (PyObject *self, PyObject *args)
{
int status;
PyMessage *py_msg;
PyStream *py_stm;
if (!PyArg_ParseTuple (args, "O!O", &PyMessageType, &py_msg, &py_stm))
return NULL;
if (!PyStream_Check ((PyObject *)py_stm))
{
PyErr_SetString (PyExc_TypeError, "");
return NULL;
}
status = mu_message_set_stream (py_msg->msg, py_stm->stm, NULL);
py_stm->stm = NULL;
return _ro (PyInt_FromLong (status));
}
static PyMethodDef methods[] = {
{ "create", (PyCFunction) api_message_create, METH_VARARGS,
"Create message." },
......@@ -402,9 +380,6 @@ static PyMethodDef methods[] = {
{ "unencapsulate", (PyCFunction) api_message_unencapsulate,
METH_VARARGS, "" },
{ "set_stream", (PyCFunction) api_message_set_stream, METH_VARARGS,
"" },
{ NULL, NULL, 0, NULL }
};
......
......@@ -115,20 +115,13 @@ api_file_stream_create (PyObject *self, PyObject *args)
static PyObject *
api_stdio_stream_create (PyObject *self, PyObject *args)
{
int status, flags;
FILE *fp;
int status, fd, flags;
PyStream *py_stm;
PyFileObject *py_file;
if (!PyArg_ParseTuple (args, "O!O!i",
&PyStreamType, &py_stm,
&PyFile_Type, &py_file,
&flags))
if (!PyArg_ParseTuple (args, "O!ii", &PyStreamType, &py_stm, &fd, &flags))
return NULL;
fp = PyFile_AsFile ((PyObject *)py_file);
status = mu_stdio_stream_create (&py_stm->stm, fp, flags);
status = mu_stdio_stream_create (&py_stm->stm, fd, flags);
return _ro (PyInt_FromLong (status));
}
......@@ -166,6 +159,30 @@ api_filter_prog_stream_create (PyObject *self, PyObject *args)
}
static PyObject *
api_stream_ref (PyObject *self, PyObject *args)
{
PyStream *py_stm;
if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
return NULL;
mu_stream_ref (py_stm->stm);
return _ro (Py_None);
}
static PyObject *
api_stream_unref (PyObject *self, PyObject *args)
{
PyStream *py_stm;
if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
return NULL;
mu_stream_unref (py_stm->stm);
return _ro (Py_None);
}
static PyObject *
api_stream_destroy (PyObject *self, PyObject *args)
{
PyStream *py_stm;
......@@ -173,7 +190,7 @@ api_stream_destroy (PyObject *self, PyObject *args)
if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
return NULL;
mu_stream_destroy (&py_stm->stm, NULL);
mu_stream_destroy (&py_stm->stm);
return _ro (Py_None);
}
......@@ -233,7 +250,6 @@ static PyObject *
api_stream_read (PyObject *self, PyObject *args)
{
int status;
size_t offset;
size_t read_count;
char rbuf[1024];
PyObject *py_ret;
......@@ -241,11 +257,10 @@ api_stream_read (PyObject *self, PyObject *args)
memset (rbuf, 0, sizeof (rbuf));
if (!PyArg_ParseTuple (args, "O!i", &PyStreamType, &py_stm, &offset))
if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
return NULL;
status = mu_stream_read (py_stm->stm, rbuf, sizeof (rbuf), offset,
&read_count);
status = mu_stream_read (py_stm->stm, rbuf, sizeof (rbuf), &read_count);
py_ret = PyTuple_New (3);
PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
......@@ -258,17 +273,14 @@ static PyObject *
api_stream_write (PyObject *self, PyObject *args)
{
int status;
size_t offset;
size_t write_count;
char *wbuf;
size_t size, write_count;
PyStream *py_stm;
if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm,
&wbuf, &offset))
if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, &wbuf, &size))
return NULL;
status = mu_stream_write (py_stm->stm, wbuf, strlen (wbuf), offset,
&write_count);
status = mu_stream_write (py_stm->stm, wbuf, size, &write_count);
return status_object (status, PyInt_FromLong (write_count));
}
......@@ -276,31 +288,6 @@ static PyObject *
api_stream_readline (PyObject *self, PyObject *args)
{
int status;
size_t offset;
size_t read_count;
char rbuf[1024];
PyObject *py_ret;
PyStream *py_stm;
memset (rbuf, 0, sizeof (rbuf));
if (!PyArg_ParseTuple (args, "O!i", &PyStreamType, &py_stm, &offset))
return NULL;
status = mu_stream_readline (py_stm->stm, rbuf, sizeof (rbuf), offset,
&read_count);
py_ret = PyTuple_New (3);
PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
PyTuple_SetItem (py_ret, 1, PyString_FromString (rbuf));
PyTuple_SetItem (py_ret, 2, PyInt_FromLong (read_count));
return _ro (py_ret);
}
static PyObject *
api_stream_sequential_readline (PyObject *self, PyObject *args)
{
int status;
size_t read_count;
char rbuf[1024];
PyObject *py_ret;
......@@ -311,8 +298,7 @@ api_stream_sequential_readline (PyObject *self, PyObject *args)
if (!PyArg_ParseTuple (args, "O!", &PyStreamType, &py_stm))
return NULL;
status = mu_stream_sequential_readline (py_stm->stm, rbuf, sizeof (rbuf),
&read_count);
status = mu_stream_readline (py_stm->stm, rbuf, sizeof (rbuf), &read_count);
py_ret = PyTuple_New (3);
PyTuple_SetItem (py_ret, 0, PyInt_FromLong (status));
......@@ -321,22 +307,6 @@ api_stream_sequential_readline (PyObject *self, PyObject *args)
return _ro (py_ret);
}
static PyObject *
api_stream_sequential_write (PyObject *self, PyObject *args)
{
int status;
char *wbuf;
size_t size;
PyStream *py_stm;
if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, &wbuf,
&size))
return NULL;
status = mu_stream_sequential_write (py_stm->stm, wbuf, size);
return _ro (PyInt_FromLong (status));
}
static PyMethodDef methods[] = {
{ "tcp_stream_create", (PyCFunction) api_tcp_stream_create, METH_VARARGS,
"" },
......@@ -354,6 +324,12 @@ static PyMethodDef methods[] = {
(PyCFunction) api_filter_prog_stream_create, METH_VARARGS,
"" },
{ "ref", (PyCFunction) api_stream_ref, METH_VARARGS,
"" },
{ "unref", (PyCFunction) api_stream_unref, METH_VARARGS,
"" },
{ "destroy", (PyCFunction) api_stream_destroy, METH_VARARGS,
"" },
......@@ -378,12 +354,6 @@ static PyMethodDef methods[] = {
{ "readline", (PyCFunction) api_stream_readline, METH_VARARGS,
"" },
{ "sequential_readline", (PyCFunction) api_stream_sequential_readline,
METH_VARARGS, "" },
{ "sequential_write", (PyCFunction) api_stream_sequential_write,
METH_VARARGS, "" },
{ NULL, NULL, 0, NULL }
};
......
......@@ -36,6 +36,10 @@ MU_STREAM_IROTH = 0x00004000
MU_STREAM_IWOTH = 0x00008000
MU_STREAM_IMASK = 0x0000F000
MU_STDIN_FD = 0
MU_STDOUT_FD = 1
MU_STDERR_FD = 2
class Stream:
__refcount = 0
......@@ -80,36 +84,25 @@ class Stream:
if status:
raise StreamError (status)
def read (self, offset = 0):
status, rbuf, self.read_count = stream.read (self.stm, offset)
def read (self):
status, rbuf, self.read_count = stream.read (self.stm)
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)
def write (self, wbuf, size=None):
if size == None:
size = len (wbuf)
status, self.write_count = stream.write (self.stm, wbuf, size)
if status:
raise StreamError (status)
return rbuf
def sequential_readline (self):
def 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)
......@@ -125,9 +118,9 @@ class FileStream (Stream):
raise StreamError (status)
class StdioStream (Stream):
def __init__ (self, file, flags = MU_STREAM_READ):
def __init__ (self, fd=MU_STDIN_FD, flags=MU_STREAM_READ):
Stream.__init__ (self)
status = stream.stdio_stream_create (self.stm, file, flags)
status = stream.stdio_stream_create (self.stm, fd, flags)
if status:
raise StreamError (status)
......