ó
JNc           @   s†  d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l Z d  d l m Z d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ e d k r‚e ƒ  Z d Ge j ƒ  GHy e j ƒ  Z Wn e
 k
 rô d GHn Xd Ge j d ƒ GHd Ge j ƒ  GHy d Ge j ƒ  GHWn e k
 rAd GHn Xd GHe j ƒ  y e j ƒ  Z d GHWq‚e
 k
 r~d GHq‚Xn  d S(   iÿÿÿÿN(   t
   LocaleInfo(   t   language2localet   ExceptionMultipleConfigurationsc           B   s   e  Z d  Z RS(   s-    error when multiple languages are symlinked (   t   __name__t
   __module__t   __doc__(    (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyR      s   t   ExceptionUnconfiguredc           B   s   e  Z d  Z RS(   s"    error if no configuration is set (   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyR      s   t   ExceptionNoConfigForLocalec           B   s   e  Z d  Z RS(   s2    error if there is no config for the given locale (   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyR      s   t   FontConfigHackc           B   sS   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z	 RS(
   s    abstract the fontconfig hack s   /usr/share/language-selector/s
   /etc/fontsc         C   s5   d | |  _  | |  _ | |  _ t d | ƒ |  _ d  S(   Ns   %s/fontconfigt   languagelist(   t   datadirt   _datadirt   globalConfDirR    t   li(   t   selfR
   R   (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt   __init__!   s    		c         C   sT   t  j j t  j j | ƒ ƒ d } t j | d d ƒ \ } } } d | | j ƒ  f S(   sÞ    
        internal helper to extracr from our fontconfig filenames
        of the form 69-language-selector-zh-tw.conf the locale
        and country

        returns string of the form locale_COUTNRY (e.g. zh_TW)
        i    t   -i   s   %s_%s(   t   ost   patht   splitextt   basenamet   stringt   rsplitt   upper(   R   t   namet   fnamet   headt   llt   cc(    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt   _getLocaleCountryFromFileName(   s    "c         C   sG   g  } d |  j  } x- t j | ƒ D] } | j |  j | ƒ ƒ q# W| S(   sq    get the configurations we have as a list of languages
            (returns a list of ['zh_CN','zh_TW'])
        s$   %s/conf.avail/69-language-selector-*(   R   t   globt   appendR   (   R   t   rest   patternR   (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt   getAvailableConfigs3   s
    c         C   si   d |  j  } t j | ƒ } t | ƒ d k r: t ƒ  ‚ n  t | ƒ d k rX t ƒ  ‚ n  |  j | d ƒ S(   s   returns the current language configuration as a string (e.g. zh_CN)
        
            if the configfile is not a symlink it raises a
             ExceptionNotSymlink exception
            if the file dosn't exists raise a
             ExceptionUnconfigured exception
        s    %s/conf.d/69-language-selector-*i    i   (   R   R   t   lenR   R   R   (   R   R!   t   current_config(    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt   getCurrentConfig<   s    c         C   sM   d |  j  } x9 t j | ƒ D]( } t j j | ƒ r t j | ƒ q q Wd S(   se    removes the current fontconfig-voodoo configuration
            and do some sanity checking
        s   %s/conf.d/*-language-selector-*N(   R   R   R   R   t   existst   unlink(   R   R!   t   f(    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt   removeConfigL   s    c   	      C   s  t  j |  j | ƒ } | d } | |  j ƒ  k r= t ƒ  ‚ n  |  j ƒ  d |  j } xµ d | d | d j ƒ  f d | d g D]‡ } x~ t j t	 j
 j | | ƒ ƒ D]^ } t	 j
 j | ƒ } t	 j
 j |  j d | ƒ } t	 j
 j |  j d | ƒ } t	 j | | ƒ q¦ Wq Wt S(	   s    set the configuration for 'locale'. if locale can't be
            found a NoConfigurationForLocale exception it thrown
        t   LOCALEs   %s/conf.avail/s   *-language-selector-%s-%s.conft   LCODEt   CCODEs   *-language-selector-%s.confs
   conf.avails   conf.d(   t   macrost   LangpackMacrosR   R"   R   R)   R   t   lowerR   R   R   t   joinR   t   symlinkt   True(	   R   t   localet   macrt   basedirR!   R(   R   t	   from_linkt   to_link(    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt	   setConfigU   s    

%c         C   sX   |  j  j ƒ  d } t | ƒ d k r; |  j  j ƒ  d } n  t | ƒ } |  j | ƒ d S(   sÀ    set the configuration based on the locale in LocaleInfo. If
            no configuration is found the fontconfig config is set to
            'none'
            Can throw a exception
        i   i    N(   R   t   getUserDefaultLanguageR#   t   getSystemDefaultLanguageR   R8   (   R   t   langR3   (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt   setConfigBasedOnLocalel   s
    (
   R   R   R   R   R   R"   R%   R)   R8   R<   (    (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyR      s   							t   __main__s   available: t   unconfigureds   set config: t   zh_CNs	   current: s   run auto mode: s   no config for this locales   removeConfig()s/   ERROR: have config after calling removeConfig()s   unconfigured (as expected)(   R   R   t   os.pathR   R    R-   t   utilsR   t	   ExceptionR   R   R   t   objectR   R   t   fcR"   R%   t   configR8   R<   R)   (    (    (    s?   /usr/lib/python2.7/dist-packages/LanguageSelector/FontConfig.pyt   <module>   s:   Z			
	