Commit 0c606c74 0c606c74e0195d00bb1410114e7b871c08c2a7f0 by Wojciech Polak

Python: Bugfixes.

* python/libmu_py/mailbox.c (api_mailbox_append_message): Bugfix.
* python/libmu_py/header.c (api_header_get_value_n): New function.
* python/mailutils/header.py (Header.get_value_n): New method.
* python/mailutils/mailbox.py (Mailbox.__str__): New method.
* python/mailutils/message.py (Message.__str__): Likewise.
1 parent 5171c0f9
...@@ -123,6 +123,21 @@ api_header_get_value (PyObject *self, PyObject *args) ...@@ -123,6 +123,21 @@ api_header_get_value (PyObject *self, PyObject *args)
123 } 123 }
124 124
125 static PyObject * 125 static PyObject *
126 api_header_get_value_n (PyObject *self, PyObject *args)
127 {
128 int status, n;
129 char *name;
130 const char *value = NULL;
131 PyHeader *py_hdr;
132
133 if (!PyArg_ParseTuple (args, "O!si", &PyHeaderType, &py_hdr, &name, &n))
134 return NULL;
135
136 status = mu_header_sget_value_n (py_hdr->hdr, name, n, &value);
137 return status_object (status, PyString_FromString (value ? value : ""));
138 }
139
140 static PyObject *
126 api_header_set_value (PyObject *self, PyObject *args) 141 api_header_set_value (PyObject *self, PyObject *args)
127 { 142 {
128 int status, replace = 1; 143 int status, replace = 1;
...@@ -191,6 +206,9 @@ static PyMethodDef methods[] = { ...@@ -191,6 +206,9 @@ static PyMethodDef methods[] = {
191 { "get_value", (PyCFunction) api_header_get_value, METH_VARARGS, 206 { "get_value", (PyCFunction) api_header_get_value, METH_VARARGS,
192 "Retrieve header field value." }, 207 "Retrieve header field value." },
193 208
209 { "get_value_n", (PyCFunction) api_header_get_value_n, METH_VARARGS,
210 "Retrieve Nth header field value." },
211
194 { "set_value", (PyCFunction) api_header_set_value, METH_VARARGS, 212 { "set_value", (PyCFunction) api_header_set_value, METH_VARARGS,
195 "Set header field value." }, 213 "Set header field value." },
196 214
......
...@@ -241,7 +241,7 @@ api_mailbox_append_message (PyObject *self, PyObject *args) ...@@ -241,7 +241,7 @@ api_mailbox_append_message (PyObject *self, PyObject *args)
241 } 241 }
242 242
243 status = mu_mailbox_append_message (py_mbox->mbox, py_msg->msg); 243 status = mu_mailbox_append_message (py_mbox->mbox, py_msg->msg);
244 return status_object (status, PyInt_FromLong (status)); 244 return _ro (PyInt_FromLong (status));
245 } 245 }
246 246
247 static PyObject * 247 static PyObject *
......
...@@ -96,6 +96,17 @@ class Header: ...@@ -96,6 +96,17 @@ class Header:
96 raise HeaderError (status) 96 raise HeaderError (status)
97 return value 97 return value
98 98
99 def get_value_n (self, name, n = 1, default = None):
100 status, value = header.get_value_n (self.hdr, name, n)
101 if status == MU_ERR_NOENT:
102 if default != None:
103 return default
104 else:
105 raise KeyError (name)
106 elif status:
107 raise HeaderError (status)
108 return value
109
99 def set_value (self, name, value, replace = True): 110 def set_value (self, name, value, replace = True):
100 status = header.set_value (self.hdr, name, value, replace) 111 status = header.set_value (self.hdr, name, value, replace)
101 if status: 112 if status:
......
...@@ -184,6 +184,8 @@ class MailboxBase: ...@@ -184,6 +184,8 @@ class MailboxBase:
184 def __len__ (self): 184 def __len__ (self):
185 return self.messages_count () 185 return self.messages_count ()
186 186
187 def __str__ (self):
188 return '<Mailbox %s (%d)>' % (self.get_url (), self.messages_count ())
187 189
188 class Mailbox (MailboxBase): 190 class Mailbox (MailboxBase):
189 __owner = False 191 __owner = False
......
...@@ -43,6 +43,16 @@ class Message: ...@@ -43,6 +43,16 @@ class Message:
43 message.destroy (self.msg) 43 message.destroy (self.msg)
44 del self.msg 44 del self.msg
45 45
46 def __str__ (self):
47 try:
48 env = self.get_envelope ()
49 envelope = '%s %s' % (env.get_sender ().strip (),
50 env.get_date ().strip ())
51 except MessageError:
52 envelope = 'UNKNOWN'
53 return '<Message "%s" %d %d>' % (envelope, self.get_lines (),
54 self.get_size ())
55
46 def __getattr__ (self, name): 56 def __getattr__ (self, name):
47 if name == 'header': 57 if name == 'header':
48 return self.get_header () 58 return self.get_header ()
......