o
    VÛë\$  ã                   @   sR   d Z ddlZddlZddlmZmZ ddlmZmZ dZ	dZ
dd„ Zdd
d„ZdS )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)ÚCatalogÚMessage)Ú
range_typeÚarray_tobytesl   Þ* l   •$< c                    sZ  t ƒ ‰ i }t| ddƒ}|  ¡ }t|ƒ}tj}|d|dd… ƒd }|tkr5|d|dd… ƒ\}}}	}
d	}n|tkrI|d
|dd… ƒ\}}}	}
d}ntdd|ƒ‚t	d|ƒD ]Ñ}||||	|	d … ƒ\}}|| }||||
|
d … ƒ\}}|| }||k r�||k r�|||… }|||… }ntdd|ƒ‚|dkrÐd }}| 
¡ D ]0}| ¡ }|s¨qŸd|v rÃ| dd¡\}}| ¡  ¡  }}| ¡ ||< qŸ|rÏ||  d| 7  < qŸd|v rÜ| d¡\}}nd}d|v �r| d¡}| d¡}ˆ j�r‡ fdd„|D ƒ}‡ fdd„|D ƒ}nˆ j�r| ˆ j¡}| ˆ j¡}t|||d�ˆ |< |	d7 }	|
d7 }
qT| ¡ ˆ _ˆ S )aa  Read 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é   r   z<4Ié   z<IIz>4Iz>IIzBad magic numberé   zFile is corruptó   :é   ó   
ó   ó    c                    ó   g | ]}|  ˆ j¡‘qS © ©ÚdecodeÚcharset©Ú.0Úx©Úcatalogr   ú7/usr/lib/python3/dist-packages/babel/messages/mofile.pyÚ
<listcomp>[   ó    zread_mo.<locals>.<listcomp>c                    r   r   r   r   r   r   r   r   \   r   )Úcontext)r   ÚgetattrÚreadÚlenÚstructÚunpackÚLE_MAGICÚBE_MAGICÚIOErrorr   Ú
splitlinesÚstripÚsplitÚlowerr   r   r   ÚitemsÚmime_headers)ÚfileobjÚheadersÚfilenameÚbufÚbuflenr"   ÚmagicÚversionÚmsgcountÚorigidxÚtransidxÚiiÚiÚmlenÚmoffÚmendÚtlenÚtoffÚtendÚmsgÚtmsgÚlastkeyÚkeyÚitemÚvalueÚctxtr   r   r   Úread_mo   sl   
€


€

rE   Fc                    sà  t ˆ ƒ}‡fdd„|dd… D ƒ|dd…< | ¡  d }}g }|D ]z}|jrad ‡ fdd„|jD ƒ¡}g }	t|jƒD ]\}
}|sN|	 |jtt	|
ƒdƒ ¡ q:|	 |¡ q:d ‡ fdd„|	D ƒ¡}n|j 
ˆ j¡}|j 
ˆ j¡}|jr~d	 |j 
ˆ j¡|g¡}| t|ƒt|ƒt|ƒt|ƒf¡ ||d 7 }||d 7 }q!d
dt|ƒ  }|t|ƒ }g }g }|D ]\}}}}|||| g7 }|||| g7 }q°|| }|  t dtdt|ƒd
d
t|ƒd  dd¡tt d|¡ƒ | | ¡ dS )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 babel._compat 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
    c                    s    g | ]}|j rˆ s|js|‘qS r   )ÚstringÚfuzzy)r   Úm)Ú	use_fuzzyr   r   r   œ   s    ÿÿ
ÿzwrite_mo.<locals>.<listcomp>r   Nó    r   c                    r   r   ©Úencoder   )r   Úmsgidr   r   r   r   §   ó    ÿc                    r   r   rK   )r   Úmsgstrr   r   r   r   °   rN   r   é   é   ÚIiiiiiir   r
   r7   )ÚlistÚsortÚpluralizableÚjoinÚidÚ	enumeraterF   ÚappendÚminÚintrL   r   r   r    Úwriter!   Úpackr#   r   Úarray)r,   r   rI   ÚmessagesÚidsÚstrsÚoffsetsÚmessagerM   ÚmsgstrsÚidxrF   rO   ÚkeystartÚ
valuestartÚkoffsetsÚvoffsetsÚo1Úl1Úo2Úl2r   )r   rI   r   Úwrite_mok   sb   0"ÿ
ÿÿ"
úùù
ùrn   )F)Ú__doc__r^   r!   Úbabel.messages.catalogr   r   Úbabel._compatr   r   r#   r$   rE   rn   r   r   r   r   Ú<module>   s   
T