m
թHc           @   s  d  k  Z  d  k Z d  k Z d  k Z d  k Z d k l Z d k l Z d k l	 Z	 d k
 l Z d k l Z d k l Z d k l Z d k l Z d	 k l Z d  k Z d
 k l Z d k l Z d k l Z d k l Z d k l Z l  Z  d k l! Z! d k" l# Z# d k$ l% Z% d k$ l& Z& d k' l( Z( d k l) Z) d k l Z d k l* Z* d k l+ Z+ d k l, Z, d k- l. Z. d k/ l/ Z/ d k0 l1 Z1 d k2 l2 Z2 d k3 l4 Z4 d k3 l5 Z5 d k6 l7 Z7 l8 Z8 d Z9 g  Z: d  Z; e i< d! h  d" e; < Z= e i< d# h  d" e; < Z> e? e i@ iA d$ d%  iB d&   ZC e8   ZD eD iE ZF e8   ZG eG iE ZH d' e f d(     YZI e! eI  d) e# f d*     YZJ e! eJ  d  S(+   N(   s   ST_MTIME(   s   getGlobalSiteManager(   s   queryUtility(   s	   deprecate(   s   GettextMessageCatalog(   s   TranslationDomain(   s   ITranslationDomain(   s
   implements(   s   IBrowserRequest(   s   Base(   s
   aq_acquire(   s   ImplicitAcquisitionWrapper(   s   ClassSecurityInfo(   s   views   view_management_screens(   s   InitializeClass(   s   Folder(   s   _checkLanguage(   s   PTS_LANGUAGES(   s   LazyGettextMessageCatalog(   s   BrokenMessageCatalog(   s   translationRegistry(   s   rtlRegistry(   s
   getMessage(   s
   negotiator(   s   Domain(   s   IPlacelessTranslationService(   s   memoize(   s   Msgfmt(   s   PoSyntaxError(   s   logs   Registryt   _pts_is_rtls   [a-zA-Z][-a-zA-Z0-9_]*s   (?<!\$)(\$(?:%(n)s|{%(n)s}))t   ns   %(n)st   LANGUAGE_FALLBACKSt   ent    t
   PTSWrapperc           B   s  t  Z d  Z e   Z d   Z e i d  d   Z e i e	 d  e
 e
 e
 e
 d  Z e i e	 d  e d  e
 e
 e
 e
 d   Z e i e	 d	  d
   Z e i e	 d  e
 d  Z e i d  d   Z e i d  e d  d    Z d   Z RS(   s^   
    Wrap the persistent PTS since persistent
    objects can't be passed around threads.
    c         C   s   | i   |  _ d  S(   N(   t   servicet   getPhysicalPatht   selft   _path(   R   R   (    (    tf   /home2/kennisonb/webapps/zope/Zope/Products/PlacelessTranslationService/PlacelessTranslationService.pyt   __init__C   s    t   loadc         C   s1   y | i   } Wn d  Sn X| i |  i d   S(   N(   t   contextt   getPhysicalRoott   roott   Nonet   unrestrictedTraverseR   R	   (   R   R   R   (    (    R
   R   H   s
      t	   translatec         C   s:   |  i |  } | p | Sn | i | | | | | |  S(   s4   
        Translate a message using Unicode.
        N(
   R   R   R   R   t   defaultR   t   domaint   msgidt   mappingt   target_language(   R   R   R   R   R   R   R   R   (    (    R
   R   P   s
     t
   utranslates}   The utranslate method of the PTS is deprecated and will be removed in the next PTS release. Use the translate method instead.c         C   s:   |  i |  } | p | Sn | i | | | | | |  S(   s5   
        Translate a message using Unicode..
        N(
   R   R   R   R   R   R   R   R   R   R   (   R   R   R   R   R   R   R   R   (    (    R
   R   [   s
     t   getLanguageNamec         C   s   |  i |  } | i |  S(   N(   R   R   R   R   R   t   code(   R   R   R   R   (    (    R
   R   i   s    t   getLanguagesc         C   s   |  i |  } | i |  S(   N(   R   R   R   R   R   R   (   R   R   R   R   (    (    R
   R   n   s    t   negotiate_languagec         C   s"   |  i |  } | i | i |  S(   N(   R   R   R   R   R   t   REQUESTR   (   R   R   R   R   (    (    R
   R   s   s    t   isRTLs   The isRTL method of the PTS is deprecated and will be removed in the next PTS release. Use the information found in the Zope3 locale instead.c         C   s7   |  i |  } | d  j o t Sn | i | i |  S(   N(	   R   R   R   R   R   t   FalseR   R   R   (   R   R   R   R   (    (    R
   R   x   s    c         C   s   d d i  |  i  S(   s0   
        Return a string representation
        s   <PTSWrapper for %s>t   /N(   t   joinR   R	   (   R   (    (    R
   t   __repr__   s     (   t   __name__t
   __module__t   __doc__t   ClassSecurityInfot   securityR   t   declarePrivateR   t   declareProtectedt   viewR   R   t	   deprecateR   t   declarePublicR   R   R   R   R"   (    (    (    R
   R   ;   s$    			
!		
t   PlacelessTranslationServicec           B   sM  t  Z d  Z e e  d Z Z d Z d1 Z f  Z	 e
   Z d e d  Z d	   Z d
   Z d   Z e i d  e e d  Z e e d  Z d   Z d   Z d   Z e i e d  e d  Z d d  Z e i d  d   Z e i d  d   Z e i d  e e d   Z e i d  e d  Z e i e  d  d   Z! e i e  d  e d   Z" e i e  d!  e# d"  d#    Z$ e i e  d$  e# d%  e e e e d&   Z% e i e  d'  e e e e d(  Z& e i d)  e d*    Z' e i e  d+  d,   Z( e i d-  d.   Z) e i e d/  d0   Z* RS(2   s+   
    The Placeless Translation Service
    s   Placeless Translation ServicesA   misc_/PlacelessTranslationService/PlacelessTranslationService.pngi   i   i   i    t   globalc         C   sE   |  i |  _ | |  _ h  } h  } | d  j o
 t	 } n | |  _
 d  S(   N(   R   t   _class_versiont   _instance_versiont   default_domaint   _domaint   catalogRegistryt   fbcatalogRegistryt	   fallbacksR   R   t
   _fallbacks(   R   R1   R5   R3   R4   (    (    R
   R      s    	
c         C   s   t  | t  o d  Sn | i   } | i   } t i | | f g   i	 | i
    x9 | i   D]+ } t i | | f g   i	 | i
    qb Wd |  _ d  S(   Ni   (   t
   isinstancet   catalogt   BrokenMessageCatalogt	   getDomainR   t   getLanguaget   languageR3   t
   setdefaultt   appendt   getIdentifiert   getOtherLanguagest   langR4   R   t
   _p_changed(   R   R8   RA   R   R<   (    (    R
   t   _registerMessageCatalog   s    % )c         C   sc   x\ | i   D]N \ } } y | i | i    Wn t j
 o
 q n X| p | | =q q Wd  S(   N(   t   clistt   itemst   keyt   combot   removeR8   R?   t
   ValueError(   R   R8   RD   RF   RG   (    (    R
   t   _unregister_inner   s     c         C   s-   |  i | t  |  i | t  d |  _ d  S(   Ni   (   R   RJ   R8   R3   R4   RB   (   R   R8   (    (    R
   t   _unregisterMessageCatalog   s    t   calculatePoIdc         C   s  t  i i t i d  t  i }	 t  i i t i d  t  i } t  i i t i d  } t  i i t i d  } t } | i |	  o | t |	  }
 n| i |  o | t |  }
 n | i |  o | t |  }
 t } n | i |  o | t |  }
 t } n | i t  i  } y | i d  } WnR t j
 oF y | i d  } Wqt j
 o t d | | f   qXn Xt  i i | | d | |  }
 | p) |
 i t  i  } d i | d   } n d } | o | o d	 | | | f Sn d
 | | f Sd S(   s   Calulate the po id
        t   Productst   i18nt   localess1   Invalid po path %s for %s. That should not happeni   t   .i   t   GlobalCatalogss   %s-%s-%s.pos   %s-%sN(   t   ost   pathR!   t   Globalst   INSTANCE_HOMEt   sept   iPatht   SOFTWARE_HOMEt   sPatht   gci18nNPatht	   gcLocPathR   t   isGlobalCatalogt   popatht
   startswitht   lent   Truet   splitt   pt   indext   idxRI   t   OSErrort   namet   preR<   R   (   R   Rf   R]   R<   R   Rg   RY   Rb   R[   RW   RS   R\   RZ   Rd   (    (    R
   RL      s@     

 !c   	      C   sg  |  i | | d | d | } y |  i | d  Wn | } n Xt i	 i
 | |  } |  i | t  } y+ t | t  o |  i |  t } n Wn n Xy> | t j o  |  i t | | | |   n |  i |  Wn t j
 o" y |  i |  WqcqcXnX t j
 o
   nC t i   } t d t i | |  |  i t | | |   n Xd S(   s2   
        create catalog instances in ZODB
        R<   R   i   s   Message Catalog has errorsN(   R   RL   Rf   R]   R<   R   t   idt   _checkIdRR   RS   R!   t   pofilet   _getObt   _markert   obR7   R9   t
   _delObjectt
   addCatalogt   GettextMessageCatalogt   reloadCatalogt   IOErrort   KeyboardInterruptt   syst   exc_infot   exct   logt   loggingt   WARNING(	   R   Rf   R]   R<   R   Rm   Rj   Rh   Rv   (    (    R
   t   _load_catalog_file   s:     
 c         C   s  t  d | t i  t i i |  p t  d t i  d Sn t i t i	 |  d  } | o' d k } | i d | t d d n t i t i	 |  d  } | p t  d	 t i  d Sn x | D] } |  i | |  q Wt  d
 d t |  d | d S(   s   
        Loads an i18n directory (Zope3 PTS format)
        Format:
            Products/MyProduct/i18n/*.po
        The language and domain are stored in the po file
        s   looking into s   it does not existNs   *.mos   Compiled po files (*.mo) found in %s. PlacelessTranslationService now compiles mo files automatically. All mo files have been ignored.t
   stackleveli   s   *.pos   nothing founds   Initialized:t   details	    from %s
(   Rw   t   basepathRx   t   DEBUGRR   RS   t   isdirt   fnmatcht   filtert   listdirt
   depr_namest   warningst   warnt   DeprecationWarningt   namesRf   R   Rz   t   repr(   R   R}   R   R   R   Rf   (    (    R
   t   _load_i18n_dir"  s$     	 c         C   s  t  i i t  i i | |   } t  i i t  i i | | d   } t	 } t	 } y t  i |  t } Wn t t f j
 o d } n Xt  i i |  oX y t  i |  t } Wn t t f j
 o d } n X| | j o
 t } q d Sn t } | p | o yE t | |  i   }
 t | d  }	 |	 i |
 i    |	 i   Wn4 t t t f j
 o t d | t i  d Sn X| o t Sqn d S(   su   
        Creates or updates a mo file in the locales folder. Returns True if a
        new file was created.
        s   .moi    Nt   wbs   Error while compiling %s(!   RR   RS   t   normpathR!   t   msgpathRf   Rj   R   t   mofileR   t   createt   updatet   statt   ST_MTIMEt   po_mtimeRr   Re   t   existst   mo_mtimeR`   t   Msgfmtt	   getAsFilet   mot   opent   fdt   writet   readt   closet   PoSyntaxErrorRw   Rx   Ry   R   (   R   Rf   R   RA   R   R   R   R   R   R   R   Rj   R   (    (    R
   t   _updateMoFileB  s<     !%
	c         C   sI  g  } t d | t i  t i i |  p t d t i  d Sn xt i |  D]} t i i
 | |  } t i i |  p qU n t |  p d Sn t i i
 | d  } t i i |  p qU n t i t i |  d  } x| D]}	 |	 d  } | i d | | f  |  i |	 | | |  } | o t i i
 | | d  } t t d	 | d j o/ t |  } t   } | i | t d	 | n t t d	 | } | d j	 oF t! d j	 o t" | | |  }
 n t$ | | |  }
 | i% |
  q q q WqU W| p t d
 t i  d Sn t d d t& |  d | d S(   s  
        Loads an locales directory (Zope3 format)
        Format:
            Products/MyProduct/locales/${lang}/LC_MESSAGES/${domain}.po
        Where ${lang} and ${domain} are the language and the domain of the po
        file (e.g. locales/de/LC_MESSAGES/plone.po)
        s   looking into s   it does not existNt   LC_MESSAGESs   *.pois   %s:%ss   .moRf   s   nothing founds   Initialized:R|   s	    from %s
('   t   foundRw   R}   Rx   R~   RR   RS   R   R   RA   R!   t   langpatht   _checkLanguageR   R   R   R   Rf   R   R>   R   R   t   resultR   t   queryUtilityt   ITranslationDomainR   t   TranslationDomaint	   ts_domaint   getGlobalSiteManagert   smt   registerUtilityt   utilt   PTS_LANGUAGESRp   t   catt   LazyGettextMessageCatalogRo   R   (   R   R}   R   R   R   R   R   RA   R   Rf   R   R   R   R   R   (    (    R
   t   _load_locales_dirr  sL       
	t   manage_renameObjectc         C   sF   |  i |  } |  i |  t i |  | | d d |  i	 |  d S(   sD   
        wrap manage_renameObject to deal with registration
        R   N(
   R   Rk   Rh   R8   RK   t   FolderR   t   new_idR   RC   (   R   Rh   R   R   R8   (    (    R
   R     s
     c         C   s3   |  i |  } t i |  | |  |  i |  d  S(   N(   R   Rk   Rh   R8   R   Rn   t   dpRK   (   R   Rh   R   R8   (    (    R
   Rn     s    Rq   c         C   sM   | i } |  i |  |  i |  | i   |  i |  } |  i |  d  S(   N(   R8   Rh   R   Rk   RK   t   reloadRC   (   R   R8   Rh   (    (    R
   Rq     s    	
Ro   c         C   s   y |  i | i  Wn n Xt | t  p% | i   } t |  p d  SqS n |  i	 | i | d t
 t d | i | i f  |  i |  d  S(   Nt	   set_owners   adding %s: %s(   R   Rn   R8   Rh   R7   R9   R;   RA   R   t
   _setObjectR   Rw   t   titleRC   (   R   R8   RA   (    (    R
   Ro     s    t   getCatalogsForTranslationc         C   s  | d  j o |  i | |  } n t i | | f f   p t i | | f f   } t
 |  } xc |  i D]X }	 t i |	 | f f   } | o3 x0 | D]$ } | | j o | i |  q q Wqj qj Wd } xY t t |   D]E }
 | |
 } | i d  o% | |
 =| i | |  | d 7} q q W| i t  p | i t t  n x3 | D]+ } t i |  o | i t t  PqSqSWg  } | D] } | t | q~ S(   Ni    s   GlobalCatalogs-i   (    R   R   R   R   t   requestR   R3   t   getR4   t   catalog_namest   listR6   R<   t   fallback_catalog_namest   fallback_catalog_nameR>   t   post   rangeR_   t   it   catalog_nameR^   t   insertt   has_keyt
   PTS_IS_RTLt   setR   Rf   t   rtlRegistryR`   t   _[1]t   translationRegistry(   R   R   R   R   R   R   R   R   Rf   R<   R   R   R   (    (    R
   R     s8    1
   
 	t   setLanguageFallbacksc         C   s$   | d  j o
 t } n | |  _ d  S(   N(   R5   R   R   R   R6   (   R   R5   (    (    R
   R     s    
R   c         C   sm   xf t  i   D]X \ \ } } } | | j o9 x6 | D]* } |  i |  } | i
 o | i
 Sq3 q3 Wq q Wd  S(   N(   R3   RE   t   ccodet   cdomaint   cnamesR   t   cnameR   Rk   R   Rf   (   R   R   R   R   R   R   R   (    (    R
   R     s      
R   c         C   s   | d j oA t i   } h  } x | D] } d | | d <q& W| i   } nA g  } t i   D]& } | d | j o | | d q_ q_ ~ } | i	   | S(   s)   
        Get available languages
        i   i    N(
   R   R   R3   t   keyst   langst   dt   lR   t   kt   sort(   R   R   R   R   R   R   R   (    (    R
   R     s      @
R   s   The isRTL method of the PTS is deprecated and will be removed in the next PTS release. Use the information found in the Zope3 locale instead.c         C   sU   t  | d |  } | i t d  } | d j o |  i | |  n | i t t
  S(   s   get RTL settings
        R   N(   t   getattrR   R   R   R   R   t
   pts_is_rtlR   R   R   R   (   R   R   R   R   R   (    (    R
   R     s     R   s}   The utranslate method of the PTS is deprecated and will be removed in the next PTS release. Use the translate method instead.c         C   s   |  i | | | | | |  S(   s,   
        translate() using Unicode.
        N(   R   R   R   R   R   R   R   R   (   R   R   R   R   R   R   R   (    (    R
   R   (  s     R   c   
      C   s   | p | Sn t i |  p t | d  } n | }	 |  i | | |  } xU | D]6 } y t | | |  }	 Wn t j
 o
 qT n XPqT W| d j	 o
 | }	 n |  i |	 |  S(   s4   
        Translate a message using Unicode.
        R   N(   R   R   t   IBrowserRequestt
   providedByR   t
   aq_acquiret   textR   R   R   R   t   catalogsR8   t
   getMessaget   KeyErrorR   t   interpolateR   (
   R   R   R   R   R   R   R   R   R8   R   (    (    R
   R   4  s"      
R   c         C   s   g  } t i   D]& } | d | j o | | d q q ~ g  } t i   D]& } | d | j o | | d qN qN ~ } x/ |  i D]$ } | | j o | i
 |  q q Wt i | | d  S(   Ni   i    R<   (   R   R3   R   t   mR   R4   R   R   R6   t   fallbackR>   t
   negotiatort	   negotiateR   (   R   R   R   R   R   R   R   (    (    R
   R   T  s    ~
 R:   c         C   s   t  | |   S(   s*   
        return a domain instance
        N(   t   DomainR   R   (   R   R   (    (    R
   R:   ^  s     R   c         C   s   | p | Sn t i |  } x | D] } t i |  d } | i | d  } | d j o
 | } n yT t | t  p t |  } n t | t  o d | } n | i | |  } Wq% t j
 o# } t d | | f t i  q% Xq% W| S(   sC   
        Insert the data passed from mapping into the text
        i    u   %ss   Decoding problem in: %s %sN(   R   R   t   _interp_regext   findallt
   to_replacet   stringt   _get_var_regext   varR   R   t   valueR7   t
   basestringt   strt   unicodet   replacet   UnicodeDecodeErrort   msgRw   Rx   Ry   (   R   R   R   R   R   R   R   R   (    (    R
   R   e  s&      
#t   manage_mainc         O   s   | |  j o | o | d } | d } n y t i |  |  | | |  } Wn= t j
 o1 t t i |   } | |  |  | | |  } n Xt	 | t
  o | i d  } n | i i d d  | S(   sN   
        Wrap Folder's manage_main to render international characters
        i    i   s   utf-8s   Content-types   text/html; charset=utf-8N(   R   R   t   aR   R   t   kwt   rt   AttributeErrort   ImplicitAcquisitionWrapperR7   R   t   encodet   RESPONSEt	   setHeader(   R   R   R   R   R   R   (    (    R
   R     s     
(   i   i   i   i    (+   R#   R$   R%   t
   implementst   IPlacelessTranslationServicet	   meta_typeR   t   iconR/   t   all_meta_typesR&   R'   R   R   RC   RJ   RK   R(   RL   Rz   R   R   R   R)   t   view_management_screensR   Rn   Rq   Ro   t   memoizeR   R   R*   R   R   R+   R   R   R   R   R:   R   R   (    (    (    R
   R-      s\    

			
	1*	 	0	7				'	!			(K   Rt   RR   t   reR   Rx   R   R   t   zope.componentR   R   t   zope.deprecationR+   t   zope.i18n.gettextmessagecatalogRp   t   zope.i18n.translationdomainR   t   zope.i18n.interfacesR   t   zope.interfaceR   t!   zope.publisher.interfaces.browserR   RT   t   ExtensionClasst   Baset   AcquisitionR   R   t   AccessControlR&   t   AccessControl.PermissionsR*   R   t   InitializeClasst
   OFS.FolderR   t)   Products.PlacelessTranslationService.loadR   R   t0   Products.PlacelessTranslationService.lazycatalogR   R9   R   R   R   t
   NegotiatorR   R   t
   interfacesR   R   t   msgfmtR   R   t   utilsRw   t   RegistryR   Rl   t   NAME_REt   compileR   R   R   t   environR   Ra   R   R3   t   registert   registerCatalogR4   t   registerFBCatalogR   R-   (2   R   R   R   R   R   R   R   R   R   Rp   R4   Rw   R   R  R   R   R   R  R
  R   R   R   R   R   R  R   R   R   R   R&   R  R9   Rt   R3   R+   R   R   R   R   Rx   Rl   R   R   R  RT   R-   R  R   RR   R*   (    (    R
   t   ?   sb   $		$				M
  