← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~danilo/pygettextpo/msgctxt-support into lp:pygettextpo

 

Данило Шеган has proposed merging lp:~danilo/pygettextpo/msgctxt-support into lp:pygettextpo with lp:~danilo/pygettextpo/simplify as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~danilo/pygettextpo/msgctxt-support/+merge/92726

Provide support for msgctxt field in GNU gettext PO files.

This depends on the 'simplify' branch which simplifies the C code to reduce repeating of the same pattern. A new test is added to ensure this works like the other fields.

http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files
-- 
https://code.launchpad.net/~danilo/pygettextpo/msgctxt-support/+merge/92726
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~danilo/pygettextpo/msgctxt-support into lp:pygettextpo.
=== modified file 'gettextpo.c'
--- gettextpo.c	2009-06-02 15:31:25 +0000
+++ gettextpo.c	2012-02-13 10:08:18 +0000
@@ -11,6 +11,7 @@
 #include <gettext-po.h>
 
 #define MIN_REQUIRED_GETTEXTPO_VERSION 0x000E02
+#define MIN_MSGCTXT_GETTEXTPO_VERSION 0x000F00
 
 #if LIBGETTEXTPO_VERSION < MIN_REQUIRED_GETTEXTPO_VERSION
 #  error "this module requires gettext >= 0.14.2"
@@ -533,35 +534,60 @@
     return res;
 }
 
+typedef void (po_value_setter_t)(po_message_t, const char *);
+
+static PyObject *
+_message_set_field(PyPoMessage *self, PyObject *args, const char *field,
+                         po_value_setter_t * setter)
+{
+    const char *value;
+    PyObject *object;
+    PyObject *string;
+
+    if (!PyArg_ParseTuple(args, field, &object))
+        return NULL;
+
+    if (object == Py_None) {
+        (*setter)(self->msg, NULL);
+    } else {
+        string = get_pystring_from_pyobject(object);
+
+        if (string == NULL)
+            /* Got an exception */
+            return NULL;
+        else {
+            value = PyString_AsString(string);
+            (*setter)(self->msg, value);
+            Py_DECREF(string);
+        }
+    }
+
+    Py_RETURN_NONE;
+}
+
+/* msgctxt support was added in 0.15. */
+#if LIBGETTEXTPO_VERSION >= MIN_MSGCTXT_GETTEXTPO_VERSION
+
+PyDoc_STRVAR(doc_pypo_message_set_msgctxt,
+"M.set_msgctxt(msgctxt) -> None.  Set the msgctxt for this PoMessage");
+
+static PyObject *
+pypo_message_set_msgctxt(PyPoMessage *self, PyObject *args)
+{
+    return _message_set_field(self, args, "O:set_msgctxt",
+                                    &po_message_set_msgctxt);
+}
+#endif
+
+
 PyDoc_STRVAR(doc_pypo_message_set_msgid,
 "M.set_msgid(msgid) -> None.  Set the msgid for this PoMessage");
 
 static PyObject *
 pypo_message_set_msgid(PyPoMessage *self, PyObject *args)
 {
-    const char *msgid;
-    PyObject *object;
-    PyObject *string;
-
-    if (!PyArg_ParseTuple(args, "O:set_msgid", &object))
-        return NULL;
-
-    if (object == Py_None) {
-        po_message_set_msgid(self->msg, NULL);
-    } else {
-        string = get_pystring_from_pyobject(object);
-
-        if (string == NULL)
-            /* Got an exception */
-            return NULL;
-        else {
-            msgid = PyString_AsString(string);
-            po_message_set_msgid(self->msg, msgid);
-            Py_DECREF(string);
-        }
-    }
-
-    Py_RETURN_NONE;
+    return _message_set_field(self, args, "O:set_msgid",
+                                    &po_message_set_msgid);
 }
 
 PyDoc_STRVAR(doc_pypo_message_set_msgid_plural,
@@ -571,29 +597,8 @@
 static PyObject *
 pypo_message_set_msgid_plural(PyPoMessage *self, PyObject *args)
 {
-    const char *msgid;
-    PyObject *object;
-    PyObject *string;
-
-    if (!PyArg_ParseTuple(args, "O:set_msgid_plural", &object))
-        return NULL;
-
-    if (object == Py_None) {
-        po_message_set_msgid_plural(self->msg, NULL);
-    } else {
-        string = get_pystring_from_pyobject(object);
-
-        if (string == NULL)
-            /* Got an exception */
-            return NULL;
-        else {
-            msgid = PyString_AsString(string);
-            po_message_set_msgid_plural(self->msg, msgid);
-            Py_DECREF(string);
-        }
-    }
-
-    Py_RETURN_NONE;
+    return _message_set_field(self, args, "O:set_msgid_plural",
+                                    &po_message_set_msgid_plural);
 }
 
 PyDoc_STRVAR(doc_pypo_message_set_msgstr,
@@ -602,29 +607,8 @@
 static PyObject *
 pypo_message_set_msgstr(PyPoMessage *self, PyObject *args)
 {
-    const char *msgstr;
-    PyObject *object;
-    PyObject *string;
-
-    if (!PyArg_ParseTuple(args, "O:set_msgstr", &object))
-        return NULL;
-
-    if (object == Py_None) {
-        po_message_set_msgstr(self->msg, NULL);
-    } else {
-        string = get_pystring_from_pyobject(object);
-
-        if (string == NULL)
-            /* Got an exception */
-            return NULL;
-        else {
-            msgstr = PyString_AsString(string);
-            po_message_set_msgstr(self->msg, msgstr);
-            Py_DECREF(string);
-        }
-    }
-
-    Py_RETURN_NONE;
+    return _message_set_field(self, args, "O:set_msgstr",
+                                    &po_message_set_msgstr);
 }
 
 PyDoc_STRVAR(doc_pypo_message_set_msgstr_plural,
@@ -675,29 +659,8 @@
 static PyObject *
 pypo_message_set_comments(PyPoMessage *self, PyObject *args)
 {
-    const char *comments;
-    PyObject *object;
-    PyObject *string;
-
-    if (!PyArg_ParseTuple(args, "O:set_comments", &object))
-        return NULL;
-
-    if (object == Py_None) {
-        po_message_set_comments(self->msg, NULL);
-    } else {
-        string = get_pystring_from_pyobject(object);
-
-        if (string == NULL)
-            /* Got an exception */
-            return NULL;
-        else {
-            comments = PyString_AsString(string);
-            po_message_set_comments(self->msg, comments);
-            Py_DECREF(string);
-        }
-    }
-
-    Py_RETURN_NONE;
+    return _message_set_field(self, args, "O:set_comments",
+                                    &po_message_set_comments);
 }
 
 PyDoc_STRVAR(doc_pypo_message_set_format,
@@ -779,6 +742,11 @@
 }
 
 static PyMethodDef pypo_message_methods[] = {
+/* msgctxt support was added in 0.15. */
+#if LIBGETTEXTPO_VERSION >= MIN_MSGCTXT_GETTEXTPO_VERSION
+    { "set_msgctxt", (PyCFunction)pypo_message_set_msgctxt, METH_VARARGS,
+      doc_pypo_message_set_msgctxt },
+#endif
     { "set_msgid", (PyCFunction)pypo_message_set_msgid, METH_VARARGS,
       doc_pypo_message_set_msgid },
     { "set_msgid_plural", (PyCFunction)pypo_message_set_msgid_plural, METH_VARARGS,
@@ -796,6 +764,23 @@
     { NULL, 0, 0 }
 };
 
+/* msgctxt support was added in 0.15. */
+#if LIBGETTEXTPO_VERSION >= MIN_MSGCTXT_GETTEXTPO_VERSION
+PyDoc_STRVAR(doc_pypo_message_msgctxt,
+"M.msgctxt -> the msgctxt for this PoMessage.");
+
+static PyObject *
+pypo_message_get_msgctxt(PyPoMessage *self, void *closure)
+{
+    const char *msgctxt;
+
+    msgctxt = po_message_msgctxt(self->msg);
+    if (msgctxt)
+	return PyString_FromString(msgctxt);
+    Py_RETURN_NONE;
+}
+#endif
+
 PyDoc_STRVAR(doc_pypo_message_msgid,
 "M.msgid -> the msgid for this PoMessage.");
 
@@ -878,6 +863,11 @@
 
 
 static PyGetSetDef pypo_message_getsets[] = {
+/* msgctxt support was added in 0.15. */
+#if LIBGETTEXTPO_VERSION >= MIN_MSGCTXT_GETTEXTPO_VERSION
+    { "msgctxt", (getter)pypo_message_get_msgctxt,             (setter)0,
+      doc_pypo_message_msgctxt },
+#endif
     { "msgid", (getter)pypo_message_get_msgid,                 (setter)0,
       doc_pypo_message_msgid },
     { "msgid_plural", (getter)pypo_message_get_msgid_plural,   (setter)0,

=== modified file 'test_gettextpo.py'
--- test_gettextpo.py	2009-06-02 15:31:25 +0000
+++ test_gettextpo.py	2012-02-13 10:08:18 +0000
@@ -4,11 +4,14 @@
 import unittest
 import gettextpo
 
+
 class PoFileTestCase(unittest.TestCase):
+
     def testCreateEmpty(self):
         # Test that we can create an empty pofile object
         pofile = gettextpo.PoFile()
         self.assertEquals(list(iter(pofile)), [])
+
     def testAddMessage(self):
         # Test that we can add messages to a new pofile object
         pofile = gettextpo.PoFile()
@@ -18,6 +21,7 @@
         poiter.insert(msg)
 
         self.assertEquals(list(iter(pofile)), [msg])
+
     def testAddMessageTwice(self):
         # A message object can only be added to one pofile object
         pofile1 = gettextpo.PoFile()
@@ -31,7 +35,9 @@
         poiter = iter(pofile2)
         self.assertRaises(ValueError, poiter.insert, msg)
 
+
 class PoMessageTestCase(unittest.TestCase):
+
     def testCreateMessage(self):
         # Test that messages can be created.
         msg = gettextpo.PoMessage()
@@ -43,11 +49,16 @@
         msg.set_msgid_plural('Hellos')
         self.assertEquals(msg.msgid_plural, 'Hellos')
 
+    def testSetMsgCtxt(self):
+        msg = gettextpo.PoMessage()
+        msg.set_msgctxt('Hello')
+        self.assertEquals(msg.msgctxt, 'Hello')
+
     def testSetMsgStr(self):
         msg = gettextpo.PoMessage()
         msg.set_msgstr('Hello World')
         self.assertEquals(msg.msgstr, 'Hello World')
-    
+
     def testSetMsgStrPlural(self):
         # Test handling of plural msgstrs.  The PoMessage object can
         # not hold plural msgstrs if the msgid does not have a plural.
@@ -63,7 +74,9 @@
         msg.set_msgstr_plural(2, 'Two')
         self.assertEquals(msg.msgstr_plural, ['Zero', 'One', 'Two'])
 
+
 class CheckFormatTestCase(unittest.TestCase):
+
     def testGoodFormat(self):
         # Check that no exception is raised on a good translation.