a `$@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-2021 by the Babel Team. :license: BSD, see LICENSE for more details. N)CatalogMessage) range_type array_tobytesl*l$<csnti}t|dd}|}t|}tj}|d|ddd}|tkrj|d|dd\}}} } d } n4|tkr|d |dd\}}} } d } n tdd |t d|D]} || || | d \} }|| }|| || | d \}}||}||kr ||kr |||}|||}n tdd|| dkrd}}| D]j}| }|s\qFd|vr| dd\}}| }}| ||<n|rF||d|7<qFd|vr| d\}}nd}d|vr| d}| d}jr<fdd|D}fdd|D}n jr<|j}|j}t|||d|<| d 7} | d 7} q|_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|]}|jqSdecodecharset.0xcatalogr9/usr/lib/python3.9/site-packages/babel/messages/mofile.py [zread_mo..csg|]}|jqSrrrrrrr\r)context)rgetattrreadlenstructunpackLE_MAGICBE_MAGICIOErrorr splitlinesstripsplitlowerrrritemsZ mime_headers)fileobjZheadersfilenamebufbuflenr!magicversionmsgcountZorigidxtransidxiiimlenmoffmendtlentofftendmsgtmsgZlastkeykeyitemvalueZctxtrrrread_mosh                r?Fc st}fdd|ddD|dd<|d}}g}|D]}|jrdfdd|jD}g} t|jD]2\} } | s| |jtt | dqt| | qtdfdd| D} n|j j }|j j } |j rd |j j |g}|t |t |t |t | f||d7}|| d7}qBd 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)stringZfuzzy)rm) use_fuzzyrrrszwrite_mo..r Nrrcsg|]}|jqSrencoder)rmsgidrrrrscsg|]}|jqSrrC)rmsgstrrrrrsrZIiiiiiirr r3)listsortZ pluralizablejoinid enumerater@appendminintrDrrrwriter packr"rarray)r*rrBmessagesZidsZstrsZoffsetsmessagerEZmsgstrsidxr@rFZkeystartZ valuestartZkoffsetsZvoffsetsZo1l1Zo2l2r)rrBrwrite_moksb0"  "   rY)F) __doc__rSr Zbabel.messages.catalogrrZ babel._compatrrr"r#r?rYrrrrs T