o V\$@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-2019 by the Babel Team. :license: BSD, see LICENSE for more details. N)CatalogMessage) range_type array_tobytesl*l$<csZti}t|dd}|}t|}tj}|d|ddd}|tkr5|d|dd\}}} } d } n|tkrI|d |dd\}}} } d } ntdd |t d|D]} || || | d \} }|| }|| || | d \}}||}||kr||kr|||}|||}ntdd|| dkrd}}| D]0}| }|sqd|vr| dd\}}| }}| ||<q|r||d|7<qd|vr| d\}}nd}d|vr| d}| d}jrfdd|D}fdd|D}njr|j}|j}t|||d|<| d 7} | d 7} qT|_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: cg|]}|jqSdecodecharset.0xcatalogr7/usr/lib/python3/dist-packages/babel/messages/mofile.py [zread_mo..crrrrrrrr\r)context)rgetattrreadlenstructunpackLE_MAGICBE_MAGICIOErrorr splitlinesstripsplitlowerrrritems mime_headers)fileobjheadersfilenamebufbuflenr"magicversionmsgcountorigidxtransidxiiimlenmoffmendtlentofftendmsgtmsglastkeykeyitemvaluectxtrrrread_mosl             rEFc st}fdd|ddD|dd<|d}}g}|D]z}|jradfdd|jD}g} t|jD]\} } | sN| |jtt | dq:| | q:dfdd| D} n|j j }|j j } |j r~d |j j |g}|t |t |t |t | f||d7}|| d7}q!d d t |} | t |}g}g}|D]\}}}}|||| g7}||||g7}q||}|td td t |d d t |dd d ttd|||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 cs g|] }|jrs |js|qSr)stringfuzzy)rm) use_fuzzyrrrs zwrite_mo..r Nrcrrencoder)rmsgidrrrr crrrK)rmsgstrrrrrrNrIiiiiiirr r7)listsort pluralizablejoinid enumeraterFappendminintrLrrr writer!packr#rarray)r,rrImessagesidsstrsoffsetsmessagerMmsgstrsidxrFrOkeystart valuestartkoffsetsvoffsetso1l1o2l2r)rrIrwrite_moksb0"  "    rn)F) __doc__r^r!babel.messages.catalogrr babel._compatrrr#r$rErnrrrrs T