Index: translate/storage/test_cpo.py =================================================================== --- translate/storage/test_cpo.py (revision 8228) +++ translate/storage/test_cpo.py (working copy) @@ -72,6 +72,16 @@ unit.addnote("# Double commented comment") assert unit.getnotes() == "# Double commented comment" + def test_typecomment(self): + unit = self.UnitClass("Comment") + assert unit.gettypecomments() == [] + + unit.addtypecomment("c-format") + assert unit.hastypecomment("c-format") + assert unit.gettypecomments() == ["c-format"] + + assert raises(ValueError, unit.addtypecomment, "invalid-format") + class TestCPOFile(test_po.TestPOFile): StoreClass = cpo.pofile def test_msgidcomments(self): Index: translate/storage/cpo.py =================================================================== --- translate/storage/cpo.py (revision 8228) +++ translate/storage/cpo.py (working copy) @@ -134,6 +134,8 @@ gpo.po_message_msgid_plural.restype = STRING gpo.po_message_msgstr.restype = STRING gpo.po_message_msgstr_plural.restype = STRING +gpo.po_message_set_format.argtypes = [c_int, STRING, c_int] +gpo.po_format_list.restype = POINTER(STRING) # Message (set methods) gpo.po_message_set_comments.argtypes = [c_int, STRING] @@ -422,6 +424,40 @@ def hastypecomment(self, typecomment): return gpo.po_message_is_format(self._gpo_message, typecomment) + def _get_available_typecomments(self): + """Returns a list of all the type comments available""" + formats = gpo.po_format_list() + typecomments = [] + for format in formats: + if format is None: + break + else: + typecomments.append(format) + return typecomments + + def gettypecomments(self): + """Returns a list of the unit type comments + + @returns: a list of the typecomments or an empty list if the unit not + have type comments. + """ + comments = [] + for typecomment in self._get_available_typecomments(): + if self.hastypecomment(typecomment): + comments.append(typecomment) + return comments + + def addtypecomment(self, typecomment): + """Adds a type comment to the unit + + @param typecomment: the type comment to be added, for example + "c-format" + """ + if typecomment not in self._get_available_typecomments(): + raise ValueError("Invalid typecomment: %s" % typecomment) + + gpo.po_message_set_format(self._gpo_message, typecomment, 1) + def hasmarkedcomment(self, commentmarker): commentmarker = "(%s)" % commentmarker for comment in self.getnotes("translator").split("\n"):