3 4\0X~@sRdZddlZddlZddlmZmZddlmZmZdZ dZ ddZ d d d Z dS) z babel.messages.mofile ~~~~~~~~~~~~~~~~~~~~~ Writing of files in the ``gettext`` MO (machine object) format. :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. N)CatalogMessage) range_type array_tobytesl*l$<csxti}t|dd}|j}t|}tj}|d|ddd}|tkrj|d|dd\}}} } d } n4|tkr|d |dd\}}} } d } n tdd |xt d|D]} || || | d \} }|| }|| || | d \}}||}||kr$||kr$|||}|||}n tdd|| dkrd}}xv|j D]j}|j }|sbqLd|kr|j dd\}}|j j }}|j ||<n|rL||d|7<qLWd|kr|j d\}}nd}d|kr$|j d}|j d}jrDfdd|D}fdd|D}n jrD|jj}|jj}t|||d|<| d 7} | d 7} qW|j_S)aaRead a binary MO file from the given file-like object and return a corresponding `Catalog` object. :param fileobj: the file-like object to read the MO file from :note: The implementation of this function is heavily based on the ``GNUTranslations._parse`` method of the ``gettext`` module in the standard library. namez4Iz>IIzBad magic numberzFile is corrupt: csg|]}|jjqS)decodecharset).0x)catalogr/usr/lib/python3.6/mofile.py [szread_mo..csg|]}|jjqSr)rr)rr)rrrr\s)context)rgetattrreadlenstructunpackLE_MAGICBE_MAGICIOErrorr splitlinesstripsplitlowerrrritemsZ mime_headers)fileobjZheadersfilenameZbufZbuflenrmagicversionZmsgcountZorigidxZtransidxZiiiZmlenZmoffZmendZtlenZtoffZtendmsgZtmsgZlastkeykeyitemvalueZctxtr)rrread_mosh               r/Fc s t}|s*dd|ddD|dd<|jd}}g}x|D]}|jrdjfdd|jD}g} x@t|jD]2\} } | s| j|jtt | dq|| j| q|Wdjfdd| D} n2|jj j }|js|jj j } n|jj j } |j rd j|j j j |g}|jt |t |t |t | f||d7}|| d7}qFWdd t |} | t |}g}g}x6|D].\}}}}|||| g7}||||g7}qW||}|jtjd tdt |ddt |dddttjd|||dS)aWrite a catalog to the specified file-like object using the GNU MO file format. >>> import sys >>> from babel.messages import Catalog >>> from gettext import GNUTranslations >>> from babel._compat import BytesIO >>> catalog = Catalog(locale='en_US') >>> catalog.add('foo', 'Voh') >>> catalog.add((u'bar', u'baz'), (u'Bahr', u'Batz')) >>> catalog.add('fuz', 'Futz', flags=['fuzzy']) >>> catalog.add('Fizz', '') >>> catalog.add(('Fuzz', 'Fuzzes'), ('', '')) >>> buf = BytesIO() >>> write_mo(buf, catalog) >>> x = buf.seek(0) >>> translations = GNUTranslations(fp=buf) >>> if sys.version_info[0] >= 3: ... translations.ugettext = translations.gettext ... translations.ungettext = translations.ngettext >>> translations.ugettext('foo') u'Voh' >>> translations.ungettext('bar', 'baz', 1) u'Bahr' >>> translations.ungettext('bar', 'baz', 2) u'Batz' >>> translations.ugettext('fuz') u'fuz' >>> translations.ugettext('Fizz') u'Fizz' >>> translations.ugettext('Fuzz') u'Fuzz' >>> translations.ugettext('Fuzzes') u'Fuzzes' :param fileobj: the file-like object to write to :param catalog: the `Catalog` instance :param use_fuzzy: whether translations marked as "fuzzy" should be included in the output cSsg|]}|js|qSr)Zfuzzy)rmrrrrszwrite_mo..r Nrcsg|]}|jjqSr)encoder)rmsgid)rrrrscsg|]}|jjqSr)r2r)rmsgstr)rrrrsrrZIiiiiiirr r*r7r7)listsortZ pluralizablejoinid enumeratestringappendminintr2rrrwriterpackrrarray)r&rZ use_fuzzyZmessagesZidsZstrsZoffsetsmessager3Zmsgstrsidxr=r4ZkeystartZ valuestartZkoffsetsZvoffsetsZo1l1Zo2l2r)rrwrite_moksJ0"  rH)F) __doc__rCrZbabel.messages.catalogrrZ babel._compatrrrrr/rHrrrr sT