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rd }}|
 D ]0}| }|sqd|v r|dd\}}|   }}| ||< q|r||  d| 7  < qd|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