3

X'ícÎã@sBdZddlZddlZddlmZmZdZdZdd„Zdd	d
„Z	dS)zÝ
    babel.messages.mofile
    ~~~~~~~~~~~~~~~~~~~~~

    Writing of files in the ``gettext`` MO (machine object) format.

    :copyright: (c) 2013-2022 by the Babel Team.
    :license: BSD, see LICENSE for more details.
éN)ÚCatalogÚMessagelÞ*l•$<csxtƒ‰i}t|ddƒ}|jƒ}t|ƒ}tj}|d|dd…ƒd}|tkrj|d|dd…ƒ\}}}	}
d	}n4|tkr’|d
|dd…ƒ\}}}	}
d}ntdd|ƒ‚xÈt	d|ƒD]¸}||||	|	d
…ƒ\}
}||
}||||
|
d
…ƒ\}}||}||kr$||kr$|||…}|||…}ntdd|ƒ‚|
dkrºd}}xv|j
ƒD]j}|jƒ}|sbqLd|krš|jddƒ\}}|jƒj
ƒ}}|jƒ||<n|rL||d|7<qLWd|krÔ|jdƒ\}}nd}d|kr$|jdƒ}|jdƒ}ˆjrD‡fdd„|Dƒ}‡fdd„|Dƒ}n ˆjrD|jˆjƒ}|jˆjƒ}t|||dˆ|<|	d
7}	|
d
7}
q¬W|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.
    ÚnameÚz<INérz<4Iéz<IIz>4Iz>IIzBad magic numberézFile is corruptó:éó
óócsg|]}|jˆjƒ‘qS©)ÚdecodeÚcharset)Ú.0Úx)Úcatalogrú6/tmp/pip-build-gk9425m9/babel/babel/messages/mofile.pyú
<listcomp>Yszread_mo.<locals>.<listcomp>csg|]}|jˆjƒ‘qSr)rr)rr)rrrrZs)Úcontext)rÚgetattrÚreadÚlenÚstructÚunpackÚLE_MAGICÚBE_MAGICÚOSErrorÚrangeÚ
splitlinesÚstripÚsplitÚlowerrrrÚitemsZmime_headers)ÚfileobjÚheadersÚfilenameÚbufÚbuflenrÚmagicÚversionÚmsgcountZorigidxÚtransidxÚiiÚiÚmlenÚmoffÚmendÚtlenÚtoffÚtendÚmsgÚtmsgZlastkeyÚkeyÚitemÚvalueZctxtr)rrÚread_mosh







r;Fc
sötˆƒ}‡fdd„|dd…Dƒ|dd…<|jƒd}}g}x|D]ú}|jrÊdj‡fdd„|jDƒƒ}g}	x@t|jƒD]2\}
}|s¢|	j|jtt	|
ƒdƒƒqz|	j|ƒqzWdj‡fdd„|	Dƒƒ}n|jj
ˆjƒ}|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}qnW||}|jtjd
tdt
|ƒddt
|ƒdddƒtjjtjd|ƒƒ||ƒdS)a¨Write 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 io import BytesIO

    >>> catalog = Catalog(locale='en_US')
    >>> catalog.add('foo', 'Voh')
    <Message ...>
    >>> catalog.add((u'bar', u'baz'), (u'Bahr', u'Batz'))
    <Message ...>
    >>> catalog.add('fuz', 'Futz', flags=['fuzzy'])
    <Message ...>
    >>> catalog.add('Fizz', '')
    <Message ...>
    >>> catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
    <Message ...>
    >>> 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|]}|jrˆs|jr|‘qSr)ÚstringZfuzzy)rÚm)Ú	use_fuzzyrrršszwrite_mo.<locals>.<listcomp>r
Nór
csg|]}|jˆjƒ‘qSr)Úencoder)rÚmsgid)rrrr¦scsg|]}|jˆjƒ‘qSr)r@r)rÚmsgstr)rrrr¯sréréZIiiiiiirrr/érErE)ÚlistÚsortZpluralizableÚjoinÚidÚ	enumerater<ÚappendÚminÚintr@rrrÚwriterÚpackrÚarrayÚtobytes)r%rr>ÚmessagesÚidsÚstrsÚoffsetsÚmessagerAZmsgstrsÚidxr<rBZkeystartZ
valuestartZkoffsetsZvoffsetsZo1Úl1Zo2Úl2r)rr>rÚwrite_moisD0""rZ)F)
Ú__doc__rPrZbabel.messages.catalogrrrrr;rZrrrrÚ<module>	sT