ó
µ”ù\c           @   so   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 „  Z e d „ Z d S(	   sÝ   
    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.
iÿÿÿÿN(   t   Catalogt   Message(   t
   range_typet   array_tobytesIÞ•    I•Þ    c         C   s]  t  ƒ  } i  } t |  d d ƒ } |  j ƒ  } t | ƒ } t j } | d | d  ƒ d } | t k r | d | d d !ƒ \ } }	 }
 } d } nI | t k rÇ | d	 | d d !ƒ \ } }	 }
 } d
 } n t d d | ƒ ‚ xnt	 d |	 ƒ D]]} | | | |
 |
 d !ƒ \ } } | | } | | | | | d !ƒ \ } } | | } | | k  rx| | k  rx| | | !} | | | !} n t d d | ƒ ‚ | d k r<d } } x™ | j ƒ  D]ˆ } | j ƒ  } | sËq­n  d | k r| j d d ƒ \ } } | j ƒ  j ƒ  } } | j ƒ  | | <q­| r­| | c d | 7<q­q­Wn  d | k r`| j d ƒ \ } } n d } d | k ré| j d ƒ } | j d ƒ } | j rg  | D] } | j | j ƒ ^ q } g  | D] } | j | j ƒ ^ qÅ} qn0 | j r| j | j ƒ } | j | j ƒ } n  t | | d | ƒ| | <|
 d 7}
 | d 7} qé W| j ƒ  | _ | S(   sa  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.
    t   namet    s   <Ii   i    s   <4Ii   s   <IIs   >4Is   >IIs   Bad magic numberi   s   File is corruptt   :i   s   
s   s    t   contextN(   R    t   getattrt   readt   lent   structt   unpackt   LE_MAGICt   BE_MAGICt   IOErrorR   t   Nonet
   splitlinest   stript   splitt   lowert   charsett   decodeR   t   itemst   mime_headers(   t   fileobjt   catalogt   headerst   filenamet   buft   buflenR   t   magict   versiont   msgcountt   origidxt   transidxt   iit   it   mlent   mofft   mendt   tlent   tofft   tendt   msgt   tmsgt   lastkeyt   keyt   itemt   valuet   ctxtt   x(    (    s4   lib/python2.7/site-packages/babel/messages/mofile.pyt   read_mo   sh    
		"	"	 
 

	%+	
c      
   C   sÂ  t  | ƒ } g  | d D]% } | j r | s6 | j r | ^ q | d )| j ƒ  d } } g  } x| D]w} | j r7d j g  | j D] }	 |	 j | j ƒ ^ q† ƒ }	 g  }
 xV t	 | j ƒ D]E \ } } | sõ |
 j
 | j t t | ƒ d ƒ ƒ q½ |
 j
 | ƒ q½ Wd j g  |
 D] } | j | j ƒ ^ qƒ } n* | j j | j ƒ }	 | j j | j ƒ } | j r‘d j | j j | j ƒ |	 g ƒ }	 n  | j
 t | ƒ t |	 ƒ t | ƒ t | ƒ f ƒ | |	 d 7} | | d 7} qg Wd d t | ƒ } | t | ƒ } g  } g  } xB | D]: \ } } } } | | | | g 7} | | | | g 7} qW| | } |  j t j d t d	 t | ƒ d d t | ƒ d
 d	 d	 ƒ t t j d | ƒ ƒ | | ƒ d S(   s³  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
    i   R   s    s   i   i   i   t   Iiiiiiii    i   R%   Ni   i   i   (   t   listt   stringt   fuzzyt   sortt   pluralizablet   joint   idt   encodeR   t	   enumeratet   appendt   mint   intR   R
   t   writeR   t   packR   R   t   array(   R   R   t	   use_fuzzyt   messagest   mt   idst   strst   offsetst   messaget   msgidt   msgstrst   idxR7   t   msgstrt   keystartt
   valuestartt   koffsetst   voffsetst   o1t   l1t   o2t   l2(    (    s4   lib/python2.7/site-packages/babel/messages/mofile.pyt   write_mok   sF    0)

	+&+	1
(   t   __doc__RD   R   t   babel.messages.catalogR    R   t   babel._compatR   R   R   R   R4   t   FalseRX   (    (    (    s4   lib/python2.7/site-packages/babel/messages/mofile.pyt   <module>
   s   	T