{"parents": [{"link": "../", "title": "Module code"}], "title": "polib", "body": "<h1>Source code for polib</h1><div class=\"highlight\"><pre>\n<span></span><span class=\"c1\"># -* coding: utf-8 -*-</span>\n<span class=\"c1\">#</span>\n<span class=\"c1\"># License: MIT (see LICENSE file provided)</span>\n<span class=\"c1\"># vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:</span>\n\n<span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">**polib** allows you to manipulate, create, modify gettext files (pot, po and</span>\n<span class=\"sd\">mo files).  You can load existing files, iterate through it&#39;s entries, add,</span>\n<span class=\"sd\">modify entries, comments or metadata, etc. or create new po files from scratch.</span>\n\n<span class=\"sd\">**polib** provides a simple and pythonic API via the :func:`~polib.pofile` and</span>\n<span class=\"sd\">:func:`~polib.mofile` convenience functions.</span>\n<span class=\"sd\">&quot;&quot;&quot;</span>\n\n<span class=\"kn\">import</span> <span class=\"nn\">array</span>\n<span class=\"kn\">import</span> <span class=\"nn\">codecs</span>\n<span class=\"kn\">import</span> <span class=\"nn\">os</span>\n<span class=\"kn\">import</span> <span class=\"nn\">re</span>\n<span class=\"kn\">import</span> <span class=\"nn\">struct</span>\n<span class=\"kn\">import</span> <span class=\"nn\">sys</span>\n<span class=\"kn\">import</span> <span class=\"nn\">textwrap</span>\n<span class=\"kn\">import</span> <span class=\"nn\">io</span>\n\n\n<span class=\"n\">__author__</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;David Jean Louis &lt;izimobil@gmail.com&gt;&#39;</span>\n<span class=\"n\">__version__</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;1.2.0&#39;</span>\n<span class=\"n\">__all__</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;pofile&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;POFile&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;POEntry&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mofile&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;MOFile&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;MOEntry&#39;</span><span class=\"p\">,</span>\n           <span class=\"s1\">&#39;default_encoding&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;escape&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;unescape&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;detect_encoding&#39;</span><span class=\"p\">,</span> <span class=\"p\">]</span>\n\n\n<span class=\"c1\"># the default encoding to use when encoding cannot be detected</span>\n<span class=\"n\">default_encoding</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;utf-8&#39;</span>\n\n<span class=\"c1\"># python 2/3 compatibility helpers {{{</span>\n\n\n<span class=\"k\">if</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">version_info</span> <span class=\"o\">&lt;</span> <span class=\"p\">(</span><span class=\"mi\">3</span><span class=\"p\">,):</span>\n    <span class=\"n\">PY3</span> <span class=\"o\">=</span> <span class=\"kc\">False</span>\n    <span class=\"n\">text_type</span> <span class=\"o\">=</span> <span class=\"n\">unicode</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">b</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"n\">s</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">u</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"n\">unicode</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">,</span> <span class=\"s2\">&quot;unicode_escape&quot;</span><span class=\"p\">)</span>\n\n<span class=\"k\">else</span><span class=\"p\">:</span>\n    <span class=\"n\">PY3</span> <span class=\"o\">=</span> <span class=\"kc\">True</span>\n    <span class=\"n\">text_type</span> <span class=\"o\">=</span> <span class=\"nb\">str</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">b</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"n\">s</span><span class=\"o\">.</span><span class=\"n\">encode</span><span class=\"p\">(</span><span class=\"s2\">&quot;latin-1&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">u</span><span class=\"p\">(</span><span class=\"n\">s</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"n\">s</span>\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># _pofile_or_mofile {{{</span>\n\n\n<span class=\"k\">def</span> <span class=\"nf\">_pofile_or_mofile</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">,</span> <span class=\"nb\">type</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Internal function used by :func:`polib.pofile` and :func:`polib.mofile` to</span>\n<span class=\"sd\">    honor the DRY concept.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"c1\"># get the file encoding</span>\n    <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;encoding&#39;</span><span class=\"p\">)</span>\n    <span class=\"k\">if</span> <span class=\"n\">enc</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n        <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">detect_encoding</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">,</span> <span class=\"nb\">type</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;mofile&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"c1\"># parse the file</span>\n    <span class=\"n\">kls</span> <span class=\"o\">=</span> <span class=\"nb\">type</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;pofile&#39;</span> <span class=\"ow\">and</span> <span class=\"n\">_POFileParser</span> <span class=\"ow\">or</span> <span class=\"n\">_MOFileParser</span>\n    <span class=\"n\">parser</span> <span class=\"o\">=</span> <span class=\"n\">kls</span><span class=\"p\">(</span>\n        <span class=\"n\">f</span><span class=\"p\">,</span>\n        <span class=\"n\">encoding</span><span class=\"o\">=</span><span class=\"n\">enc</span><span class=\"p\">,</span>\n        <span class=\"n\">check_for_duplicates</span><span class=\"o\">=</span><span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;check_for_duplicates&#39;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">),</span>\n        <span class=\"n\">klass</span><span class=\"o\">=</span><span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;klass&#39;</span><span class=\"p\">)</span>\n    <span class=\"p\">)</span>\n    <span class=\"n\">instance</span> <span class=\"o\">=</span> <span class=\"n\">parser</span><span class=\"o\">.</span><span class=\"n\">parse</span><span class=\"p\">()</span>\n    <span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">wrapwidth</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;wrapwidth&#39;</span><span class=\"p\">,</span> <span class=\"mi\">78</span><span class=\"p\">)</span>\n    <span class=\"k\">return</span> <span class=\"n\">instance</span>\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># _is_file {{{</span>\n\n\n<span class=\"k\">def</span> <span class=\"nf\">_is_file</span><span class=\"p\">(</span><span class=\"n\">filename_or_contents</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Safely returns the value of os.path.exists(filename_or_contents).</span>\n\n<span class=\"sd\">    Arguments:</span>\n\n<span class=\"sd\">    ``filename_or_contents``</span>\n<span class=\"sd\">        either a filename, or a string holding the contents of some file.</span>\n<span class=\"sd\">        In the latter case, this function will always return False.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">try</span><span class=\"p\">:</span>\n        <span class=\"k\">return</span> <span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">path</span><span class=\"o\">.</span><span class=\"n\">isfile</span><span class=\"p\">(</span><span class=\"n\">filename_or_contents</span><span class=\"p\">)</span>\n    <span class=\"k\">except</span> <span class=\"p\">(</span><span class=\"ne\">TypeError</span><span class=\"p\">,</span> <span class=\"ne\">ValueError</span><span class=\"p\">,</span> <span class=\"ne\">UnicodeEncodeError</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"kc\">False</span>\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># function pofile() {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"pofile\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.pofile\">[docs]</a>\n<span class=\"k\">def</span> <span class=\"nf\">pofile</span><span class=\"p\">(</span><span class=\"n\">pofile</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Convenience function that parses the po or pot file ``pofile`` and returns</span>\n<span class=\"sd\">    a :class:`~polib.POFile` instance.</span>\n\n<span class=\"sd\">    Arguments:</span>\n\n<span class=\"sd\">    ``pofile``</span>\n<span class=\"sd\">        string, full or relative path to the po/pot file or its content (data).</span>\n\n<span class=\"sd\">    ``wrapwidth``</span>\n<span class=\"sd\">        integer, the wrap width, only useful when the ``-w`` option was passed</span>\n<span class=\"sd\">        to xgettext (optional, default: ``78``).</span>\n\n<span class=\"sd\">    ``encoding``</span>\n<span class=\"sd\">        string, the encoding to use (e.g. &quot;utf-8&quot;) (default: ``None``, the</span>\n<span class=\"sd\">        encoding will be auto-detected).</span>\n\n<span class=\"sd\">    ``check_for_duplicates``</span>\n<span class=\"sd\">        whether to check for duplicate entries when adding entries to the</span>\n<span class=\"sd\">        file (optional, default: ``False``).</span>\n\n<span class=\"sd\">    ``klass``</span>\n<span class=\"sd\">        class which is used to instantiate the return value (optional,</span>\n<span class=\"sd\">        default: ``None``, the return value with be a :class:`~polib.POFile`</span>\n<span class=\"sd\">        instance).</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">return</span> <span class=\"n\">_pofile_or_mofile</span><span class=\"p\">(</span><span class=\"n\">pofile</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pofile&#39;</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span></div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># function mofile() {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"mofile\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.mofile\">[docs]</a>\n<span class=\"k\">def</span> <span class=\"nf\">mofile</span><span class=\"p\">(</span><span class=\"n\">mofile</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Convenience function that parses the mo file ``mofile`` and returns a</span>\n<span class=\"sd\">    :class:`~polib.MOFile` instance.</span>\n\n<span class=\"sd\">    Arguments:</span>\n\n<span class=\"sd\">    ``mofile``</span>\n<span class=\"sd\">        string, full or relative path to the mo file or its content (string</span>\n<span class=\"sd\">        or bytes).</span>\n\n<span class=\"sd\">    ``wrapwidth``</span>\n<span class=\"sd\">        integer, the wrap width, only useful when the ``-w`` option was passed</span>\n<span class=\"sd\">        to xgettext to generate the po file that was used to format the mo file</span>\n<span class=\"sd\">        (optional, default: ``78``).</span>\n\n<span class=\"sd\">    ``encoding``</span>\n<span class=\"sd\">        string, the encoding to use (e.g. &quot;utf-8&quot;) (default: ``None``, the</span>\n<span class=\"sd\">        encoding will be auto-detected).</span>\n\n<span class=\"sd\">    ``check_for_duplicates``</span>\n<span class=\"sd\">        whether to check for duplicate entries when adding entries to the</span>\n<span class=\"sd\">        file (optional, default: ``False``).</span>\n\n<span class=\"sd\">    ``klass``</span>\n<span class=\"sd\">        class which is used to instantiate the return value (optional,</span>\n<span class=\"sd\">        default: ``None``, the return value with be a :class:`~polib.POFile`</span>\n<span class=\"sd\">        instance).</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">return</span> <span class=\"n\">_pofile_or_mofile</span><span class=\"p\">(</span><span class=\"n\">mofile</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mofile&#39;</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span></div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># function detect_encoding() {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"detect_encoding\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.detect_encoding\">[docs]</a>\n<span class=\"k\">def</span> <span class=\"nf\">detect_encoding</span><span class=\"p\">(</span><span class=\"n\">file</span><span class=\"p\">,</span> <span class=\"n\">binary_mode</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Try to detect the encoding used by the ``file``. The ``file`` argument can</span>\n<span class=\"sd\">    be a PO or MO file path or a string containing the contents of the file.</span>\n<span class=\"sd\">    If the encoding cannot be detected, the function will return the value of</span>\n<span class=\"sd\">    ``default_encoding``.</span>\n\n<span class=\"sd\">    Arguments:</span>\n\n<span class=\"sd\">    ``file``</span>\n<span class=\"sd\">        string, full or relative path to the po/mo file or its content.</span>\n\n<span class=\"sd\">    ``binary_mode``</span>\n<span class=\"sd\">        boolean, set this to True if ``file`` is a mo file.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"n\">PATTERN</span> <span class=\"o\">=</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;&quot;?Content-Type:.+? charset=([\\w_\\-:\\.]+)&#39;</span>\n    <span class=\"n\">rxt</span> <span class=\"o\">=</span> <span class=\"n\">re</span><span class=\"o\">.</span><span class=\"n\">compile</span><span class=\"p\">(</span><span class=\"n\">u</span><span class=\"p\">(</span><span class=\"n\">PATTERN</span><span class=\"p\">))</span>\n    <span class=\"n\">rxb</span> <span class=\"o\">=</span> <span class=\"n\">re</span><span class=\"o\">.</span><span class=\"n\">compile</span><span class=\"p\">(</span><span class=\"n\">b</span><span class=\"p\">(</span><span class=\"n\">PATTERN</span><span class=\"p\">))</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">charset_exists</span><span class=\"p\">(</span><span class=\"n\">charset</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Check whether ``charset`` is valid or not.&quot;&quot;&quot;</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            <span class=\"n\">codecs</span><span class=\"o\">.</span><span class=\"n\">lookup</span><span class=\"p\">(</span><span class=\"n\">charset</span><span class=\"p\">)</span>\n        <span class=\"k\">except</span> <span class=\"ne\">LookupError</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"kc\">False</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">_is_file</span><span class=\"p\">(</span><span class=\"n\">file</span><span class=\"p\">):</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            <span class=\"n\">match</span> <span class=\"o\">=</span> <span class=\"n\">rxt</span><span class=\"o\">.</span><span class=\"n\">search</span><span class=\"p\">(</span><span class=\"n\">file</span><span class=\"p\">)</span>\n        <span class=\"k\">except</span> <span class=\"ne\">TypeError</span><span class=\"p\">:</span>\n            <span class=\"n\">match</span> <span class=\"o\">=</span> <span class=\"n\">rxb</span><span class=\"o\">.</span><span class=\"n\">search</span><span class=\"p\">(</span><span class=\"n\">file</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">match</span><span class=\"p\">:</span>\n            <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">match</span><span class=\"o\">.</span><span class=\"n\">group</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n            <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">enc</span><span class=\"p\">,</span> <span class=\"n\">text_type</span><span class=\"p\">):</span>\n                <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">enc</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"s1\">&#39;utf-8&#39;</span><span class=\"p\">)</span>\n            <span class=\"k\">if</span> <span class=\"n\">charset_exists</span><span class=\"p\">(</span><span class=\"n\">enc</span><span class=\"p\">):</span>\n                <span class=\"k\">return</span> <span class=\"n\">enc</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"c1\"># For PY3, always treat as binary</span>\n        <span class=\"k\">if</span> <span class=\"n\">binary_mode</span> <span class=\"ow\">or</span> <span class=\"n\">PY3</span><span class=\"p\">:</span>\n            <span class=\"n\">mode</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;rb&#39;</span>\n            <span class=\"n\">rx</span> <span class=\"o\">=</span> <span class=\"n\">rxb</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">mode</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;r&#39;</span>\n            <span class=\"n\">rx</span> <span class=\"o\">=</span> <span class=\"n\">rxt</span>\n        <span class=\"k\">with</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"n\">file</span><span class=\"p\">,</span> <span class=\"n\">mode</span><span class=\"p\">)</span> <span class=\"k\">as</span> <span class=\"n\">f</span><span class=\"p\">:</span>\n            <span class=\"k\">for</span> <span class=\"n\">line</span> <span class=\"ow\">in</span> <span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">readlines</span><span class=\"p\">():</span>\n                <span class=\"n\">match</span> <span class=\"o\">=</span> <span class=\"n\">rx</span><span class=\"o\">.</span><span class=\"n\">search</span><span class=\"p\">(</span><span class=\"n\">line</span><span class=\"p\">)</span>\n                <span class=\"k\">if</span> <span class=\"n\">match</span><span class=\"p\">:</span>\n                    <span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">close</span><span class=\"p\">()</span>\n                    <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">match</span><span class=\"o\">.</span><span class=\"n\">group</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n                    <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">enc</span><span class=\"p\">,</span> <span class=\"n\">text_type</span><span class=\"p\">):</span>\n                        <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">enc</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"s1\">&#39;utf-8&#39;</span><span class=\"p\">)</span>\n                    <span class=\"k\">if</span> <span class=\"n\">charset_exists</span><span class=\"p\">(</span><span class=\"n\">enc</span><span class=\"p\">):</span>\n                        <span class=\"k\">return</span> <span class=\"n\">enc</span>\n    <span class=\"k\">return</span> <span class=\"n\">default_encoding</span></div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># function escape() {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"escape\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.escape\">[docs]</a>\n<span class=\"k\">def</span> <span class=\"nf\">escape</span><span class=\"p\">(</span><span class=\"n\">st</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Escapes the characters ``\\\\\\\\``, ``\\\\t``, ``\\\\n``, ``\\\\r``, ``\\\\v``,</span>\n<span class=\"sd\">    ``\\\\b``, ``\\\\f`` and ``&quot;`` in the given string ``st`` and returns it.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">return</span> <span class=\"n\">st</span><span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\\\</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;</span><span class=\"se\">\\\\</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span>\\\n             <span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\t</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;\\t&#39;</span><span class=\"p\">)</span>\\\n             <span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\r</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;\\r&#39;</span><span class=\"p\">)</span>\\\n             <span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;\\n&#39;</span><span class=\"p\">)</span>\\\n             <span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\v</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;\\v&#39;</span><span class=\"p\">)</span>\\\n             <span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\b</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;\\b&#39;</span><span class=\"p\">)</span>\\\n             <span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\f</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;\\f&#39;</span><span class=\"p\">)</span>\\\n             <span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\&quot;</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"sa\">r</span><span class=\"s1\">&#39;\\&quot;&#39;</span><span class=\"p\">)</span></div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># function unescape() {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"unescape\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.unescape\">[docs]</a>\n<span class=\"k\">def</span> <span class=\"nf\">unescape</span><span class=\"p\">(</span><span class=\"n\">st</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Unescapes the characters ``\\\\\\\\``, ``\\\\t``, ``\\\\n``, ``\\\\r``, ``\\\\v``,</span>\n<span class=\"sd\">    ``\\\\b``, ``\\\\f`` and ``&quot;`` in the given string ``st`` and returns it.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">def</span> <span class=\"nf\">unescape_repl</span><span class=\"p\">(</span><span class=\"n\">m</span><span class=\"p\">):</span>\n        <span class=\"n\">m</span> <span class=\"o\">=</span> <span class=\"n\">m</span><span class=\"o\">.</span><span class=\"n\">group</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;n&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;t&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\t</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;r&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\r</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;v&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\v</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;b&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\b</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;f&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\f</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">m</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\\\</span><span class=\"s1\">&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\\\</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">return</span> <span class=\"n\">m</span>  <span class=\"c1\"># handles escaped double quote</span>\n    <span class=\"k\">return</span> <span class=\"n\">re</span><span class=\"o\">.</span><span class=\"n\">sub</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;</span><span class=\"se\">\\\\</span><span class=\"s1\">(</span><span class=\"se\">\\\\</span><span class=\"s1\">|n|t|r|v|b|f|&quot;)&#39;</span><span class=\"p\">,</span> <span class=\"n\">unescape_repl</span><span class=\"p\">,</span> <span class=\"n\">st</span><span class=\"p\">)</span></div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># function natural_sort() {{{</span>\n\n\n<span class=\"k\">def</span> <span class=\"nf\">natural_sort</span><span class=\"p\">(</span><span class=\"n\">lst</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Sort naturally the given list.</span>\n<span class=\"sd\">    Credits: http://stackoverflow.com/a/4836734</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">def</span> <span class=\"nf\">convert</span><span class=\"p\">(</span><span class=\"n\">text</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">text</span><span class=\"p\">)</span> <span class=\"k\">if</span> <span class=\"n\">text</span><span class=\"o\">.</span><span class=\"n\">isdigit</span><span class=\"p\">()</span> <span class=\"k\">else</span> <span class=\"n\">text</span><span class=\"o\">.</span><span class=\"n\">lower</span><span class=\"p\">()</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">alphanum_key</span><span class=\"p\">(</span><span class=\"n\">key</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"n\">convert</span><span class=\"p\">(</span><span class=\"n\">c</span><span class=\"p\">)</span> <span class=\"k\">for</span> <span class=\"n\">c</span> <span class=\"ow\">in</span> <span class=\"n\">re</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s1\">&#39;([0-9]+)&#39;</span><span class=\"p\">,</span> <span class=\"n\">key</span><span class=\"p\">)]</span>\n\n    <span class=\"k\">return</span> <span class=\"nb\">sorted</span><span class=\"p\">(</span><span class=\"n\">lst</span><span class=\"p\">,</span> <span class=\"n\">key</span><span class=\"o\">=</span><span class=\"n\">alphanum_key</span><span class=\"p\">)</span>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class _BaseFile {{{</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">_BaseFile</span><span class=\"p\">(</span><span class=\"nb\">list</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Common base class for the :class:`~polib.POFile` and :class:`~polib.MOFile`</span>\n<span class=\"sd\">    classes. This class should **not** be instantiated directly.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Constructor, accepts the following keyword arguments:</span>\n\n<span class=\"sd\">        ``pofile``</span>\n<span class=\"sd\">            string, the path to the po or mo file, or its content as a string.</span>\n\n<span class=\"sd\">        ``wrapwidth``</span>\n<span class=\"sd\">            integer, the wrap width, only useful when the ``-w`` option was</span>\n<span class=\"sd\">            passed to xgettext (optional, default: ``78``).</span>\n\n<span class=\"sd\">        ``encoding``</span>\n<span class=\"sd\">            string, the encoding to use, defaults to ``default_encoding``</span>\n<span class=\"sd\">            global variable (optional).</span>\n\n<span class=\"sd\">        ``check_for_duplicates``</span>\n<span class=\"sd\">            whether to check for duplicate entries when adding entries to the</span>\n<span class=\"sd\">            file, (optional, default: ``False``).</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"nb\">list</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span>\n        <span class=\"c1\"># the opened file handle</span>\n        <span class=\"n\">pofile</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;pofile&#39;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">pofile</span> <span class=\"ow\">and</span> <span class=\"n\">_is_file</span><span class=\"p\">(</span><span class=\"n\">pofile</span><span class=\"p\">):</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"o\">=</span> <span class=\"n\">pofile</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;fpath&#39;</span><span class=\"p\">)</span>\n        <span class=\"c1\"># the width at which lines should be wrapped</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">wrapwidth</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;wrapwidth&#39;</span><span class=\"p\">,</span> <span class=\"mi\">78</span><span class=\"p\">)</span>\n        <span class=\"c1\"># the file encoding</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;encoding&#39;</span><span class=\"p\">,</span> <span class=\"n\">default_encoding</span><span class=\"p\">)</span>\n        <span class=\"c1\"># whether to check for duplicate entries or not</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">check_for_duplicates</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;check_for_duplicates&#39;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n        <span class=\"c1\"># header</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">header</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&#39;</span>\n        <span class=\"c1\"># both po and mo files have metadata</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">metadata</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">metadata_is_fuzzy</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns the unicode representation of the file.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"n\">entries</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">metadata_as_entry</span><span class=\"p\">()]</span> <span class=\"o\">+</span> \\\n                  <span class=\"p\">[</span><span class=\"n\">e</span> <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span> <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">]</span>\n        <span class=\"k\">for</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"n\">entries</span><span class=\"p\">:</span>\n            <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">wrapwidth</span><span class=\"p\">))</span>\n        <span class=\"k\">for</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete_entries</span><span class=\"p\">():</span>\n            <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">wrapwidth</span><span class=\"p\">))</span>\n        <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"n\">u</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">ret</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"n\">ret</span>\n\n    <span class=\"k\">if</span> <span class=\"n\">PY3</span><span class=\"p\">:</span>\n        <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__unicode__</span><span class=\"p\">()</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">            </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">            Returns the string representation of the file.</span>\n<span class=\"sd\">            &quot;&quot;&quot;</span>\n            <span class=\"k\">return</span> <span class=\"n\">unicode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">encode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__contains__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">entry</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Overridden ``list`` method to implement the membership test (in and</span>\n<span class=\"sd\">        not in).</span>\n<span class=\"sd\">        The method considers that an entry is in the file if it finds an entry</span>\n<span class=\"sd\">        that has the same msgid (the test is **case sensitive**) and the same</span>\n<span class=\"sd\">        msgctxt (or none for both entries).</span>\n\n<span class=\"sd\">        Argument:</span>\n\n<span class=\"sd\">        ``entry``</span>\n<span class=\"sd\">            an instance of :class:`~polib._BaseEntry`.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">find</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">,</span> <span class=\"n\">by</span><span class=\"o\">=</span><span class=\"s1\">&#39;msgid&#39;</span><span class=\"p\">,</span> <span class=\"n\">msgctxt</span><span class=\"o\">=</span><span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span><span class=\"p\">)</span> \\\n            <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">None</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__eq__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">entry</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Overridden method to check for duplicates entries, if a user tries to</span>\n<span class=\"sd\">        add an entry that is already in the file, the method will raise a</span>\n<span class=\"sd\">        ``ValueError`` exception.</span>\n\n<span class=\"sd\">        Argument:</span>\n\n<span class=\"sd\">        ``entry``</span>\n<span class=\"sd\">            an instance of :class:`~polib._BaseEntry`.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"c1\"># check_for_duplicates may not be defined (yet) when unpickling.</span>\n        <span class=\"c1\"># But if pickling, we never want to check for duplicates anyway.</span>\n        <span class=\"k\">if</span> <span class=\"nb\">getattr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"s1\">&#39;check_for_duplicates&#39;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">)</span> <span class=\"ow\">and</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"ne\">ValueError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Entry &quot;</span><span class=\"si\">%s</span><span class=\"s1\">&quot; already exists&#39;</span> <span class=\"o\">%</span> <span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">)</span>\n        <span class=\"nb\">super</span><span class=\"p\">(</span><span class=\"n\">_BaseFile</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">insert</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">index</span><span class=\"p\">,</span> <span class=\"n\">entry</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Overridden method to check for duplicates entries, if a user tries to</span>\n<span class=\"sd\">        add an entry that is already in the file, the method will raise a</span>\n<span class=\"sd\">        ``ValueError`` exception.</span>\n\n<span class=\"sd\">        Arguments:</span>\n\n<span class=\"sd\">        ``index``</span>\n<span class=\"sd\">            index at which the entry should be inserted.</span>\n\n<span class=\"sd\">        ``entry``</span>\n<span class=\"sd\">            an instance of :class:`~polib._BaseEntry`.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">check_for_duplicates</span> <span class=\"ow\">and</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"ne\">ValueError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Entry &quot;</span><span class=\"si\">%s</span><span class=\"s1\">&quot; already exists&#39;</span> <span class=\"o\">%</span> <span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">)</span>\n        <span class=\"nb\">super</span><span class=\"p\">(</span><span class=\"n\">_BaseFile</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">insert</span><span class=\"p\">(</span><span class=\"n\">index</span><span class=\"p\">,</span> <span class=\"n\">entry</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">metadata_as_entry</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns the file metadata as a :class:`~polib.POFile` instance.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">e</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">msgid</span><span class=\"o\">=</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"n\">mdata</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">ordered_metadata</span><span class=\"p\">()</span>\n        <span class=\"k\">if</span> <span class=\"n\">mdata</span><span class=\"p\">:</span>\n            <span class=\"n\">strs</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n            <span class=\"k\">for</span> <span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"n\">value</span> <span class=\"ow\">in</span> <span class=\"n\">mdata</span><span class=\"p\">:</span>\n                <span class=\"c1\"># Strip whitespace off each line in a multi-line entry</span>\n                <span class=\"n\">strs</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"si\">%s</span><span class=\"s1\">: </span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">))</span>\n            <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">strs</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">metadata_is_fuzzy</span><span class=\"p\">:</span>\n            <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">flags</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;fuzzy&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"n\">e</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">save</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fpath</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"n\">repr_method</span><span class=\"o\">=</span><span class=\"s1\">&#39;__unicode__&#39;</span><span class=\"p\">,</span> <span class=\"n\">newline</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Saves the po file to ``fpath``.</span>\n<span class=\"sd\">        If it is an existing file and no ``fpath`` is provided, then the</span>\n<span class=\"sd\">        existing file is rewritten with the modified data.</span>\n\n<span class=\"sd\">        Keyword arguments:</span>\n\n<span class=\"sd\">        ``fpath``</span>\n<span class=\"sd\">            string, full or relative path to the file.</span>\n\n<span class=\"sd\">        ``repr_method``</span>\n<span class=\"sd\">            string, the method to use for output.</span>\n\n<span class=\"sd\">        ``newline``</span>\n<span class=\"sd\">            string, controls how universal newlines works</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span> <span class=\"ow\">and</span> <span class=\"n\">fpath</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;You must provide a file path to save() method&#39;</span><span class=\"p\">)</span>\n        <span class=\"n\">contents</span> <span class=\"o\">=</span> <span class=\"nb\">getattr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">repr_method</span><span class=\"p\">)()</span>\n        <span class=\"k\">if</span> <span class=\"n\">fpath</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n            <span class=\"n\">fpath</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fpath</span>\n        <span class=\"k\">if</span> <span class=\"n\">repr_method</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;to_binary&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">with</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"s1\">&#39;wb&#39;</span><span class=\"p\">)</span> <span class=\"k\">as</span> <span class=\"n\">fhandle</span><span class=\"p\">:</span>\n                <span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"n\">contents</span><span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">with</span> <span class=\"n\">io</span><span class=\"o\">.</span><span class=\"n\">open</span><span class=\"p\">(</span>\n                <span class=\"n\">fpath</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;w&#39;</span><span class=\"p\">,</span>\n                <span class=\"n\">encoding</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span><span class=\"p\">,</span>\n                <span class=\"n\">newline</span><span class=\"o\">=</span><span class=\"n\">newline</span>\n            <span class=\"p\">)</span> <span class=\"k\">as</span> <span class=\"n\">fhandle</span><span class=\"p\">:</span>\n                <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">contents</span><span class=\"p\">,</span> <span class=\"n\">text_type</span><span class=\"p\">):</span>\n                    <span class=\"n\">contents</span> <span class=\"o\">=</span> <span class=\"n\">contents</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n                <span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">write</span><span class=\"p\">(</span><span class=\"n\">contents</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># set the file path if not set</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span> <span class=\"ow\">and</span> <span class=\"n\">fpath</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"o\">=</span> <span class=\"n\">fpath</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">find</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">st</span><span class=\"p\">,</span> <span class=\"n\">by</span><span class=\"o\">=</span><span class=\"s1\">&#39;msgid&#39;</span><span class=\"p\">,</span> <span class=\"n\">include_obsolete_entries</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">,</span>\n             <span class=\"n\">msgctxt</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Find the entry which msgid (or property identified by the ``by``</span>\n<span class=\"sd\">        argument) matches the string ``st``.</span>\n\n<span class=\"sd\">        Keyword arguments:</span>\n\n<span class=\"sd\">        ``st``</span>\n<span class=\"sd\">            string, the string to search for.</span>\n\n<span class=\"sd\">        ``by``</span>\n<span class=\"sd\">            string, the property to use for comparison (default: ``msgid``).</span>\n\n<span class=\"sd\">        ``include_obsolete_entries``</span>\n<span class=\"sd\">            boolean, whether to also search in entries that are obsolete.</span>\n\n<span class=\"sd\">        ``msgctxt``</span>\n<span class=\"sd\">            string, allows specifying a specific message context for the</span>\n<span class=\"sd\">            search.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"n\">include_obsolete_entries</span><span class=\"p\">:</span>\n            <span class=\"n\">entries</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"p\">[:]</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">entries</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">e</span> <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span> <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">]</span>\n        <span class=\"n\">matches</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"n\">entries</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span> <span class=\"nb\">getattr</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"p\">,</span> <span class=\"n\">by</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"n\">st</span><span class=\"p\">:</span>\n                <span class=\"k\">if</span> <span class=\"n\">msgctxt</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">False</span> <span class=\"ow\">and</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"o\">!=</span> <span class=\"n\">msgctxt</span><span class=\"p\">:</span>\n                    <span class=\"k\">continue</span>\n                <span class=\"n\">matches</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">matches</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"n\">matches</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n        <span class=\"k\">elif</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">matches</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">msgctxt</span><span class=\"p\">:</span>\n                <span class=\"c1\"># find the entry with no msgctx</span>\n                <span class=\"n\">e</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n                <span class=\"k\">for</span> <span class=\"n\">m</span> <span class=\"ow\">in</span> <span class=\"n\">matches</span><span class=\"p\">:</span>\n                    <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">m</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span><span class=\"p\">:</span>\n                        <span class=\"n\">e</span> <span class=\"o\">=</span> <span class=\"n\">m</span>\n                <span class=\"k\">if</span> <span class=\"n\">e</span><span class=\"p\">:</span>\n                    <span class=\"k\">return</span> <span class=\"n\">e</span>\n                <span class=\"c1\"># fallback to the first entry found</span>\n                <span class=\"k\">return</span> <span class=\"n\">matches</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n        <span class=\"k\">return</span> <span class=\"kc\">None</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">ordered_metadata</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method that returns an ordered version of the metadata</span>\n<span class=\"sd\">        dictionary. The return value is list of tuples (metadata name,</span>\n<span class=\"sd\">        metadata_value).</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"c1\"># copy the dict first</span>\n        <span class=\"n\">metadata</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">metadata</span><span class=\"o\">.</span><span class=\"n\">copy</span><span class=\"p\">()</span>\n        <span class=\"n\">data_order</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n            <span class=\"s1\">&#39;Project-Id-Version&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;Report-Msgid-Bugs-To&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;POT-Creation-Date&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;PO-Revision-Date&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;Last-Translator&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;Language-Team&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;Language&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;MIME-Version&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;Content-Type&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;Content-Transfer-Encoding&#39;</span><span class=\"p\">,</span>\n            <span class=\"s1\">&#39;Plural-Forms&#39;</span>\n        <span class=\"p\">]</span>\n        <span class=\"n\">ordered_data</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"k\">for</span> <span class=\"n\">data</span> <span class=\"ow\">in</span> <span class=\"n\">data_order</span><span class=\"p\">:</span>\n            <span class=\"k\">try</span><span class=\"p\">:</span>\n                <span class=\"n\">value</span> <span class=\"o\">=</span> <span class=\"n\">metadata</span><span class=\"o\">.</span><span class=\"n\">pop</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n                <span class=\"n\">ordered_data</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">((</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">))</span>\n            <span class=\"k\">except</span> <span class=\"ne\">KeyError</span><span class=\"p\">:</span>\n                <span class=\"k\">pass</span>\n        <span class=\"c1\"># the rest of the metadata will be alphabetically ordered since there</span>\n        <span class=\"c1\"># are no specs for this AFAIK</span>\n        <span class=\"k\">for</span> <span class=\"n\">data</span> <span class=\"ow\">in</span> <span class=\"n\">natural_sort</span><span class=\"p\">(</span><span class=\"n\">metadata</span><span class=\"o\">.</span><span class=\"n\">keys</span><span class=\"p\">()):</span>\n            <span class=\"n\">value</span> <span class=\"o\">=</span> <span class=\"n\">metadata</span><span class=\"p\">[</span><span class=\"n\">data</span><span class=\"p\">]</span>\n            <span class=\"n\">ordered_data</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">((</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">))</span>\n        <span class=\"k\">return</span> <span class=\"n\">ordered_data</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">to_binary</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Return the binary representation of the file.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">offsets</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"n\">entries</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">translated_entries</span><span class=\"p\">()</span>\n\n        <span class=\"c1\"># the keys are sorted in the .mo file</span>\n        <span class=\"k\">def</span> <span class=\"nf\">cmp</span><span class=\"p\">(</span><span class=\"n\">_self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n            <span class=\"c1\"># msgfmt compares entries with msgctxt if it exists</span>\n            <span class=\"n\">self_msgid</span> <span class=\"o\">=</span> <span class=\"n\">_self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"ow\">and</span> <span class=\"n\">_self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"ow\">or</span> <span class=\"n\">_self</span><span class=\"o\">.</span><span class=\"n\">msgid</span>\n            <span class=\"n\">other_msgid</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"ow\">and</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"ow\">or</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgid</span>\n            <span class=\"k\">if</span> <span class=\"n\">self_msgid</span> <span class=\"o\">&gt;</span> <span class=\"n\">other_msgid</span><span class=\"p\">:</span>\n                <span class=\"k\">return</span> <span class=\"mi\">1</span>\n            <span class=\"k\">elif</span> <span class=\"n\">self_msgid</span> <span class=\"o\">&lt;</span> <span class=\"n\">other_msgid</span><span class=\"p\">:</span>\n                <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n            <span class=\"k\">else</span><span class=\"p\">:</span>\n                <span class=\"k\">return</span> <span class=\"mi\">0</span>\n        <span class=\"c1\"># add metadata entry</span>\n        <span class=\"n\">entries</span><span class=\"o\">.</span><span class=\"n\">sort</span><span class=\"p\">(</span><span class=\"n\">key</span><span class=\"o\">=</span><span class=\"k\">lambda</span> <span class=\"n\">o</span><span class=\"p\">:</span> <span class=\"n\">o</span><span class=\"o\">.</span><span class=\"n\">msgid_with_context</span><span class=\"o\">.</span><span class=\"n\">encode</span><span class=\"p\">(</span><span class=\"s1\">&#39;utf-8&#39;</span><span class=\"p\">))</span>\n        <span class=\"n\">mentry</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">metadata_as_entry</span><span class=\"p\">()</span>\n        <span class=\"n\">entries</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">mentry</span><span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"n\">entries</span>\n        <span class=\"n\">entries_len</span> <span class=\"o\">=</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">entries</span><span class=\"p\">)</span>\n        <span class=\"n\">ids</span><span class=\"p\">,</span> <span class=\"n\">strs</span> <span class=\"o\">=</span> <span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">),</span> <span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"n\">entries</span><span class=\"p\">:</span>\n            <span class=\"c1\"># For each string, we need size and file offset.  Each string is</span>\n            <span class=\"c1\"># NUL terminated; the NUL does not count into the size.</span>\n            <span class=\"n\">msgid</span> <span class=\"o\">=</span> <span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n            <span class=\"k\">if</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span><span class=\"p\">:</span>\n                <span class=\"c1\"># Contexts are stored by storing the concatenation of the</span>\n                <span class=\"c1\"># context, a &lt;EOT&gt; byte, and the original string</span>\n                <span class=\"n\">msgid</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_encode</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"o\">+</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\4</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span>\n            <span class=\"k\">if</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span><span class=\"p\">:</span>\n                <span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n                <span class=\"k\">for</span> <span class=\"n\">index</span> <span class=\"ow\">in</span> <span class=\"nb\">sorted</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"o\">.</span><span class=\"n\">keys</span><span class=\"p\">()):</span>\n                    <span class=\"n\">msgstr</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"n\">index</span><span class=\"p\">])</span>\n                <span class=\"n\">msgid</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_encode</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgid</span> <span class=\"o\">+</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\0</span><span class=\"s1\">&#39;</span> <span class=\"o\">+</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span><span class=\"p\">)</span>\n                <span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_encode</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\0</span><span class=\"s1\">&#39;</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">msgstr</span><span class=\"p\">))</span>\n            <span class=\"k\">else</span><span class=\"p\">:</span>\n                <span class=\"n\">msgid</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_encode</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">)</span>\n                <span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_encode</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">msgstr</span><span class=\"p\">)</span>\n            <span class=\"n\">offsets</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">((</span><span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">ids</span><span class=\"p\">),</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">msgid</span><span class=\"p\">),</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">strs</span><span class=\"p\">),</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">msgstr</span><span class=\"p\">)))</span>\n            <span class=\"n\">ids</span> <span class=\"o\">+=</span> <span class=\"n\">msgid</span> <span class=\"o\">+</span> <span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\0</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span>\n            <span class=\"n\">strs</span> <span class=\"o\">+=</span> <span class=\"n\">msgstr</span> <span class=\"o\">+</span> <span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\0</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># The header is 7 32-bit unsigned integers.</span>\n        <span class=\"n\">keystart</span> <span class=\"o\">=</span> <span class=\"mi\">7</span> <span class=\"o\">*</span> <span class=\"mi\">4</span> <span class=\"o\">+</span> <span class=\"mi\">16</span> <span class=\"o\">*</span> <span class=\"n\">entries_len</span>\n        <span class=\"c1\"># and the values start after the keys</span>\n        <span class=\"n\">valuestart</span> <span class=\"o\">=</span> <span class=\"n\">keystart</span> <span class=\"o\">+</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">ids</span><span class=\"p\">)</span>\n        <span class=\"n\">koffsets</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"n\">voffsets</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"c1\"># The string table first has the list of keys, then the list of values.</span>\n        <span class=\"c1\"># Each entry has first the size of the string, then the file offset.</span>\n        <span class=\"k\">for</span> <span class=\"n\">o1</span><span class=\"p\">,</span> <span class=\"n\">l1</span><span class=\"p\">,</span> <span class=\"n\">o2</span><span class=\"p\">,</span> <span class=\"n\">l2</span> <span class=\"ow\">in</span> <span class=\"n\">offsets</span><span class=\"p\">:</span>\n            <span class=\"n\">koffsets</span> <span class=\"o\">+=</span> <span class=\"p\">[</span><span class=\"n\">l1</span><span class=\"p\">,</span> <span class=\"n\">o1</span> <span class=\"o\">+</span> <span class=\"n\">keystart</span><span class=\"p\">]</span>\n            <span class=\"n\">voffsets</span> <span class=\"o\">+=</span> <span class=\"p\">[</span><span class=\"n\">l2</span><span class=\"p\">,</span> <span class=\"n\">o2</span> <span class=\"o\">+</span> <span class=\"n\">valuestart</span><span class=\"p\">]</span>\n        <span class=\"n\">offsets</span> <span class=\"o\">=</span> <span class=\"n\">koffsets</span> <span class=\"o\">+</span> <span class=\"n\">voffsets</span>\n\n        <span class=\"n\">output</span> <span class=\"o\">=</span> <span class=\"n\">struct</span><span class=\"o\">.</span><span class=\"n\">pack</span><span class=\"p\">(</span>\n            <span class=\"s2\">&quot;Iiiiiii&quot;</span><span class=\"p\">,</span>\n            <span class=\"c1\"># Magic number</span>\n            <span class=\"n\">MOFile</span><span class=\"o\">.</span><span class=\"n\">MAGIC</span><span class=\"p\">,</span>\n            <span class=\"c1\"># Version</span>\n            <span class=\"mi\">0</span><span class=\"p\">,</span>\n            <span class=\"c1\"># number of entries</span>\n            <span class=\"n\">entries_len</span><span class=\"p\">,</span>\n            <span class=\"c1\"># start of key index</span>\n            <span class=\"mi\">7</span> <span class=\"o\">*</span> <span class=\"mi\">4</span><span class=\"p\">,</span>\n            <span class=\"c1\"># start of value index</span>\n            <span class=\"mi\">7</span> <span class=\"o\">*</span> <span class=\"mi\">4</span> <span class=\"o\">+</span> <span class=\"n\">entries_len</span> <span class=\"o\">*</span> <span class=\"mi\">8</span><span class=\"p\">,</span>\n            <span class=\"c1\"># size and offset of hash table, we don&#39;t use hash tables</span>\n            <span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"n\">keystart</span>\n\n        <span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">PY3</span> <span class=\"ow\">and</span> <span class=\"n\">sys</span><span class=\"o\">.</span><span class=\"n\">version_info</span><span class=\"o\">.</span><span class=\"n\">minor</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>  <span class=\"c1\"># python 3.2 or superior</span>\n            <span class=\"n\">output</span> <span class=\"o\">+=</span> <span class=\"n\">array</span><span class=\"o\">.</span><span class=\"n\">array</span><span class=\"p\">(</span><span class=\"s2\">&quot;i&quot;</span><span class=\"p\">,</span> <span class=\"n\">offsets</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">tobytes</span><span class=\"p\">()</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">output</span> <span class=\"o\">+=</span> <span class=\"n\">array</span><span class=\"o\">.</span><span class=\"n\">array</span><span class=\"p\">(</span><span class=\"s2\">&quot;i&quot;</span><span class=\"p\">,</span> <span class=\"n\">offsets</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">tostring</span><span class=\"p\">()</span>\n        <span class=\"n\">output</span> <span class=\"o\">+=</span> <span class=\"n\">ids</span>\n        <span class=\"n\">output</span> <span class=\"o\">+=</span> <span class=\"n\">strs</span>\n        <span class=\"k\">return</span> <span class=\"n\">output</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">_encode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">mixed</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Encodes the given ``mixed`` argument with the file encoding if and</span>\n<span class=\"sd\">        only if it&#39;s an unicode string and returns the encoded string.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">mixed</span><span class=\"p\">,</span> <span class=\"n\">text_type</span><span class=\"p\">):</span>\n            <span class=\"n\">mixed</span> <span class=\"o\">=</span> <span class=\"n\">mixed</span><span class=\"o\">.</span><span class=\"n\">encode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"n\">mixed</span>\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class POFile {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"POFile\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile\">[docs]</a>\n<span class=\"k\">class</span> <span class=\"nc\">POFile</span><span class=\"p\">(</span><span class=\"n\">_BaseFile</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Po (or Pot) file reader/writer.</span>\n<span class=\"sd\">    This class inherits the :class:`~polib._BaseFile` class and, by extension,</span>\n<span class=\"sd\">    the python ``list`` type.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns the unicode representation of the po file.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">ret</span><span class=\"p\">,</span> <span class=\"n\">headers</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">header</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">for</span> <span class=\"n\">header</span> <span class=\"ow\">in</span> <span class=\"n\">headers</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">header</span><span class=\"p\">):</span>\n                <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"s2\">&quot;#</span><span class=\"se\">\\n</span><span class=\"s2\">&quot;</span>\n            <span class=\"k\">elif</span> <span class=\"n\">header</span><span class=\"p\">[:</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;,&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;:&#39;</span><span class=\"p\">]:</span>\n                <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"s1\">&#39;#</span><span class=\"si\">%s</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"n\">header</span>\n            <span class=\"k\">else</span><span class=\"p\">:</span>\n                <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"s1\">&#39;# </span><span class=\"si\">%s</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"n\">header</span>\n\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">ret</span><span class=\"p\">,</span> <span class=\"n\">text_type</span><span class=\"p\">):</span>\n            <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n\n        <span class=\"k\">return</span> <span class=\"n\">ret</span> <span class=\"o\">+</span> <span class=\"n\">_BaseFile</span><span class=\"o\">.</span><span class=\"n\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span>\n\n<div class=\"viewcode-block\" id=\"POFile.save_as_mofile\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile.save_as_mofile\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">save_as_mofile</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fpath</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Saves the binary representation of the file to given ``fpath``.</span>\n\n<span class=\"sd\">        Keyword argument:</span>\n\n<span class=\"sd\">        ``fpath``</span>\n<span class=\"sd\">            string, full or relative path to the mo file.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">_BaseFile</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"s1\">&#39;to_binary&#39;</span><span class=\"p\">)</span></div>\n\n\n<div class=\"viewcode-block\" id=\"POFile.percent_translated\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile.percent_translated\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">percent_translated</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method that returns the percentage of translated</span>\n<span class=\"sd\">        messages.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">total</span> <span class=\"o\">=</span> <span class=\"nb\">len</span><span class=\"p\">([</span><span class=\"n\">e</span> <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span> <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">])</span>\n        <span class=\"k\">if</span> <span class=\"n\">total</span> <span class=\"o\">==</span> <span class=\"mi\">0</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"mi\">100</span>\n        <span class=\"n\">translated</span> <span class=\"o\">=</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">translated_entries</span><span class=\"p\">())</span>\n        <span class=\"k\">return</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">translated</span> <span class=\"o\">*</span> <span class=\"mi\">100</span> <span class=\"o\">/</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"n\">total</span><span class=\"p\">))</span></div>\n\n\n<div class=\"viewcode-block\" id=\"POFile.translated_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile.translated_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">translated_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method that returns the list of translated entries.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"n\">e</span> <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span> <span class=\"k\">if</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">translated</span><span class=\"p\">()]</span></div>\n\n\n<div class=\"viewcode-block\" id=\"POFile.untranslated_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile.untranslated_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">untranslated_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method that returns the list of untranslated entries.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"n\">e</span> <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span> <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">translated</span><span class=\"p\">()</span> <span class=\"ow\">and</span> <span class=\"ow\">not</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">obsolete</span>\n                <span class=\"ow\">and</span> <span class=\"ow\">not</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">fuzzy</span><span class=\"p\">]</span></div>\n\n\n<div class=\"viewcode-block\" id=\"POFile.fuzzy_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile.fuzzy_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">fuzzy_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method that returns the list of fuzzy entries.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"n\">e</span> <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span> <span class=\"k\">if</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">fuzzy</span> <span class=\"ow\">and</span> <span class=\"ow\">not</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">]</span></div>\n\n\n<div class=\"viewcode-block\" id=\"POFile.obsolete_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile.obsolete_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">obsolete_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method that returns the list of obsolete entries.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"p\">[</span><span class=\"n\">e</span> <span class=\"k\">for</span> <span class=\"n\">e</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span> <span class=\"k\">if</span> <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">]</span></div>\n\n\n<div class=\"viewcode-block\" id=\"POFile.merge\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POFile.merge\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">merge</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">refpot</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method that merges the current pofile with the pot file</span>\n<span class=\"sd\">        provided. It behaves exactly as the gettext msgmerge utility:</span>\n\n<span class=\"sd\">        * comments of this file will be preserved, but extracted comments and</span>\n<span class=\"sd\">          occurrences will be discarded;</span>\n<span class=\"sd\">        * any translations or comments in the file will be discarded, however,</span>\n<span class=\"sd\">          dot comments and file positions will be preserved;</span>\n<span class=\"sd\">        * the fuzzy flags are preserved.</span>\n\n<span class=\"sd\">        Keyword argument:</span>\n\n<span class=\"sd\">        ``refpot``</span>\n<span class=\"sd\">            object POFile, the reference catalog.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"c1\"># Store entries in dict/set for faster access</span>\n        <span class=\"n\">self_entries</span> <span class=\"o\">=</span> <span class=\"nb\">dict</span><span class=\"p\">(</span>\n            <span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgid_with_context</span><span class=\"p\">,</span> <span class=\"n\">entry</span><span class=\"p\">)</span> <span class=\"k\">for</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span>\n        <span class=\"p\">)</span>\n        <span class=\"n\">refpot_msgids</span> <span class=\"o\">=</span> <span class=\"nb\">set</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgid_with_context</span> <span class=\"k\">for</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"n\">refpot</span><span class=\"p\">)</span>\n        <span class=\"c1\"># Merge entries that are in the refpot</span>\n        <span class=\"k\">for</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"n\">refpot</span><span class=\"p\">:</span>\n            <span class=\"n\">e</span> <span class=\"o\">=</span> <span class=\"n\">self_entries</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgid_with_context</span><span class=\"p\">)</span>\n            <span class=\"k\">if</span> <span class=\"n\">e</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n                <span class=\"n\">e</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">()</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">e</span><span class=\"p\">)</span>\n            <span class=\"n\">e</span><span class=\"o\">.</span><span class=\"n\">merge</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"p\">)</span>\n        <span class=\"c1\"># ok, now we must &quot;obsolete&quot; entries that are not in the refpot anymore</span>\n        <span class=\"k\">for</span> <span class=\"n\">entry</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span> <span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">msgid_with_context</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"n\">refpot_msgids</span><span class=\"p\">:</span>\n                <span class=\"n\">entry</span><span class=\"o\">.</span><span class=\"n\">obsolete</span> <span class=\"o\">=</span> <span class=\"kc\">True</span></div>\n</div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class MOFile {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"MOFile\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile\">[docs]</a>\n<span class=\"k\">class</span> <span class=\"nc\">MOFile</span><span class=\"p\">(</span><span class=\"n\">_BaseFile</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Mo file reader/writer.</span>\n<span class=\"sd\">    This class inherits the :class:`~polib._BaseFile` class and, by</span>\n<span class=\"sd\">    extension, the python ``list`` type.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"n\">MAGIC</span> <span class=\"o\">=</span> <span class=\"mh\">0x950412de</span>\n    <span class=\"n\">MAGIC_SWAPPED</span> <span class=\"o\">=</span> <span class=\"mh\">0xde120495</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Constructor, accepts all keywords arguments accepted by</span>\n<span class=\"sd\">        :class:`~polib._BaseFile` class.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">_BaseFile</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">magic_number</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">version</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n\n<div class=\"viewcode-block\" id=\"MOFile.save_as_pofile\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile.save_as_pofile\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">save_as_pofile</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fpath</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Saves the mofile as a pofile to ``fpath``.</span>\n\n<span class=\"sd\">        Keyword argument:</span>\n\n<span class=\"sd\">        ``fpath``</span>\n<span class=\"sd\">            string, full or relative path to the file.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">_BaseFile</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fpath</span><span class=\"p\">)</span></div>\n\n\n<div class=\"viewcode-block\" id=\"MOFile.save\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile.save\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">save</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fpath</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Saves the mofile to ``fpath``.</span>\n\n<span class=\"sd\">        Keyword argument:</span>\n\n<span class=\"sd\">        ``fpath``</span>\n<span class=\"sd\">            string, full or relative path to the file.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">_BaseFile</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"s1\">&#39;to_binary&#39;</span><span class=\"p\">)</span></div>\n\n\n<div class=\"viewcode-block\" id=\"MOFile.percent_translated\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile.percent_translated\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">percent_translated</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method to keep the same interface with POFile instances.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"mi\">100</span></div>\n\n\n<div class=\"viewcode-block\" id=\"MOFile.translated_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile.translated_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">translated_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method to keep the same interface with POFile instances.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span></div>\n\n\n<div class=\"viewcode-block\" id=\"MOFile.untranslated_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile.untranslated_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">untranslated_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method to keep the same interface with POFile instances.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"p\">[]</span></div>\n\n\n<div class=\"viewcode-block\" id=\"MOFile.fuzzy_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile.fuzzy_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">fuzzy_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method to keep the same interface with POFile instances.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"p\">[]</span></div>\n\n\n<div class=\"viewcode-block\" id=\"MOFile.obsolete_entries\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOFile.obsolete_entries\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">obsolete_entries</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Convenience method to keep the same interface with POFile instances.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">return</span> <span class=\"p\">[]</span></div>\n</div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class _BaseEntry {{{</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">_BaseEntry</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Base class for :class:`~polib.POEntry` and :class:`~polib.MOEntry` classes.</span>\n<span class=\"sd\">    This class should **not** be instantiated directly.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Constructor, accepts the following keyword arguments:</span>\n\n<span class=\"sd\">        ``msgid``</span>\n<span class=\"sd\">            string, the entry msgid.</span>\n\n<span class=\"sd\">        ``msgstr``</span>\n<span class=\"sd\">            string, the entry msgstr.</span>\n\n<span class=\"sd\">        ``msgid_plural``</span>\n<span class=\"sd\">            string, the entry msgid_plural.</span>\n\n<span class=\"sd\">        ``msgstr_plural``</span>\n<span class=\"sd\">            dict, the entry msgstr_plural lines.</span>\n\n<span class=\"sd\">        ``msgctxt``</span>\n<span class=\"sd\">            string, the entry context (msgctxt).</span>\n\n<span class=\"sd\">        ``obsolete``</span>\n<span class=\"sd\">            bool, whether the entry is &quot;obsolete&quot; or not.</span>\n\n<span class=\"sd\">        ``encoding``</span>\n<span class=\"sd\">            string, the encoding to use, defaults to ``default_encoding``</span>\n<span class=\"sd\">            global variable (optional).</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;msgid&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;msgstr&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;msgid_plural&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;msgstr_plural&#39;</span><span class=\"p\">,</span> <span class=\"p\">{})</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;msgctxt&#39;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;obsolete&#39;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;encoding&#39;</span><span class=\"p\">,</span> <span class=\"n\">default_encoding</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">wrapwidth</span><span class=\"o\">=</span><span class=\"mi\">78</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns the unicode representation of the entry.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">:</span>\n            <span class=\"n\">delflag</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;#~ &#39;</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">delflag</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&#39;</span>\n        <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"c1\"># write the msgctxt if any</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n            <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_str_field</span><span class=\"p\">(</span><span class=\"s2\">&quot;msgctxt&quot;</span><span class=\"p\">,</span> <span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span><span class=\"p\">,</span>\n                                   <span class=\"n\">wrapwidth</span><span class=\"p\">)</span>\n        <span class=\"c1\"># write the msgid</span>\n        <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_str_field</span><span class=\"p\">(</span><span class=\"s2\">&quot;msgid&quot;</span><span class=\"p\">,</span> <span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">,</span> <span class=\"n\">wrapwidth</span><span class=\"p\">)</span>\n        <span class=\"c1\"># write the msgid_plural if any</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span><span class=\"p\">:</span>\n            <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_str_field</span><span class=\"p\">(</span><span class=\"s2\">&quot;msgid_plural&quot;</span><span class=\"p\">,</span> <span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span>\n                                   <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span><span class=\"p\">,</span> <span class=\"n\">wrapwidth</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">:</span>\n            <span class=\"c1\"># write the msgstr_plural if any</span>\n            <span class=\"n\">msgstrs</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span>\n            <span class=\"n\">keys</span> <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">(</span><span class=\"n\">msgstrs</span><span class=\"p\">)</span>\n            <span class=\"n\">keys</span><span class=\"o\">.</span><span class=\"n\">sort</span><span class=\"p\">()</span>\n            <span class=\"k\">for</span> <span class=\"n\">index</span> <span class=\"ow\">in</span> <span class=\"n\">keys</span><span class=\"p\">:</span>\n                <span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"n\">msgstrs</span><span class=\"p\">[</span><span class=\"n\">index</span><span class=\"p\">]</span>\n                <span class=\"n\">plural_index</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;[</span><span class=\"si\">%s</span><span class=\"s1\">]&#39;</span> <span class=\"o\">%</span> <span class=\"n\">index</span>\n                <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_str_field</span><span class=\"p\">(</span><span class=\"s2\">&quot;msgstr&quot;</span><span class=\"p\">,</span> <span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"n\">plural_index</span><span class=\"p\">,</span> <span class=\"n\">msgstr</span><span class=\"p\">,</span>\n                                       <span class=\"n\">wrapwidth</span><span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"c1\"># otherwise write the msgstr</span>\n            <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_str_field</span><span class=\"p\">(</span><span class=\"s2\">&quot;msgstr&quot;</span><span class=\"p\">,</span> <span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr</span><span class=\"p\">,</span>\n                                   <span class=\"n\">wrapwidth</span><span class=\"p\">)</span>\n        <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"n\">u</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">ret</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"n\">ret</span>\n\n    <span class=\"k\">if</span> <span class=\"n\">PY3</span><span class=\"p\">:</span>\n        <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n            <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__unicode__</span><span class=\"p\">()</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">            </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">            Returns the string representation of the entry.</span>\n<span class=\"sd\">            &quot;&quot;&quot;</span>\n            <span class=\"k\">return</span> <span class=\"n\">unicode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">encode</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__eq__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"nb\">str</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">_str_field</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fieldname</span><span class=\"p\">,</span> <span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"n\">plural_index</span><span class=\"p\">,</span> <span class=\"n\">field</span><span class=\"p\">,</span>\n                   <span class=\"n\">wrapwidth</span><span class=\"o\">=</span><span class=\"mi\">78</span><span class=\"p\">):</span>\n        <span class=\"n\">lines</span> <span class=\"o\">=</span> <span class=\"n\">field</span><span class=\"o\">.</span><span class=\"n\">splitlines</span><span class=\"p\">(</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">lines</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n            <span class=\"n\">lines</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"n\">lines</span>  <span class=\"c1\"># start with initial empty line</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">escaped_field</span> <span class=\"o\">=</span> <span class=\"n\">escape</span><span class=\"p\">(</span><span class=\"n\">field</span><span class=\"p\">)</span>\n            <span class=\"n\">specialchars_count</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n            <span class=\"k\">for</span> <span class=\"n\">c</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;</span><span class=\"se\">\\\\</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\r</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\t</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\v</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\b</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\f</span><span class=\"s1\">&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;&quot;&#39;</span><span class=\"p\">]:</span>\n                <span class=\"n\">specialchars_count</span> <span class=\"o\">+=</span> <span class=\"n\">field</span><span class=\"o\">.</span><span class=\"n\">count</span><span class=\"p\">(</span><span class=\"n\">c</span><span class=\"p\">)</span>\n            <span class=\"c1\"># comparison must take into account fieldname length + one space</span>\n            <span class=\"c1\"># + 2 quotes (eg. msgid &quot;&lt;string&gt;&quot;)</span>\n            <span class=\"n\">flength</span> <span class=\"o\">=</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">fieldname</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"mi\">3</span>\n            <span class=\"k\">if</span> <span class=\"n\">plural_index</span><span class=\"p\">:</span>\n                <span class=\"n\">flength</span> <span class=\"o\">+=</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">plural_index</span><span class=\"p\">)</span>\n            <span class=\"n\">real_wrapwidth</span> <span class=\"o\">=</span> <span class=\"n\">wrapwidth</span> <span class=\"o\">-</span> <span class=\"n\">flength</span> <span class=\"o\">+</span> <span class=\"n\">specialchars_count</span>\n            <span class=\"k\">if</span> <span class=\"n\">wrapwidth</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span> <span class=\"ow\">and</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">field</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"n\">real_wrapwidth</span><span class=\"p\">:</span>\n                <span class=\"c1\"># Wrap the line but take field name into account</span>\n                <span class=\"n\">lines</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"p\">[</span><span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"n\">item</span><span class=\"p\">)</span> <span class=\"k\">for</span> <span class=\"n\">item</span> <span class=\"ow\">in</span> <span class=\"n\">textwrap</span><span class=\"o\">.</span><span class=\"n\">wrap</span><span class=\"p\">(</span>\n                    <span class=\"n\">escaped_field</span><span class=\"p\">,</span>\n                    <span class=\"n\">wrapwidth</span> <span class=\"o\">-</span> <span class=\"mi\">2</span><span class=\"p\">,</span>  <span class=\"c1\"># 2 for quotes &quot;&quot;</span>\n                    <span class=\"n\">drop_whitespace</span><span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">,</span>\n                    <span class=\"n\">break_long_words</span><span class=\"o\">=</span><span class=\"kc\">False</span>\n                <span class=\"p\">)]</span>\n            <span class=\"k\">else</span><span class=\"p\">:</span>\n                <span class=\"n\">lines</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">field</span><span class=\"p\">]</span>\n        <span class=\"k\">if</span> <span class=\"n\">fieldname</span><span class=\"o\">.</span><span class=\"n\">startswith</span><span class=\"p\">(</span><span class=\"s1\">&#39;previous_&#39;</span><span class=\"p\">):</span>\n            <span class=\"c1\"># quick and dirty trick to get the real field name</span>\n            <span class=\"n\">fieldname</span> <span class=\"o\">=</span> <span class=\"n\">fieldname</span><span class=\"p\">[</span><span class=\"mi\">9</span><span class=\"p\">:]</span>\n\n        <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;</span><span class=\"si\">%s%s%s</span><span class=\"s1\"> &quot;</span><span class=\"si\">%s</span><span class=\"s1\">&quot;&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"n\">fieldname</span><span class=\"p\">,</span> <span class=\"n\">plural_index</span><span class=\"p\">,</span>\n                                <span class=\"n\">escape</span><span class=\"p\">(</span><span class=\"n\">lines</span><span class=\"o\">.</span><span class=\"n\">pop</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">)))]</span>\n        <span class=\"k\">for</span> <span class=\"n\">line</span> <span class=\"ow\">in</span> <span class=\"n\">lines</span><span class=\"p\">:</span>\n            <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"si\">%s</span><span class=\"s1\">&quot;</span><span class=\"si\">%s</span><span class=\"s1\">&quot;&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"n\">delflag</span><span class=\"p\">,</span> <span class=\"n\">escape</span><span class=\"p\">(</span><span class=\"n\">line</span><span class=\"p\">)))</span>\n        <span class=\"k\">return</span> <span class=\"n\">ret</span>\n\n    <span class=\"nd\">@property</span>\n    <span class=\"k\">def</span> <span class=\"nf\">msgid_with_context</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"s1\">&#39;</span><span class=\"si\">%s%s%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span><span class=\"p\">,</span> <span class=\"s2\">&quot;</span><span class=\"se\">\\x04</span><span class=\"s2\">&quot;</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span>\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class POEntry {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"POEntry\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POEntry\">[docs]</a>\n<span class=\"k\">class</span> <span class=\"nc\">POEntry</span><span class=\"p\">(</span><span class=\"n\">_BaseEntry</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Represents a po file entry.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Constructor, accepts the following keyword arguments:</span>\n\n<span class=\"sd\">        ``comment``</span>\n<span class=\"sd\">            string, the entry comment.</span>\n\n<span class=\"sd\">        ``tcomment``</span>\n<span class=\"sd\">            string, the entry translator comment.</span>\n\n<span class=\"sd\">        ``occurrences``</span>\n<span class=\"sd\">            list, the entry occurrences.</span>\n\n<span class=\"sd\">        ``flags``</span>\n<span class=\"sd\">            list, the entry flags.</span>\n\n<span class=\"sd\">        ``previous_msgctxt``</span>\n<span class=\"sd\">            string, the entry previous context.</span>\n\n<span class=\"sd\">        ``previous_msgid``</span>\n<span class=\"sd\">            string, the entry previous msgid.</span>\n\n<span class=\"sd\">        ``previous_msgid_plural``</span>\n<span class=\"sd\">            string, the entry previous msgid_plural.</span>\n\n<span class=\"sd\">        ``linenum``</span>\n<span class=\"sd\">            integer, the line number of the entry</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">_BaseEntry</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">comment</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;comment&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">tcomment</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;tcomment&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">occurrences</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;occurrences&#39;</span><span class=\"p\">,</span> <span class=\"p\">[])</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;flags&#39;</span><span class=\"p\">,</span> <span class=\"p\">[])</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgctxt</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;previous_msgctxt&#39;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgid</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;previous_msgid&#39;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgid_plural</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;previous_msgid_plural&#39;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">linenum</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;linenum&#39;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">wrapwidth</span><span class=\"o\">=</span><span class=\"mi\">78</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns the unicode representation of the entry.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"c1\"># comments first, if any (with text wrapping as xgettext does)</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">:</span>\n            <span class=\"n\">comments</span> <span class=\"o\">=</span> <span class=\"p\">[(</span><span class=\"s1\">&#39;tcomment&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;# &#39;</span><span class=\"p\">)]</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">comments</span> <span class=\"o\">=</span> <span class=\"p\">[(</span><span class=\"s1\">&#39;tcomment&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;# &#39;</span><span class=\"p\">),</span> <span class=\"p\">(</span><span class=\"s1\">&#39;comment&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;#. &#39;</span><span class=\"p\">)]</span>\n        <span class=\"k\">for</span> <span class=\"n\">c</span> <span class=\"ow\">in</span> <span class=\"n\">comments</span><span class=\"p\">:</span>\n            <span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"nb\">getattr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">c</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">])</span>\n            <span class=\"k\">if</span> <span class=\"n\">val</span><span class=\"p\">:</span>\n                <span class=\"k\">for</span> <span class=\"n\">comment</span> <span class=\"ow\">in</span> <span class=\"n\">val</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">):</span>\n                    <span class=\"k\">if</span> <span class=\"n\">wrapwidth</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span> <span class=\"ow\">and</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">comment</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">c</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">])</span> <span class=\"o\">&gt;</span> <span class=\"n\">wrapwidth</span><span class=\"p\">:</span>\n                        <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"n\">textwrap</span><span class=\"o\">.</span><span class=\"n\">wrap</span><span class=\"p\">(</span>\n                            <span class=\"n\">comment</span><span class=\"p\">,</span>\n                            <span class=\"n\">wrapwidth</span><span class=\"p\">,</span>\n                            <span class=\"n\">initial_indent</span><span class=\"o\">=</span><span class=\"n\">c</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">],</span>\n                            <span class=\"n\">subsequent_indent</span><span class=\"o\">=</span><span class=\"n\">c</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">],</span>\n                            <span class=\"n\">break_long_words</span><span class=\"o\">=</span><span class=\"kc\">False</span>\n                        <span class=\"p\">)</span>\n                    <span class=\"k\">else</span><span class=\"p\">:</span>\n                        <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"si\">%s%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"n\">c</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">],</span> <span class=\"n\">comment</span><span class=\"p\">))</span>\n\n        <span class=\"c1\"># occurrences (with text wrapping as xgettext does)</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span> <span class=\"ow\">and</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">occurrences</span><span class=\"p\">:</span>\n            <span class=\"n\">filelist</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n            <span class=\"k\">for</span> <span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"n\">lineno</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">occurrences</span><span class=\"p\">:</span>\n                <span class=\"k\">if</span> <span class=\"n\">lineno</span><span class=\"p\">:</span>\n                    <span class=\"n\">filelist</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"si\">%s</span><span class=\"s1\">:</span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"n\">lineno</span><span class=\"p\">))</span>\n                <span class=\"k\">else</span><span class=\"p\">:</span>\n                    <span class=\"n\">filelist</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">)</span>\n            <span class=\"n\">filestr</span> <span class=\"o\">=</span> <span class=\"s1\">&#39; &#39;</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">filelist</span><span class=\"p\">)</span>\n            <span class=\"k\">if</span> <span class=\"n\">wrapwidth</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span> <span class=\"ow\">and</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">filestr</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"mi\">3</span> <span class=\"o\">&gt;</span> <span class=\"n\">wrapwidth</span><span class=\"p\">:</span>\n                <span class=\"c1\"># textwrap split words that contain hyphen, this is not</span>\n                <span class=\"c1\"># what we want for filenames, so the dirty hack is to</span>\n                <span class=\"c1\"># temporally replace hyphens with a char that a file cannot</span>\n                <span class=\"c1\"># contain, like &quot;*&quot;</span>\n                <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"p\">[</span><span class=\"n\">line</span><span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;*&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;-&#39;</span><span class=\"p\">)</span> <span class=\"k\">for</span> <span class=\"n\">line</span> <span class=\"ow\">in</span> <span class=\"n\">textwrap</span><span class=\"o\">.</span><span class=\"n\">wrap</span><span class=\"p\">(</span>\n                    <span class=\"n\">filestr</span><span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"s1\">&#39;-&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;*&#39;</span><span class=\"p\">),</span>\n                    <span class=\"n\">wrapwidth</span><span class=\"p\">,</span>\n                    <span class=\"n\">initial_indent</span><span class=\"o\">=</span><span class=\"s1\">&#39;#: &#39;</span><span class=\"p\">,</span>\n                    <span class=\"n\">subsequent_indent</span><span class=\"o\">=</span><span class=\"s1\">&#39;#: &#39;</span><span class=\"p\">,</span>\n                    <span class=\"n\">break_long_words</span><span class=\"o\">=</span><span class=\"kc\">False</span>\n                <span class=\"p\">)]</span>\n            <span class=\"k\">else</span><span class=\"p\">:</span>\n                <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;#: &#39;</span> <span class=\"o\">+</span> <span class=\"n\">filestr</span><span class=\"p\">)</span>\n\n        <span class=\"c1\"># flags (TODO: wrapping ?)</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span><span class=\"p\">:</span>\n            <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;#, </span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"s1\">&#39;, &#39;</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span><span class=\"p\">))</span>\n\n        <span class=\"c1\"># previous context and previous msgid/msgid_plural</span>\n        <span class=\"n\">fields</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;previous_msgctxt&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;previous_msgid&#39;</span><span class=\"p\">,</span>\n                  <span class=\"s1\">&#39;previous_msgid_plural&#39;</span><span class=\"p\">]</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">:</span>\n            <span class=\"n\">prefix</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;#~| &quot;</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">prefix</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;#| &quot;</span>\n        <span class=\"k\">for</span> <span class=\"n\">f</span> <span class=\"ow\">in</span> <span class=\"n\">fields</span><span class=\"p\">:</span>\n            <span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"nb\">getattr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">f</span><span class=\"p\">)</span>\n            <span class=\"k\">if</span> <span class=\"n\">val</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n                <span class=\"n\">ret</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_str_field</span><span class=\"p\">(</span><span class=\"n\">f</span><span class=\"p\">,</span> <span class=\"n\">prefix</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">,</span> <span class=\"n\">val</span><span class=\"p\">,</span> <span class=\"n\">wrapwidth</span><span class=\"p\">)</span>\n\n        <span class=\"n\">ret</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">_BaseEntry</span><span class=\"o\">.</span><span class=\"n\">__unicode__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">wrapwidth</span><span class=\"p\">))</span>\n        <span class=\"n\">ret</span> <span class=\"o\">=</span> <span class=\"n\">u</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">join</span><span class=\"p\">(</span><span class=\"n\">ret</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"n\">ret</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">__cmp__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Called by comparison operations if rich comparison is not defined.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"c1\"># First: Obsolete test</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span> <span class=\"o\">!=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span><span class=\"p\">:</span>\n                <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n            <span class=\"k\">else</span><span class=\"p\">:</span>\n                <span class=\"k\">return</span> <span class=\"mi\">1</span>\n        <span class=\"c1\"># Work on a copy to protect original</span>\n        <span class=\"n\">occ1</span> <span class=\"o\">=</span> <span class=\"nb\">sorted</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">occurrences</span><span class=\"p\">[:])</span>\n        <span class=\"n\">occ2</span> <span class=\"o\">=</span> <span class=\"nb\">sorted</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">occurrences</span><span class=\"p\">[:])</span>\n        <span class=\"k\">if</span> <span class=\"n\">occ1</span> <span class=\"o\">&gt;</span> <span class=\"n\">occ2</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"mi\">1</span>\n        <span class=\"k\">if</span> <span class=\"n\">occ1</span> <span class=\"o\">&lt;</span> <span class=\"n\">occ2</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n        <span class=\"c1\"># Compare context</span>\n        <span class=\"n\">msgctxt</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"ow\">or</span> <span class=\"s1\">&#39;0&#39;</span>\n        <span class=\"n\">othermsgctxt</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"ow\">or</span> <span class=\"s1\">&#39;0&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">msgctxt</span> <span class=\"o\">&gt;</span> <span class=\"n\">othermsgctxt</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"mi\">1</span>\n        <span class=\"k\">elif</span> <span class=\"n\">msgctxt</span> <span class=\"o\">&lt;</span> <span class=\"n\">othermsgctxt</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n        <span class=\"c1\"># Compare msgid_plural</span>\n        <span class=\"n\">msgid_plural</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span> <span class=\"ow\">or</span> <span class=\"s1\">&#39;0&#39;</span>\n        <span class=\"n\">othermsgid_plural</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span> <span class=\"ow\">or</span> <span class=\"s1\">&#39;0&#39;</span>\n        <span class=\"k\">if</span> <span class=\"n\">msgid_plural</span> <span class=\"o\">&gt;</span> <span class=\"n\">othermsgid_plural</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"mi\">1</span>\n        <span class=\"k\">elif</span> <span class=\"n\">msgid_plural</span> <span class=\"o\">&lt;</span> <span class=\"n\">othermsgid_plural</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n        <span class=\"c1\"># Compare msgstr_plural</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span> <span class=\"ow\">and</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">,</span> <span class=\"nb\">dict</span><span class=\"p\">):</span>\n            <span class=\"n\">msgstr_plural</span> <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"o\">.</span><span class=\"n\">values</span><span class=\"p\">())</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">msgstr_plural</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"k\">if</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span> <span class=\"ow\">and</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">,</span> <span class=\"nb\">dict</span><span class=\"p\">):</span>\n            <span class=\"n\">othermsgstr_plural</span> <span class=\"o\">=</span> <span class=\"nb\">list</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"o\">.</span><span class=\"n\">values</span><span class=\"p\">())</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">othermsgstr_plural</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"k\">if</span> <span class=\"n\">msgstr_plural</span> <span class=\"o\">&gt;</span> <span class=\"n\">othermsgstr_plural</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"mi\">1</span>\n        <span class=\"k\">elif</span> <span class=\"n\">msgstr_plural</span> <span class=\"o\">&lt;</span> <span class=\"n\">othermsgstr_plural</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n        <span class=\"c1\"># Compare msgid</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span> <span class=\"o\">&gt;</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"mi\">1</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span> <span class=\"o\">&lt;</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n        <span class=\"c1\"># Compare msgstr</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr</span> <span class=\"o\">&gt;</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgstr</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"mi\">1</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr</span> <span class=\"o\">&lt;</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgstr</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"o\">-</span><span class=\"mi\">1</span>\n        <span class=\"k\">return</span> <span class=\"mi\">0</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__gt__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__cmp__</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__lt__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__cmp__</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span> <span class=\"o\">&lt;</span> <span class=\"mi\">0</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__ge__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__cmp__</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span> <span class=\"o\">&gt;=</span> <span class=\"mi\">0</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__le__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__cmp__</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span> <span class=\"o\">&lt;=</span> <span class=\"mi\">0</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__eq__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__cmp__</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">0</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__ne__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">__cmp__</span><span class=\"p\">(</span><span class=\"n\">other</span><span class=\"p\">)</span> <span class=\"o\">!=</span> <span class=\"mi\">0</span>\n\n<div class=\"viewcode-block\" id=\"POEntry.translated\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POEntry.translated\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">translated</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Returns ``True`` if the entry has been translated or ``False``</span>\n<span class=\"sd\">        otherwise.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span> <span class=\"ow\">or</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fuzzy</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"kc\">False</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr</span> <span class=\"o\">!=</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"kc\">True</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">:</span>\n            <span class=\"k\">for</span> <span class=\"n\">pos</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">:</span>\n                <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">:</span>\n                    <span class=\"k\">return</span> <span class=\"kc\">False</span>\n            <span class=\"k\">return</span> <span class=\"kc\">True</span>\n        <span class=\"k\">return</span> <span class=\"kc\">False</span></div>\n\n\n<div class=\"viewcode-block\" id=\"POEntry.merge\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.POEntry.merge\">[docs]</a>\n    <span class=\"k\">def</span> <span class=\"nf\">merge</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Merge the current entry with the given pot entry.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgid</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">occurrences</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">occurrences</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">comment</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">comment</span>\n        <span class=\"n\">fuzzy</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fuzzy</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">flags</span><span class=\"p\">[:]</span>  <span class=\"c1\"># clone flags</span>\n        <span class=\"k\">if</span> <span class=\"n\">fuzzy</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"s1\">&#39;fuzzy&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">obsolete</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">obsolete</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgctxt</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">previous_msgctxt</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgid</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">previous_msgid</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgid_plural</span> <span class=\"o\">=</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">previous_msgid_plural</span>\n        <span class=\"k\">if</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">:</span>\n            <span class=\"k\">for</span> <span class=\"n\">pos</span> <span class=\"ow\">in</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">:</span>\n                <span class=\"k\">try</span><span class=\"p\">:</span>\n                    <span class=\"c1\"># keep existing translation at pos if any</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"p\">]</span>\n                <span class=\"k\">except</span> <span class=\"ne\">KeyError</span><span class=\"p\">:</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"n\">pos</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&#39;</span></div>\n\n\n    <span class=\"nd\">@property</span>\n    <span class=\"k\">def</span> <span class=\"nf\">fuzzy</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"s1\">&#39;fuzzy&#39;</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span>\n\n    <span class=\"nd\">@fuzzy</span><span class=\"o\">.</span><span class=\"n\">setter</span>\n    <span class=\"k\">def</span> <span class=\"nf\">fuzzy</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">value</span><span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"n\">value</span> <span class=\"ow\">and</span> <span class=\"ow\">not</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fuzzy</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span><span class=\"o\">.</span><span class=\"n\">insert</span><span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"s1\">&#39;fuzzy&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">elif</span> <span class=\"ow\">not</span> <span class=\"n\">value</span> <span class=\"ow\">and</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fuzzy</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span><span class=\"o\">.</span><span class=\"n\">remove</span><span class=\"p\">(</span><span class=\"s1\">&#39;fuzzy&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__hash__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">hash</span><span class=\"p\">((</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr</span><span class=\"p\">))</span></div>\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class MOEntry {{{</span>\n\n\n<div class=\"viewcode-block\" id=\"MOEntry\">\n<a class=\"viewcode-back\" href=\"../../api/#polib.MOEntry\">[docs]</a>\n<span class=\"k\">class</span> <span class=\"nc\">MOEntry</span><span class=\"p\">(</span><span class=\"n\">_BaseEntry</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Represents a mo file entry.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Constructor, accepts the following keyword arguments,</span>\n<span class=\"sd\">        for consistency with :class:`~polib.POEntry`:</span>\n\n<span class=\"sd\">        ``comment``</span>\n<span class=\"sd\">        ``tcomment``</span>\n<span class=\"sd\">        ``occurrences``</span>\n<span class=\"sd\">        ``flags``</span>\n<span class=\"sd\">        ``previous_msgctxt``</span>\n<span class=\"sd\">        ``previous_msgid``</span>\n<span class=\"sd\">        ``previous_msgid_plural``</span>\n\n<span class=\"sd\">        Note: even though these keyword arguments are accepted,</span>\n<span class=\"sd\">        they hold no real meaning in the context of MO files</span>\n<span class=\"sd\">        and are simply ignored.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">_BaseEntry</span><span class=\"o\">.</span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">comment</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&#39;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">tcomment</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&#39;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">occurrences</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">flags</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgctxt</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgid</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">previous_msgid_plural</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__hash__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">hash</span><span class=\"p\">((</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgid</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr</span><span class=\"p\">))</span></div>\n\n\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class _POFileParser {{{</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">_POFileParser</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    A finite state machine to efficiently and correctly parse po</span>\n<span class=\"sd\">    file format.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">pofile</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Constructor.</span>\n\n<span class=\"sd\">        Keyword arguments:</span>\n\n<span class=\"sd\">        ``pofile``</span>\n<span class=\"sd\">            string, path to the po file or its content</span>\n\n<span class=\"sd\">        ``encoding``</span>\n<span class=\"sd\">            string, the encoding to use, defaults to ``default_encoding``</span>\n<span class=\"sd\">            global variable (optional).</span>\n\n<span class=\"sd\">        ``check_for_duplicates``</span>\n<span class=\"sd\">            whether to check for duplicate entries when adding entries to the</span>\n<span class=\"sd\">            file (optional, default: ``False``).</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;encoding&#39;</span><span class=\"p\">,</span> <span class=\"n\">default_encoding</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">_is_file</span><span class=\"p\">(</span><span class=\"n\">pofile</span><span class=\"p\">):</span>\n            <span class=\"k\">try</span><span class=\"p\">:</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span> <span class=\"o\">=</span> <span class=\"n\">io</span><span class=\"o\">.</span><span class=\"n\">open</span><span class=\"p\">(</span><span class=\"n\">pofile</span><span class=\"p\">,</span> <span class=\"s1\">&#39;rt&#39;</span><span class=\"p\">,</span> <span class=\"n\">encoding</span><span class=\"o\">=</span><span class=\"n\">enc</span><span class=\"p\">)</span>\n            <span class=\"k\">except</span> <span class=\"ne\">LookupError</span><span class=\"p\">:</span>\n                <span class=\"n\">enc</span> <span class=\"o\">=</span> <span class=\"n\">default_encoding</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span> <span class=\"o\">=</span> <span class=\"n\">io</span><span class=\"o\">.</span><span class=\"n\">open</span><span class=\"p\">(</span><span class=\"n\">pofile</span><span class=\"p\">,</span> <span class=\"s1\">&#39;rt&#39;</span><span class=\"p\">,</span> <span class=\"n\">encoding</span><span class=\"o\">=</span><span class=\"n\">enc</span><span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span> <span class=\"o\">=</span> <span class=\"n\">pofile</span><span class=\"o\">.</span><span class=\"n\">splitlines</span><span class=\"p\">()</span>\n\n        <span class=\"n\">klass</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;klass&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">klass</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n            <span class=\"n\">klass</span> <span class=\"o\">=</span> <span class=\"n\">POFile</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span> <span class=\"o\">=</span> <span class=\"n\">klass</span><span class=\"p\">(</span>\n            <span class=\"n\">pofile</span><span class=\"o\">=</span><span class=\"n\">pofile</span><span class=\"p\">,</span>\n            <span class=\"n\">encoding</span><span class=\"o\">=</span><span class=\"n\">enc</span><span class=\"p\">,</span>\n            <span class=\"n\">check_for_duplicates</span><span class=\"o\">=</span><span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;check_for_duplicates&#39;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n        <span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">transitions</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;st&#39;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n        <span class=\"c1\"># two memo flags used in handlers</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_index</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">entry_obsolete</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n        <span class=\"c1\"># Configure the state machine, by adding transitions.</span>\n        <span class=\"c1\"># Signification of symbols:</span>\n        <span class=\"c1\">#     * ST: Beginning of the file (start)</span>\n        <span class=\"c1\">#     * HE: Header</span>\n        <span class=\"c1\">#     * TC: a translation comment</span>\n        <span class=\"c1\">#     * GC: a generated comment</span>\n        <span class=\"c1\">#     * OC: a file/line occurrence</span>\n        <span class=\"c1\">#     * FL: a flags line</span>\n        <span class=\"c1\">#     * CT: a message context</span>\n        <span class=\"c1\">#     * PC: a previous msgctxt</span>\n        <span class=\"c1\">#     * PM: a previous msgid</span>\n        <span class=\"c1\">#     * PP: a previous msgid_plural</span>\n        <span class=\"c1\">#     * MI: a msgid</span>\n        <span class=\"c1\">#     * MP: a msgid plural</span>\n        <span class=\"c1\">#     * MS: a msgstr</span>\n        <span class=\"c1\">#     * MX: a msgstr plural</span>\n        <span class=\"c1\">#     * MC: a msgid or msgstr continuation line</span>\n        <span class=\"nb\">all</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;st&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;he&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;oc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;fl&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ct&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">,</span>\n               <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">]</span>\n\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;st&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;he&#39;</span><span class=\"p\">],</span>                                     <span class=\"s1\">&#39;he&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;oc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;fl&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span>\n                        <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">],</span>                               <span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">,</span> <span class=\"nb\">all</span><span class=\"p\">,</span>                                              <span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;oc&#39;</span><span class=\"p\">,</span> <span class=\"nb\">all</span><span class=\"p\">,</span>                                              <span class=\"s1\">&#39;oc&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;fl&#39;</span><span class=\"p\">,</span> <span class=\"nb\">all</span><span class=\"p\">,</span>                                              <span class=\"s1\">&#39;fl&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">,</span> <span class=\"nb\">all</span><span class=\"p\">,</span>                                              <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span> <span class=\"nb\">all</span><span class=\"p\">,</span>                                              <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span> <span class=\"nb\">all</span><span class=\"p\">,</span>                                              <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;ct&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;st&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;he&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;oc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;fl&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span>\n                        <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">],</span>                               <span class=\"s1\">&#39;ct&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;st&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;he&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;oc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;fl&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ct&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">,</span>\n                 <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">],</span>                                <span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">],</span>             <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">],</span>                               <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">],</span>                         <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">add</span><span class=\"p\">(</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s1\">&#39;ct&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">],</span> <span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">parse</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Run the state machine, parse the file line by line and call process()</span>\n<span class=\"sd\">        with the current matched symbol.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            <span class=\"n\">keywords</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n                <span class=\"s1\">&#39;msgctxt&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;ct&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;msgid&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;msgstr&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;msgid_plural&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">,</span>\n            <span class=\"p\">}</span>\n            <span class=\"n\">prev_keywords</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n                <span class=\"s1\">&#39;msgid_plural&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;msgid&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;msgctxt&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">,</span>\n            <span class=\"p\">}</span>\n            <span class=\"n\">tokens</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n            <span class=\"n\">fpath</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;</span><span class=\"si\">%s</span><span class=\"s1\"> &#39;</span> <span class=\"o\">%</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"k\">else</span> <span class=\"s1\">&#39;&#39;</span>\n            <span class=\"k\">for</span> <span class=\"n\">line</span> <span class=\"ow\">in</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"p\">:</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span> <span class=\"o\">+=</span> <span class=\"mi\">1</span>\n                <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span> <span class=\"o\">==</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                    <span class=\"n\">BOM</span> <span class=\"o\">=</span> <span class=\"n\">codecs</span><span class=\"o\">.</span><span class=\"n\">BOM_UTF8</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"s1\">&#39;utf-8&#39;</span><span class=\"p\">)</span>\n                    <span class=\"k\">if</span> <span class=\"n\">line</span><span class=\"o\">.</span><span class=\"n\">startswith</span><span class=\"p\">(</span><span class=\"n\">BOM</span><span class=\"p\">):</span>\n                        <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"p\">[</span><span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">BOM</span><span class=\"p\">):]</span>\n                <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n                <span class=\"k\">if</span> <span class=\"n\">line</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">:</span>\n                    <span class=\"k\">continue</span>\n\n                <span class=\"n\">tokens</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">)</span>\n                <span class=\"n\">nb_tokens</span> <span class=\"o\">=</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">tokens</span><span class=\"p\">)</span>\n\n                <span class=\"k\">if</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#~|&#39;</span><span class=\"p\">:</span>\n                    <span class=\"k\">continue</span>\n\n                <span class=\"k\">if</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#~&#39;</span> <span class=\"ow\">and</span> <span class=\"n\">nb_tokens</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                    <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"p\">[</span><span class=\"mi\">3</span><span class=\"p\">:]</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n                    <span class=\"n\">tokens</span> <span class=\"o\">=</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>\n                    <span class=\"n\">nb_tokens</span> <span class=\"o\">-=</span> <span class=\"mi\">1</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">entry_obsolete</span> <span class=\"o\">=</span> <span class=\"mi\">1</span>\n                <span class=\"k\">else</span><span class=\"p\">:</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">entry_obsolete</span> <span class=\"o\">=</span> <span class=\"mi\">0</span>\n\n                <span class=\"c1\"># Take care of keywords like</span>\n                <span class=\"c1\"># msgid, msgid_plural, msgctxt &amp; msgstr.</span>\n                <span class=\"k\">if</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"ow\">in</span> <span class=\"n\">keywords</span> <span class=\"ow\">and</span> <span class=\"n\">nb_tokens</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                    <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"p\">[</span><span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]):]</span><span class=\"o\">.</span><span class=\"n\">lstrip</span><span class=\"p\">()</span>\n                    <span class=\"k\">if</span> <span class=\"n\">re</span><span class=\"o\">.</span><span class=\"n\">search</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;([^</span><span class=\"se\">\\\\</span><span class=\"s1\">]|^)&quot;&#39;</span><span class=\"p\">,</span> <span class=\"n\">line</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]):</span>\n                        <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Syntax error in po file </span><span class=\"si\">%s</span><span class=\"s1\">(line </span><span class=\"si\">%s</span><span class=\"s1\">): &#39;</span>\n                                      <span class=\"s1\">&#39;unescaped double quote found&#39;</span> <span class=\"o\">%</span>\n                                      <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">))</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span> <span class=\"o\">=</span> <span class=\"n\">line</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"n\">keywords</span><span class=\"p\">[</span><span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]])</span>\n                    <span class=\"k\">continue</span>\n\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span> <span class=\"o\">=</span> <span class=\"n\">line</span>\n\n                <span class=\"k\">if</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#:&#39;</span><span class=\"p\">:</span>\n                    <span class=\"k\">if</span> <span class=\"n\">nb_tokens</span> <span class=\"o\">&lt;=</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                        <span class=\"k\">continue</span>\n                    <span class=\"c1\"># we are on a occurrences line</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"s1\">&#39;oc&#39;</span><span class=\"p\">)</span>\n\n                <span class=\"k\">elif</span> <span class=\"n\">line</span><span class=\"p\">[:</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;&quot;&#39;</span><span class=\"p\">:</span>\n                    <span class=\"c1\"># we are on a continuation line</span>\n                    <span class=\"k\">if</span> <span class=\"n\">re</span><span class=\"o\">.</span><span class=\"n\">search</span><span class=\"p\">(</span><span class=\"sa\">r</span><span class=\"s1\">&#39;([^</span><span class=\"se\">\\\\</span><span class=\"s1\">]|^)&quot;&#39;</span><span class=\"p\">,</span> <span class=\"n\">line</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]):</span>\n                        <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Syntax error in po file </span><span class=\"si\">%s</span><span class=\"s1\">(line </span><span class=\"si\">%s</span><span class=\"s1\">): &#39;</span>\n                                      <span class=\"s1\">&#39;unescaped double quote found&#39;</span> <span class=\"o\">%</span>\n                                      <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">))</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">)</span>\n\n                <span class=\"k\">elif</span> <span class=\"n\">line</span><span class=\"p\">[:</span><span class=\"mi\">7</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;msgstr[&#39;</span><span class=\"p\">:</span>\n                    <span class=\"c1\"># we are on a msgstr plural</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">)</span>\n\n                <span class=\"k\">elif</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#,&#39;</span><span class=\"p\">:</span>\n                    <span class=\"k\">if</span> <span class=\"n\">nb_tokens</span> <span class=\"o\">&lt;=</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                        <span class=\"k\">continue</span>\n                    <span class=\"c1\"># we are on a flags line</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"s1\">&#39;fl&#39;</span><span class=\"p\">)</span>\n\n                <span class=\"k\">elif</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#&#39;</span> <span class=\"ow\">or</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">startswith</span><span class=\"p\">(</span><span class=\"s1\">&#39;##&#39;</span><span class=\"p\">):</span>\n                    <span class=\"k\">if</span> <span class=\"n\">line</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#&#39;</span><span class=\"p\">:</span>\n                        <span class=\"n\">line</span> <span class=\"o\">+=</span> <span class=\"s1\">&#39; &#39;</span>\n                    <span class=\"c1\"># we are on a translator comment line</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"s1\">&#39;tc&#39;</span><span class=\"p\">)</span>\n\n                <span class=\"k\">elif</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#.&#39;</span><span class=\"p\">:</span>\n                    <span class=\"k\">if</span> <span class=\"n\">nb_tokens</span> <span class=\"o\">&lt;=</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                        <span class=\"k\">continue</span>\n                    <span class=\"c1\"># we are on a generated comment line</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"s1\">&#39;gc&#39;</span><span class=\"p\">)</span>\n\n                <span class=\"k\">elif</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;#|&#39;</span><span class=\"p\">:</span>\n                    <span class=\"k\">if</span> <span class=\"n\">nb_tokens</span> <span class=\"o\">&lt;=</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                        <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Syntax error in po file </span><span class=\"si\">%s</span><span class=\"s1\">(line </span><span class=\"si\">%s</span><span class=\"s1\">)&#39;</span> <span class=\"o\">%</span>\n                                      <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">))</span>\n\n                    <span class=\"c1\"># Remove the marker and any whitespace right after that.</span>\n                    <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">:]</span><span class=\"o\">.</span><span class=\"n\">lstrip</span><span class=\"p\">()</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span> <span class=\"o\">=</span> <span class=\"n\">line</span>\n\n                    <span class=\"k\">if</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">startswith</span><span class=\"p\">(</span><span class=\"s1\">&#39;&quot;&#39;</span><span class=\"p\">):</span>\n                        <span class=\"c1\"># Continuation of previous metadata.</span>\n                        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">)</span>\n                        <span class=\"k\">continue</span>\n\n                    <span class=\"k\">if</span> <span class=\"n\">nb_tokens</span> <span class=\"o\">==</span> <span class=\"mi\">2</span><span class=\"p\">:</span>\n                        <span class=\"c1\"># Invalid continuation line.</span>\n                        <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Syntax error in po file </span><span class=\"si\">%s</span><span class=\"s1\">(line </span><span class=\"si\">%s</span><span class=\"s1\">): &#39;</span>\n                                      <span class=\"s1\">&#39;invalid continuation line&#39;</span> <span class=\"o\">%</span>\n                                      <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">))</span>\n\n                    <span class=\"c1\"># we are on a &quot;previous translation&quot; comment line,</span>\n                    <span class=\"k\">if</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"n\">prev_keywords</span><span class=\"p\">:</span>\n                        <span class=\"c1\"># Unknown keyword in previous translation comment.</span>\n                        <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Syntax error in po file </span><span class=\"si\">%s</span><span class=\"s1\">(line </span><span class=\"si\">%s</span><span class=\"s1\">): &#39;</span>\n                                      <span class=\"s1\">&#39;unknown keyword </span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span>\n                                      <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">,</span>\n                                       <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]))</span>\n\n                    <span class=\"c1\"># Remove the keyword and any whitespace</span>\n                    <span class=\"c1\"># between it and the starting quote.</span>\n                    <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"p\">[</span><span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]):]</span><span class=\"o\">.</span><span class=\"n\">lstrip</span><span class=\"p\">()</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span> <span class=\"o\">=</span> <span class=\"n\">line</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">process</span><span class=\"p\">(</span><span class=\"n\">prev_keywords</span><span class=\"p\">[</span><span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]])</span>\n\n                <span class=\"k\">else</span><span class=\"p\">:</span>\n                    <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Syntax error in po file </span><span class=\"si\">%s</span><span class=\"s1\">(line </span><span class=\"si\">%s</span><span class=\"s1\">)&#39;</span> <span class=\"o\">%</span>\n                                  <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">))</span>\n\n            <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"ow\">and</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">tokens</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">0</span> <span class=\"ow\">and</span> \\\n               <span class=\"ow\">not</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">startswith</span><span class=\"p\">(</span><span class=\"s1\">&#39;#&#39;</span><span class=\"p\">):</span>\n                <span class=\"c1\"># since entries are added when another entry is found, we must</span>\n                <span class=\"c1\"># add the last entry here (only if there are lines). Trailing</span>\n                <span class=\"c1\"># comments are ignored</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n\n            <span class=\"c1\"># before returning the instance, check if there&#39;s metadata and if</span>\n            <span class=\"c1\"># so extract it in a dict</span>\n            <span class=\"n\">metadataentry</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">find</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n            <span class=\"k\">if</span> <span class=\"n\">metadataentry</span><span class=\"p\">:</span>  <span class=\"c1\"># metadata found</span>\n                <span class=\"c1\"># remove the entry</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">remove</span><span class=\"p\">(</span><span class=\"n\">metadataentry</span><span class=\"p\">)</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">metadata_is_fuzzy</span> <span class=\"o\">=</span> <span class=\"n\">metadataentry</span><span class=\"o\">.</span><span class=\"n\">flags</span>\n                <span class=\"n\">key</span> <span class=\"o\">=</span> <span class=\"kc\">None</span>\n                <span class=\"k\">for</span> <span class=\"n\">msg</span> <span class=\"ow\">in</span> <span class=\"n\">metadataentry</span><span class=\"o\">.</span><span class=\"n\">msgstr</span><span class=\"o\">.</span><span class=\"n\">splitlines</span><span class=\"p\">():</span>\n                    <span class=\"k\">try</span><span class=\"p\">:</span>\n                        <span class=\"n\">key</span><span class=\"p\">,</span> <span class=\"n\">val</span> <span class=\"o\">=</span> <span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s1\">&#39;:&#39;</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n                        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">metadata</span><span class=\"p\">[</span><span class=\"n\">key</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">val</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n                    <span class=\"k\">except</span> <span class=\"p\">(</span><span class=\"ne\">ValueError</span><span class=\"p\">,</span> <span class=\"ne\">KeyError</span><span class=\"p\">):</span>\n                        <span class=\"k\">if</span> <span class=\"n\">key</span> <span class=\"ow\">is</span> <span class=\"ow\">not</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n                            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">metadata</span><span class=\"p\">[</span><span class=\"n\">key</span><span class=\"p\">]</span> <span class=\"o\">+=</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span> <span class=\"o\">+</span> <span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n        <span class=\"k\">finally</span><span class=\"p\">:</span>\n            <span class=\"c1\"># close opened file</span>\n            <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"nb\">isinstance</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"p\">,</span> <span class=\"nb\">list</span><span class=\"p\">):</span>  <span class=\"c1\"># must be file</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">close</span><span class=\"p\">()</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">add</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">symbol</span><span class=\"p\">,</span> <span class=\"n\">states</span><span class=\"p\">,</span> <span class=\"n\">next_state</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Add a transition to the state machine.</span>\n\n<span class=\"sd\">        Keywords arguments:</span>\n\n<span class=\"sd\">        ``symbol``</span>\n<span class=\"sd\">            string, the matched token (two chars symbol).</span>\n\n<span class=\"sd\">        ``states``</span>\n<span class=\"sd\">            list, a list of states (two chars symbols).</span>\n\n<span class=\"sd\">        ``next_state``</span>\n<span class=\"sd\">            the next state the fsm will have after the action.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">for</span> <span class=\"n\">state</span> <span class=\"ow\">in</span> <span class=\"n\">states</span><span class=\"p\">:</span>\n            <span class=\"n\">action</span> <span class=\"o\">=</span> <span class=\"nb\">getattr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"s1\">&#39;handle_</span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"n\">next_state</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">transitions</span><span class=\"p\">[(</span><span class=\"n\">symbol</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">)]</span> <span class=\"o\">=</span> <span class=\"p\">(</span><span class=\"n\">action</span><span class=\"p\">,</span> <span class=\"n\">next_state</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">process</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">symbol</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Process the transition corresponding to the current state and the</span>\n<span class=\"sd\">        symbol provided.</span>\n\n<span class=\"sd\">        Keywords arguments:</span>\n\n<span class=\"sd\">        ``symbol``</span>\n<span class=\"sd\">            string, the matched token (two chars symbol).</span>\n\n<span class=\"sd\">        ``linenum``</span>\n<span class=\"sd\">            integer, the current line number of the parsed file.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            <span class=\"p\">(</span><span class=\"n\">action</span><span class=\"p\">,</span> <span class=\"n\">state</span><span class=\"p\">)</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">transitions</span><span class=\"p\">[(</span><span class=\"n\">symbol</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span><span class=\"p\">)]</span>\n            <span class=\"k\">if</span> <span class=\"n\">action</span><span class=\"p\">():</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">=</span> <span class=\"n\">state</span>\n        <span class=\"k\">except</span> <span class=\"ne\">Exception</span><span class=\"p\">:</span>\n            <span class=\"n\">fpath</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;</span><span class=\"si\">%s</span><span class=\"s1\"> &#39;</span> <span class=\"o\">%</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">fpath</span> <span class=\"k\">else</span> <span class=\"s1\">&#39;&#39;</span>\n            <span class=\"k\">if</span> <span class=\"nb\">hasattr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"p\">,</span> <span class=\"s1\">&#39;close&#39;</span><span class=\"p\">):</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">close</span><span class=\"p\">()</span>\n            <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Syntax error in po file </span><span class=\"si\">%s</span><span class=\"s1\">(line </span><span class=\"si\">%s</span><span class=\"s1\">)&#39;</span> <span class=\"o\">%</span>\n                          <span class=\"p\">(</span><span class=\"n\">fpath</span><span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">))</span>\n\n    <span class=\"c1\"># state handlers</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_he</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a header comment.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">header</span> <span class=\"o\">!=</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">header</span> <span class=\"o\">+=</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">header</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">2</span><span class=\"p\">:]</span>\n        <span class=\"k\">return</span> <span class=\"mi\">1</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_tc</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a translator comment.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">tcomment</span> <span class=\"o\">!=</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">tcomment</span> <span class=\"o\">+=</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span>\n        <span class=\"n\">tcomment</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"o\">.</span><span class=\"n\">lstrip</span><span class=\"p\">(</span><span class=\"s1\">&#39;#&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">tcomment</span><span class=\"o\">.</span><span class=\"n\">startswith</span><span class=\"p\">(</span><span class=\"s1\">&#39; &#39;</span><span class=\"p\">):</span>\n            <span class=\"n\">tcomment</span> <span class=\"o\">=</span> <span class=\"n\">tcomment</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:]</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">tcomment</span> <span class=\"o\">+=</span> <span class=\"n\">tcomment</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_gc</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a generated comment.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">comment</span> <span class=\"o\">!=</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">comment</span> <span class=\"o\">+=</span> <span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">comment</span> <span class=\"o\">+=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">3</span><span class=\"p\">:]</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_oc</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a file:num occurrence.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"n\">occurrences</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">3</span><span class=\"p\">:]</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">()</span>\n        <span class=\"k\">for</span> <span class=\"n\">occurrence</span> <span class=\"ow\">in</span> <span class=\"n\">occurrences</span><span class=\"p\">:</span>\n            <span class=\"k\">if</span> <span class=\"n\">occurrence</span> <span class=\"o\">!=</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">:</span>\n                <span class=\"k\">try</span><span class=\"p\">:</span>\n                    <span class=\"n\">fil</span><span class=\"p\">,</span> <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"n\">occurrence</span><span class=\"o\">.</span><span class=\"n\">rsplit</span><span class=\"p\">(</span><span class=\"s1\">&#39;:&#39;</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n                    <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">line</span><span class=\"o\">.</span><span class=\"n\">isdigit</span><span class=\"p\">():</span>\n                        <span class=\"n\">fil</span> <span class=\"o\">=</span> <span class=\"n\">occurrence</span>\n                        <span class=\"n\">line</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&#39;</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">occurrences</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">((</span><span class=\"n\">fil</span><span class=\"p\">,</span> <span class=\"n\">line</span><span class=\"p\">))</span>\n                <span class=\"k\">except</span> <span class=\"p\">(</span><span class=\"ne\">ValueError</span><span class=\"p\">,</span> <span class=\"ne\">AttributeError</span><span class=\"p\">):</span>\n                    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">occurrences</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">((</span><span class=\"n\">occurrence</span><span class=\"p\">,</span> <span class=\"s1\">&#39;&#39;</span><span class=\"p\">))</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_fl</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a flags line.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">flags</span> <span class=\"o\">+=</span> <span class=\"p\">[</span><span class=\"n\">c</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span> <span class=\"k\">for</span> <span class=\"n\">c</span> <span class=\"ow\">in</span>\n                                     <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">3</span><span class=\"p\">:]</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"s1\">&#39;,&#39;</span><span class=\"p\">)]</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_pp</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a previous msgid_plural line.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">previous_msgid_plural</span> <span class=\"o\">=</span> \\\n            <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_pm</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a previous msgid line.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">previous_msgid</span> <span class=\"o\">=</span> \\\n            <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_pc</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a previous msgctxt line.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">previous_msgctxt</span> <span class=\"o\">=</span> \\\n            <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_ct</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a msgctxt.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"o\">=</span> <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_mi</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a msgid.&quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"ow\">in</span> <span class=\"p\">[</span><span class=\"s1\">&#39;mc&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">]:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span> <span class=\"o\">=</span> <span class=\"n\">POEntry</span><span class=\"p\">(</span><span class=\"n\">linenum</span><span class=\"o\">=</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_line</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">obsolete</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">entry_obsolete</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgid</span> <span class=\"o\">=</span> <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_mp</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a msgid plural.&quot;&quot;&quot;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span> <span class=\"o\">=</span> <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_ms</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a msgstr.&quot;&quot;&quot;</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_mx</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a msgstr plural.&quot;&quot;&quot;</span>\n        <span class=\"n\">index</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">7</span><span class=\"p\">]</span>\n        <span class=\"n\">value</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"o\">.</span><span class=\"n\">find</span><span class=\"p\">(</span><span class=\"s1\">&#39;&quot;&#39;</span><span class=\"p\">)</span> <span class=\"o\">+</span> <span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">index</span><span class=\"p\">)]</span> <span class=\"o\">=</span> <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"n\">value</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_index</span> <span class=\"o\">=</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"n\">index</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> <span class=\"kc\">True</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">handle_mc</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;Handle a msgid or msgstr continuation line.&quot;&quot;&quot;</span>\n        <span class=\"n\">token</span> <span class=\"o\">=</span> <span class=\"n\">unescape</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_token</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;ct&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgctxt</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;mi&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgid</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;mp&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgid_plural</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;ms&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgstr</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;mx&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">msgstr_index</span><span class=\"p\">]</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;pp&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">previous_msgid_plural</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;pm&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">previous_msgid</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"k\">elif</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_state</span> <span class=\"o\">==</span> <span class=\"s1\">&#39;pc&#39;</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">current_entry</span><span class=\"o\">.</span><span class=\"n\">previous_msgctxt</span> <span class=\"o\">+=</span> <span class=\"n\">token</span>\n        <span class=\"c1\"># don&#39;t change the current state</span>\n        <span class=\"k\">return</span> <span class=\"kc\">False</span>\n<span class=\"c1\"># }}}</span>\n<span class=\"c1\"># class _MOFileParser {{{</span>\n\n\n<span class=\"k\">class</span> <span class=\"nc\">_MOFileParser</span><span class=\"p\">(</span><span class=\"nb\">object</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    A class to parse binary mo files.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">mofile</span><span class=\"p\">,</span> <span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Constructor.</span>\n\n<span class=\"sd\">        Keyword arguments:</span>\n\n<span class=\"sd\">        ``mofile``</span>\n<span class=\"sd\">            string, path to the mo file or its content</span>\n\n<span class=\"sd\">        ``encoding``</span>\n<span class=\"sd\">            string, the encoding to use, defaults to ``default_encoding``</span>\n<span class=\"sd\">            global variable (optional).</span>\n\n<span class=\"sd\">        ``check_for_duplicates``</span>\n<span class=\"sd\">            whether to check for duplicate entries when adding entries to the</span>\n<span class=\"sd\">            file (optional, default: ``False``).</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"n\">_is_file</span><span class=\"p\">(</span><span class=\"n\">mofile</span><span class=\"p\">):</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span> <span class=\"o\">=</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"n\">mofile</span><span class=\"p\">,</span> <span class=\"s1\">&#39;rb&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span> <span class=\"o\">=</span> <span class=\"n\">io</span><span class=\"o\">.</span><span class=\"n\">BytesIO</span><span class=\"p\">(</span><span class=\"n\">mofile</span><span class=\"p\">)</span>\n\n        <span class=\"n\">klass</span> <span class=\"o\">=</span> <span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;klass&#39;</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">klass</span> <span class=\"ow\">is</span> <span class=\"kc\">None</span><span class=\"p\">:</span>\n            <span class=\"n\">klass</span> <span class=\"o\">=</span> <span class=\"n\">MOFile</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span> <span class=\"o\">=</span> <span class=\"n\">klass</span><span class=\"p\">(</span>\n            <span class=\"n\">fpath</span><span class=\"o\">=</span><span class=\"n\">mofile</span><span class=\"p\">,</span>\n            <span class=\"n\">encoding</span><span class=\"o\">=</span><span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;encoding&#39;</span><span class=\"p\">,</span> <span class=\"n\">default_encoding</span><span class=\"p\">),</span>\n            <span class=\"n\">check_for_duplicates</span><span class=\"o\">=</span><span class=\"n\">kwargs</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s1\">&#39;check_for_duplicates&#39;</span><span class=\"p\">,</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n        <span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__del__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Make sure the file is closed, this prevents warnings on unclosed file</span>\n<span class=\"sd\">        when running tests with python &gt;= 3.2.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span> <span class=\"ow\">and</span> <span class=\"nb\">hasattr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"p\">,</span> <span class=\"s1\">&#39;close&#39;</span><span class=\"p\">):</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">close</span><span class=\"p\">()</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">parse</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Build the instance with the file handle provided in the</span>\n<span class=\"sd\">        constructor.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"c1\"># parse magic number</span>\n        <span class=\"n\">magic_number</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_readbinary</span><span class=\"p\">(</span><span class=\"s1\">&#39;&lt;I&#39;</span><span class=\"p\">,</span> <span class=\"mi\">4</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">magic_number</span> <span class=\"o\">==</span> <span class=\"n\">MOFile</span><span class=\"o\">.</span><span class=\"n\">MAGIC</span><span class=\"p\">:</span>\n            <span class=\"n\">ii</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&lt;II&#39;</span>\n        <span class=\"k\">elif</span> <span class=\"n\">magic_number</span> <span class=\"o\">==</span> <span class=\"n\">MOFile</span><span class=\"o\">.</span><span class=\"n\">MAGIC_SWAPPED</span><span class=\"p\">:</span>\n            <span class=\"n\">ii</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&gt;II&#39;</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid mo file, magic number is incorrect !&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">magic_number</span> <span class=\"o\">=</span> <span class=\"n\">magic_number</span>\n        <span class=\"c1\"># parse the version number and the number of strings</span>\n        <span class=\"n\">version</span><span class=\"p\">,</span> <span class=\"n\">numofstrings</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_readbinary</span><span class=\"p\">(</span><span class=\"n\">ii</span><span class=\"p\">,</span> <span class=\"mi\">8</span><span class=\"p\">)</span>\n        <span class=\"c1\"># from MO file format specs: &quot;A program seeing an unexpected major</span>\n        <span class=\"c1\"># revision number should stop reading the MO file entirely&quot;</span>\n        <span class=\"k\">if</span> <span class=\"n\">version</span> <span class=\"o\">&gt;&gt;</span> <span class=\"mi\">16</span> <span class=\"ow\">not</span> <span class=\"ow\">in</span> <span class=\"p\">(</span><span class=\"mi\">0</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">):</span>\n            <span class=\"k\">raise</span> <span class=\"ne\">IOError</span><span class=\"p\">(</span><span class=\"s1\">&#39;Invalid mo file, unexpected major revision number&#39;</span><span class=\"p\">)</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">version</span> <span class=\"o\">=</span> <span class=\"n\">version</span>\n        <span class=\"c1\"># original strings and translation strings hash table offset</span>\n        <span class=\"n\">msgids_hash_offset</span><span class=\"p\">,</span> <span class=\"n\">msgstrs_hash_offset</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_readbinary</span><span class=\"p\">(</span><span class=\"n\">ii</span><span class=\"p\">,</span> <span class=\"mi\">8</span><span class=\"p\">)</span>\n        <span class=\"c1\"># move to msgid hash table and read length and offset of msgids</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">seek</span><span class=\"p\">(</span><span class=\"n\">msgids_hash_offset</span><span class=\"p\">)</span>\n        <span class=\"n\">msgids_index</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"n\">numofstrings</span><span class=\"p\">):</span>\n            <span class=\"n\">msgids_index</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_readbinary</span><span class=\"p\">(</span><span class=\"n\">ii</span><span class=\"p\">,</span> <span class=\"mi\">8</span><span class=\"p\">))</span>\n        <span class=\"c1\"># move to msgstr hash table and read length and offset of msgstrs</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">seek</span><span class=\"p\">(</span><span class=\"n\">msgstrs_hash_offset</span><span class=\"p\">)</span>\n        <span class=\"n\">msgstrs_index</span> <span class=\"o\">=</span> <span class=\"p\">[]</span>\n        <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"n\">numofstrings</span><span class=\"p\">):</span>\n            <span class=\"n\">msgstrs_index</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_readbinary</span><span class=\"p\">(</span><span class=\"n\">ii</span><span class=\"p\">,</span> <span class=\"mi\">8</span><span class=\"p\">))</span>\n        <span class=\"c1\"># build entries</span>\n        <span class=\"n\">encoding</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">encoding</span>\n        <span class=\"k\">for</span> <span class=\"n\">i</span> <span class=\"ow\">in</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"n\">numofstrings</span><span class=\"p\">):</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">seek</span><span class=\"p\">(</span><span class=\"n\">msgids_index</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n            <span class=\"n\">msgid</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">read</span><span class=\"p\">(</span><span class=\"n\">msgids_index</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"mi\">0</span><span class=\"p\">])</span>\n\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">seek</span><span class=\"p\">(</span><span class=\"n\">msgstrs_index</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"mi\">1</span><span class=\"p\">])</span>\n            <span class=\"n\">msgstr</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">read</span><span class=\"p\">(</span><span class=\"n\">msgstrs_index</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">][</span><span class=\"mi\">0</span><span class=\"p\">])</span>\n            <span class=\"k\">if</span> <span class=\"n\">i</span> <span class=\"o\">==</span> <span class=\"mi\">0</span> <span class=\"ow\">and</span> <span class=\"ow\">not</span> <span class=\"n\">msgid</span><span class=\"p\">:</span>  <span class=\"c1\"># metadata</span>\n                <span class=\"n\">raw_metadata</span><span class=\"p\">,</span> <span class=\"n\">metadata</span> <span class=\"o\">=</span> <span class=\"n\">msgstr</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\n</span><span class=\"s1\">&#39;</span><span class=\"p\">)),</span> <span class=\"p\">{}</span>\n                <span class=\"k\">for</span> <span class=\"n\">line</span> <span class=\"ow\">in</span> <span class=\"n\">raw_metadata</span><span class=\"p\">:</span>\n                    <span class=\"n\">tokens</span> <span class=\"o\">=</span> <span class=\"n\">line</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;:&#39;</span><span class=\"p\">),</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n                    <span class=\"k\">if</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span> <span class=\"o\">!=</span> <span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">):</span>\n                        <span class=\"k\">try</span><span class=\"p\">:</span>\n                            <span class=\"n\">k</span> <span class=\"o\">=</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n                            <span class=\"n\">v</span> <span class=\"o\">=</span> <span class=\"n\">tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n                            <span class=\"n\">metadata</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">v</span><span class=\"o\">.</span><span class=\"n\">strip</span><span class=\"p\">()</span>\n                        <span class=\"k\">except</span> <span class=\"ne\">IndexError</span><span class=\"p\">:</span>\n                            <span class=\"n\">metadata</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">u</span><span class=\"p\">(</span><span class=\"s1\">&#39;&#39;</span><span class=\"p\">)</span>\n                <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">metadata</span> <span class=\"o\">=</span> <span class=\"n\">metadata</span>\n                <span class=\"k\">continue</span>\n            <span class=\"c1\"># test if we have a plural entry</span>\n            <span class=\"n\">msgid_tokens</span> <span class=\"o\">=</span> <span class=\"n\">msgid</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\0</span><span class=\"s1\">&#39;</span><span class=\"p\">))</span>\n            <span class=\"k\">if</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">msgid_tokens</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n                <span class=\"n\">entry</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_build_entry</span><span class=\"p\">(</span>\n                    <span class=\"n\">msgid</span><span class=\"o\">=</span><span class=\"n\">msgid_tokens</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">],</span>\n                    <span class=\"n\">msgid_plural</span><span class=\"o\">=</span><span class=\"n\">msgid_tokens</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">],</span>\n                    <span class=\"n\">msgstr_plural</span><span class=\"o\">=</span><span class=\"nb\">dict</span><span class=\"p\">((</span><span class=\"n\">k</span><span class=\"p\">,</span> <span class=\"n\">v</span><span class=\"p\">)</span> <span class=\"k\">for</span> <span class=\"n\">k</span><span class=\"p\">,</span> <span class=\"n\">v</span> <span class=\"ow\">in</span>\n                                       <span class=\"nb\">enumerate</span><span class=\"p\">(</span><span class=\"n\">msgstr</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\0</span><span class=\"s1\">&#39;</span><span class=\"p\">))))</span>\n                <span class=\"p\">)</span>\n            <span class=\"k\">else</span><span class=\"p\">:</span>\n                <span class=\"n\">entry</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">_build_entry</span><span class=\"p\">(</span><span class=\"n\">msgid</span><span class=\"o\">=</span><span class=\"n\">msgid</span><span class=\"p\">,</span> <span class=\"n\">msgstr</span><span class=\"o\">=</span><span class=\"n\">msgstr</span><span class=\"p\">)</span>\n            <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">append</span><span class=\"p\">(</span><span class=\"n\">entry</span><span class=\"p\">)</span>\n        <span class=\"c1\"># close opened file</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">close</span><span class=\"p\">()</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">_build_entry</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">msgid</span><span class=\"p\">,</span> <span class=\"n\">msgstr</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"n\">msgid_plural</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">,</span>\n                     <span class=\"n\">msgstr_plural</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"n\">msgctxt_msgid</span> <span class=\"o\">=</span> <span class=\"n\">msgid</span><span class=\"o\">.</span><span class=\"n\">split</span><span class=\"p\">(</span><span class=\"n\">b</span><span class=\"p\">(</span><span class=\"s1\">&#39;</span><span class=\"se\">\\x04</span><span class=\"s1\">&#39;</span><span class=\"p\">))</span>\n        <span class=\"n\">encoding</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">instance</span><span class=\"o\">.</span><span class=\"n\">encoding</span>\n        <span class=\"k\">if</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">msgctxt_msgid</span><span class=\"p\">)</span> <span class=\"o\">&gt;</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n            <span class=\"n\">kwargs</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n                <span class=\"s1\">&#39;msgctxt&#39;</span><span class=\"p\">:</span> <span class=\"n\">msgctxt_msgid</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">),</span>\n                <span class=\"s1\">&#39;msgid&#39;</span><span class=\"p\">:</span> <span class=\"n\">msgctxt_msgid</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">),</span>\n            <span class=\"p\">}</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">kwargs</span> <span class=\"o\">=</span> <span class=\"p\">{</span><span class=\"s1\">&#39;msgid&#39;</span><span class=\"p\">:</span> <span class=\"n\">msgid</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">)}</span>\n        <span class=\"k\">if</span> <span class=\"n\">msgstr</span><span class=\"p\">:</span>\n            <span class=\"n\">kwargs</span><span class=\"p\">[</span><span class=\"s1\">&#39;msgstr&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">msgstr</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">msgid_plural</span><span class=\"p\">:</span>\n            <span class=\"n\">kwargs</span><span class=\"p\">[</span><span class=\"s1\">&#39;msgid_plural&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">msgid_plural</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">msgstr_plural</span><span class=\"p\">:</span>\n            <span class=\"k\">for</span> <span class=\"n\">k</span> <span class=\"ow\">in</span> <span class=\"n\">msgstr_plural</span><span class=\"p\">:</span>\n                <span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">msgstr_plural</span><span class=\"p\">[</span><span class=\"n\">k</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">decode</span><span class=\"p\">(</span><span class=\"n\">encoding</span><span class=\"p\">)</span>\n            <span class=\"n\">kwargs</span><span class=\"p\">[</span><span class=\"s1\">&#39;msgstr_plural&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">msgstr_plural</span>\n        <span class=\"k\">return</span> <span class=\"n\">MOEntry</span><span class=\"p\">(</span><span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">_readbinary</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">fmt</span><span class=\"p\">,</span> <span class=\"n\">numbytes</span><span class=\"p\">):</span>\n<span class=\"w\">        </span><span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">        Private method that unpack n bytes of data using format &lt;fmt&gt;.</span>\n<span class=\"sd\">        It returns a tuple or a mixed value if the tuple length is 1.</span>\n<span class=\"sd\">        &quot;&quot;&quot;</span>\n        <span class=\"nb\">bytes</span> <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">fhandle</span><span class=\"o\">.</span><span class=\"n\">read</span><span class=\"p\">(</span><span class=\"n\">numbytes</span><span class=\"p\">)</span>\n        <span class=\"n\">tup</span> <span class=\"o\">=</span> <span class=\"n\">struct</span><span class=\"o\">.</span><span class=\"n\">unpack</span><span class=\"p\">(</span><span class=\"n\">fmt</span><span class=\"p\">,</span> <span class=\"nb\">bytes</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">tup</span><span class=\"p\">)</span> <span class=\"o\">==</span> <span class=\"mi\">1</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> <span class=\"n\">tup</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n        <span class=\"k\">return</span> <span class=\"n\">tup</span>\n<span class=\"c1\"># }}}</span>\n</pre></div>", "current_page_name": "_modules/polib", "sidebars": ["localtoc.html", "relations.html", "sourcelink.html", "searchbox.html"], "alabaster_version": "1.0.0", "alabaster_version_info": [1, 0, 0]}