B
    }<[                 @   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-2018 by the Babel Team.
    :license: BSD, see LICENSE for more details.
    N)CatalogMessage)
range_typearray_tobytesl   * l   $< c                sx  t   i }t| dd}|  }t|}tj}|d|dd d }|tkrj|d|dd \}}}	}
d	}n4|tkr|d
|dd \}}}	}
d}ntdd|xt	d|D ]}||||	|	d  \}}|| }||||
|
d  \}}|| }||k r$||k r$||| }||| }ntdd||dkrd }}xv|
 D ]j}| }|sbqLd|kr|dd\}}|   }}| ||< n|rL||  d| 7  < qLW d|kr|d\}}nd}d|kr$|d}|d} jrD fdd|D } fdd|D }n  jrD| j}| j}t|||d |< |	d7 }	|
d7 }
qW |  _ 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                s   g | ]}|  jqS  )decodecharset).0x)catalogr   Jlib/python3.7/site-packages/Babel-2.6.0-py3.7.egg/babel/messages/mofile.py
<listcomp>[   s    zread_mo.<locals>.<listcomp>c                s   g | ]}|  jqS r   )r   r   )r   r   )r   r   r   r   \   s    )context)r   getattrreadlenstructunpackLE_MAGICBE_MAGICIOErrorr   
splitlinesstripsplitlowerr   r   r   itemsZmime_headers)fileobjZheadersfilenameZbufZbuflenr   magicversionZmsgcountZorigidxZtransidxZiiiZmlenZmoffZmendZtlenZtoffZtendmsgZtmsgZlastkeykeyitemvalueZctxtr   )r   r   read_mo   sh    







r/   Fc                s
  t  }|s*dd |dd D |dd< |  d }}g }x|D ]}|jrd fdd|jD }g }	x@t|jD ]2\}
}|s|	|jtt	|
d  q||	| q|W d fdd|	D }n2|j
 j}|js|j
 j}n|j
 j}|jrd	|j
 j|g}|t|t|t|t|f ||d 7 }||d 7 }qFW d
dt|  }|t| }g }g }x6|D ].\}}}}|||| g7 }|||| g7 }qW || }| 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   s   g | ]}|j s|qS r   )Zfuzzy)r   mr   r   r   r      s    zwrite_mo.<locals>.<listcomp>r   N    r   c                s   g | ]}|  jqS r   )encoder   )r   msgid)r   r   r   r      s    c                s   g | ]}|  jqS r   )r2   r   )r   msgstr)r   r   r   r      s    r         ZIiiiiiir   r
   r*   )listsortZpluralizablejoinid	enumeratestringappendminintr2   r   r   r   writer   Zpackr   r   array)r&   r   Z	use_fuzzyZmessagesZidsZstrsZoffsetsmessager3   Zmsgstrsidxr<   r4   ZkeystartZ
valuestartZkoffsetsZvoffsetsZo1l1Zo2l2r   )r   r   write_mok   sJ    0"rF   )F)__doc__rA   r   Zbabel.messages.catalogr   r   Zbabel._compatr   r   r   r   r/   rF   r   r   r   r   <module>
   s   T