polib (version , )
index
/mnt/izi/projects/polib/polib.py

a library to manipulate gettext catalogs files (.po/.mo files).
 
polib allows you to manipulate, create, modify gettext .pot/.po/.mo
files.  You can load existing files, iterate through it's entries, add,
modify entries, comments or metadata, etc... or create new po/pot files
from scratch.
 
polib provides a simple and pythonic API, exporting only two
convenience functions 'pofile' and 'mofile', and the 4 core classes:
POFileMOFilePOEntry and MOEntry for creating new files/entries.
 
Basic example:
--------------
>>> import polib
>>> # load an existing po file
>>> po = polib.pofile('tests/test.po')
>>> for entry in po:
...     # do something with entry...
...     pass
>>> # add an entry
>>> entry = polib.POEntry()
>>> entry.msgid  = 'Welcome'
>>> entry.msgstr = 'Bienvenue'
>>> entry.occurences  = [('welcome.py', '12'), ('anotherfile.py', '34')]
>>> po.append(entry)
>>> # save our modified po file
>>> po.save()
 
Some references:
----------------
* Gettext manual: http://www.gnu.org/software/gettext/manual/
* PO file format:
  http://www.gnu.org/software/gettext/manual/html_node/gettext_9.html
* MO file format:
  http://www.gnu.org/software/gettext/manual/html_node/gettext_136.html

 
Modules
       
struct
textwrap

 
Classes
       
_BaseEntry
MOEntry
POEntry
_BaseFile(__builtin__.list)
MOFile
POFile

 
class MOEntry(_BaseEntry)
    Represents a mo file entry.
 
Methods:
--------
__str__() -> string
    Return the string representation of the object.
 
__repr__() -> string
    Return the "official" string reprsentation of the object.
 
Properties:
-----------
msgid         -- a string containing the msgid to translate
msgid_plural  -- a string containing the plural msgid
msgstr        -- a string containing the msgid translation.
msgstr_plural -- a dictionary containing the translations for
                 0,1,...,n occurences (eg. msgstr[0], msgstr[1],
                 etc...)
 
An example:
-----------
>>> entry = MOEntry()
>>> entry.msgid  = 'translate me !'
>>> entry.msgstr = 'traduisez moi !'
>>> print entry
msgid "translate me !"
msgstr "traduisez moi !"
<BLANKLINE>
 
  Methods defined here:
__init__(self, msgid='', msgstr='')
MOEntry constructor.
__str__(self, wrapwidth=78)
Return the string representation of the entry.

Methods inherited from _BaseEntry:
__repr__(self)
Return the official string representation of the object.

 
class MOFile(_BaseFile)
    Mo file reader/writer.
MOFile objects inherit the list objects methods.
 
 
Methods:
--------
__str__() -> string
    Return the string representation of the object (as the po
    format).
 
__repr__() -> string
    Return the "official" string reprsentation of the object.
 
to_binary()
    Return the binary (mo format) representation of the file.
 
save([fpath])
    Save the binary representation to file <fpath>.
    If fpath is ommited and the object points to an existing file,
    the existing file is updated.
 
save_as_pofile(fpath)
    Save the string representation of the file (po format) to file
    <fpath>.
 
ordered_metadata() -> list
    Convenience method that return the metadata ordered. The return
    value is list of tuples (metadata name, metadata_value).
 
charset() -> string
    Return the file encoding charset.
    If the charset cannot be found in metadata, the function
    returns None.
 
Properties:
-----------
magic_number -- the magic number of the binary file
version      -- the version number of the binary file format
                (always 0)
metadata     -- a dict containing the mo file metadata
wrapwidth    -- an integer, the width at which lines should be
                wrapped
 
An example:
-----------
>>> mo = MOFile()
>>> entry1 = POEntry("Some english text", "Un texte en anglais")
>>> entry2 = POEntry("I need my dirty cheese", "Je veux mon sale fromage")
>>> mo.append(entry1)
>>> mo.append(entry2)
>>> print mo
msgid ""
msgstr ""
<BLANKLINE>
msgid "Some english text"
msgstr "Un texte en anglais"
<BLANKLINE>
msgid "I need my dirty cheese"
msgstr "Je veux mon sale fromage"
 
 
Method resolution order:
MOFile
_BaseFile
__builtin__.list
__builtin__.object

Methods defined here:
__init__(self, fhandle=None, wrapwidth=78)
MOFile constructor.
save(self, fpath)
Save the string representation of the file (po format) to file <fpath>.
save_as_pofile(self, fpath)
Save the string representation of the file (po format) to file <fpath>.

Methods inherited from _BaseFile:
__del__(self)
Destructor: close the file handle when the object is
destroyed.
__repr__(self)
Return the official string representation of the object.
__str__(self)
Common string representation for po and mo files.
charset(self)
Return the file encoding charset.
If the charset cannot be found in metadata, the function
returns None.
ordered_metadata(self)
Convenience method that return the metadata ordered. The return
value is list of tuples (metadata name, metadata_value).
to_binary(self)
Return the mofile binary representation.

Data and other attributes inherited from _BaseFile:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of '_BaseFile' objects>
list of weak references to the object (if defined)

Methods inherited from __builtin__.list:
__add__(...)
x.__add__(y) <==> x+y
__contains__(...)
x.__contains__(y) <==> y in x
__delitem__(...)
x.__delitem__(y) <==> del x[y]
__delslice__(...)
x.__delslice__(i, j) <==> del x[i:j]
 
Use of negative indices is not supported.
__eq__(...)
x.__eq__(y) <==> x==y
__ge__(...)
x.__ge__(y) <==> x>=y
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__gt__(...)
x.__gt__(y) <==> x>y
__hash__(...)
x.__hash__() <==> hash(x)
__iadd__(...)
x.__iadd__(y) <==> x+=y
__imul__(...)
x.__imul__(y) <==> x*=y
__iter__(...)
x.__iter__() <==> iter(x)
__le__(...)
x.__le__(y) <==> x<=y
__len__(...)
x.__len__() <==> len(x)
__lt__(...)
x.__lt__(y) <==> x<y
__mul__(...)
x.__mul__(n) <==> x*n
__ne__(...)
x.__ne__(y) <==> x!=y
__reversed__(...)
L.__reversed__() -- return a reverse iterator over the list
__rmul__(...)
x.__rmul__(n) <==> n*x
__setitem__(...)
x.__setitem__(i, y) <==> x[i]=y
__setslice__(...)
x.__setslice__(i, j, y) <==> x[i:j]=y
 
Use  of negative indices is not supported.
append(...)
L.append(object) -- append object to end
count(...)
L.count(value) -> integer -- return number of occurrences of value
extend(...)
L.extend(iterable) -- extend list by appending elements from the iterable
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value
insert(...)
L.insert(index, object) -- insert object before index
pop(...)
L.pop([index]) -> item -- remove and return item at index (default last)
remove(...)
L.remove(value) -- remove first occurrence of value
reverse(...)
L.reverse() -- reverse *IN PLACE*
sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1

Data and other attributes inherited from __builtin__.list:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
class POEntry(_BaseEntry)
    Represents a po file entry.
 
Methods:
--------
__str__() -> string
    Return the string representation of the object.
 
__repr__() -> string
    Return the "official" string reprsentation of the object.
 
translated([fpath]) -> boolean
    Return True if the entry has been translated or False otherwise.
 
Properties:
-----------
msgid         -- a string containing the msgid to translate
msgid_plural  -- a string containing the plural msgid
msgstr        -- a string containing the msgid translation.
msgstr_plural -- a dictionary containing the translations for
                 0,1,...,n occurences (eg. msgstr[0], msgstr[1],
                 etc...)
occurences    -- a list containing tuples of file name, line number
                 representing occurences of the msgid in the
                 source code.
comment       -- a string containing a generated comment
                 (eg. #. some text)
tcomment      -- a string containing a translator comment
                 (eg. # some text)
flags         -- a list of strings reprensenting the entry flags
                 (eg: fuzzy, c-format...)
 
Examples:
---------
>>> entry = POEntry(
...     msgid='Welcome', 
...     msgstr='Bienvenue',
... )
>>> entry.occurences = [('welcome.py', 12), ('anotherfile.py', 34)]
>>> print entry
#: welcome.py:12 anotherfile.py:34
msgid "Welcome"
msgstr "Bienvenue"
<BLANKLINE>
 
>>> entry = POEntry()
>>> entry.occurences = [('src/spam.c', 32), ('src/eggs.c', 45)]
>>> entry.tcomment = 'A plural translation'
>>> entry.flags.append('c-format')
>>> entry.msgid = 'I have spam but no egg !'
>>> entry.msgid_plural = 'I have spam and %d eggs !'
>>> entry.msgstr_plural[0] = "J'ai du jambon mais aucun oeuf !"
>>> entry.msgstr_plural[1] = "J'ai du jambon et %d oeufs !"
>>> print entry
# A plural translation
#: src/spam.c:32 src/eggs.c:45
#, c-format
msgid "I have spam but no egg !"
msgid_plural "I have spam and %d eggs !"
msgstr[0] "J'ai du jambon mais aucun oeuf !"
msgstr[1] "J'ai du jambon et %d oeufs !"
<BLANKLINE>
 
  Methods defined here:
__init__(self, msgid='', msgstr='')
POEntry constructor.
__str__(self, wrapwidth=78)
Return the string representation of the entry.
translated(self)
Return True if the entry has been translated or False

Methods inherited from _BaseEntry:
__repr__(self)
Return the official string representation of the object.

 
class POFile(_BaseFile)
    Po (or Pot) file reader/writer.
POFile objects inherit the list objects methods.
 
Methods:
--------
__str__() -> string
    Return the string representation of the object.
 
__repr__() -> string
    Return the "official" string reprsentation of the object.
 
to_binary()
    Return the binary (mo format) representation of the file.
 
save([fpath])
    Save the string representation to file <fpath>.
    If fpath is ommited and the object points to an existing file,
    the existing file is updated.
 
save_as_mofile(fpath)
    Save the binary representation of the file (mo format) to file
    <fpath>.
 
percent_translated() -> integer
    Convenience method that return an integer that correspond to
    the percentage of translated messages.
 
translated_entries() -> list
    Convenience method that return the list of translated entries.
 
untranslated_entries() -> list
    Convenience method that return the list of untranslated
    entries.
 
fuzzy_entries() -> list
    Convenience method that return the list of 'fuzzy' entries.
 
obsolete_entries() -> list
    Convenience method that return the list of obsolete entries.
 
ordered_metadata() -> list
    Convenience method that return the metadata ordered. The return
    value is list of tuples (metadata name, metadata_value).
 
charset() -> string
    Return the file encoding charset.
    If the charset cannot be found in metadata, the function
    returns None.
 
Properties:
-----------
header    -- a string containing the po file header
metadata  -- a dict containing the po file metadata
wrapwidth -- an integer, the width at which lines should be wrapped
 
An example:
-----------
>>> po = POFile()
>>> entry1 = POEntry("Some english text", "Un texte en anglais")
>>> entry2 = POEntry("I need my dirty cheese", "Je veux mon sale fromage")
>>> po.append(entry1)
>>> po.append(entry2)
>>> po.header = "Some Header"
>>> print po
# Some Header
msgid ""
msgstr ""
<BLANKLINE>
msgid "Some english text"
msgstr "Un texte en anglais"
<BLANKLINE>
msgid "I need my dirty cheese"
msgstr "Je veux mon sale fromage"
 
 
Method resolution order:
POFile
_BaseFile
__builtin__.list
__builtin__.object

Methods defined here:
__init__(self, fhandle=None, wrapwidth=78)
POFile constructor.
__str__(self)
Return the string representation of the po file
fuzzy_entries(self)
Convenience method that return the list of 'fuzzy' entries.
obsolete_entries(self)
Convenience method that return the list of obsolete entries.
percent_translated(self)
Convenience method that return the percentage of translated
messages.
save_as_mofile(self, fpath)
Save the binary representation of the file (mo format) to file
<fpath>.
translated_entries(self)
Convenience method that return a list of translated entries.
untranslated_entries(self)
Convenience method that return a list of untranslated entries.

Methods inherited from _BaseFile:
__del__(self)
Destructor: close the file handle when the object is
destroyed.
__repr__(self)
Return the official string representation of the object.
charset(self)
Return the file encoding charset.
If the charset cannot be found in metadata, the function
returns None.
ordered_metadata(self)
Convenience method that return the metadata ordered. The return
value is list of tuples (metadata name, metadata_value).
save(self, fpath=None, repr_method='__str__')
Save the po file to file <fpath> if no file handle exists for
the object. If there's already an open file and no fpath is
provided, then the existing file is rewritten with the modified
data.
to_binary(self)
Return the mofile binary representation.

Data and other attributes inherited from _BaseFile:
__dict__ = <dictproxy object>
dictionary for instance variables (if defined)
__weakref__ = <attribute '__weakref__' of '_BaseFile' objects>
list of weak references to the object (if defined)

Methods inherited from __builtin__.list:
__add__(...)
x.__add__(y) <==> x+y
__contains__(...)
x.__contains__(y) <==> y in x
__delitem__(...)
x.__delitem__(y) <==> del x[y]
__delslice__(...)
x.__delslice__(i, j) <==> del x[i:j]
 
Use of negative indices is not supported.
__eq__(...)
x.__eq__(y) <==> x==y
__ge__(...)
x.__ge__(y) <==> x>=y
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__gt__(...)
x.__gt__(y) <==> x>y
__hash__(...)
x.__hash__() <==> hash(x)
__iadd__(...)
x.__iadd__(y) <==> x+=y
__imul__(...)
x.__imul__(y) <==> x*=y
__iter__(...)
x.__iter__() <==> iter(x)
__le__(...)
x.__le__(y) <==> x<=y
__len__(...)
x.__len__() <==> len(x)
__lt__(...)
x.__lt__(y) <==> x<y
__mul__(...)
x.__mul__(n) <==> x*n
__ne__(...)
x.__ne__(y) <==> x!=y
__reversed__(...)
L.__reversed__() -- return a reverse iterator over the list
__rmul__(...)
x.__rmul__(n) <==> n*x
__setitem__(...)
x.__setitem__(i, y) <==> x[i]=y
__setslice__(...)
x.__setslice__(i, j, y) <==> x[i:j]=y
 
Use  of negative indices is not supported.
append(...)
L.append(object) -- append object to end
count(...)
L.count(value) -> integer -- return number of occurrences of value
extend(...)
L.extend(iterable) -- extend list by appending elements from the iterable
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value
insert(...)
L.insert(index, object) -- insert object before index
pop(...)
L.pop([index]) -> item -- remove and return item at index (default last)
remove(...)
L.remove(value) -- remove first occurrence of value
reverse(...)
L.reverse() -- reverse *IN PLACE*
sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1

Data and other attributes inherited from __builtin__.list:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

 
Functions
       
mofile(fpath, wrapwidth=78)
Convenience function that parse the mo file <fpath> and return
MOFile instance.
 
Keyword arguments:
------------------
fpath     -- full or relative path to the mo file to parse
wrapwidth -- integer, the wrap width, only useful when -w option
             was passed to xgettext to generate the po file that
             was used to format the mo file, default to 78
             (optional).
 
Example:
--------
>>> import polib
>>> mo = polib.mofile('tests/test.mo')
>>> mo #doctest: +ELLIPSIS
<MOFile instance at ...>
pofile(fpath, wrapwidth=78)
Convenience function that parse the po/pot file <fpath> and return
POFile instance.
 
Keyword argument:
-----------------
fpath     -- full or relative path to the po/pot file to parse
wrapwidth -- integer, the wrap width, only useful when -w option
             was passed to xgettext, default to 78 (optional)
 
Example:
--------
>>> import polib
>>> po = polib.pofile('tests/test.po')
>>> po #doctest: +ELLIPSIS
<POFile instance at ...>

 
Data
        __all__ = ['pofile', 'POFile', 'POEntry', 'mofile', 'MOFile', 'MOEntry']
__author__ = 'David JEAN LOUIS <izimobil@gmail.com>'
__date__ = ''
__version__ = ''

 
Author
        David JEAN LOUIS <izimobil@gmail.com>