home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / LanguageSelector / FontConfig.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  5.7 KB  |  152 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import glob
  5. import string
  6. import os.path as os
  7. from LocaleInfo import LocaleInfo
  8.  
  9. class ExceptionMultpleConfigurations(Exception):
  10.     ''' error when multiple languages are symlinked '''
  11.     pass
  12.  
  13.  
  14. class ExceptionUnconfigured(Exception):
  15.     ''' error if no configuration is set '''
  16.     pass
  17.  
  18.  
  19. class ExceptionNoConfigForLocale(Exception):
  20.     ''' error if there is no config for the given locale '''
  21.     pass
  22.  
  23.  
  24. class FontConfigHack(object):
  25.     ''' abstract the fontconfig hack '''
  26.     
  27.     def __init__(self, datadir = '/usr/share/language-selector/', globalConfDir = '/etc/fonts'):
  28.         self.datadir = '%s/fontconfig' % datadir
  29.         self.globalConfDir = globalConfDir
  30.         self.li = LocaleInfo('%s/data/languagelist' % datadir)
  31.  
  32.     
  33.     def _getLocaleCountryFromFileName(self, name):
  34.         ''' 
  35.         internal helper to extracr from our fontconfig filenames
  36.         of the form 69-language-selector-zh-tw.conf the locale
  37.         and country
  38.  
  39.         returns string of the form locale_COUTNRY (e.g. zh_TW)
  40.         '''
  41.         fname = os.path.splitext(os.path.basename(name))[0]
  42.         (head, ll, cc) = string.rsplit(fname, '-', 2)
  43.         return '%s_%s' % (ll, cc.upper())
  44.  
  45.     
  46.     def getAvailableConfigs(self):
  47.         """ get the configurations we have as a list of languages
  48.             (returns a list of ['zh_CN','zh_TW'])
  49.         """
  50.         res = []
  51.         pattern = '%s/conf.avail/69-language-selector-*' % self.globalConfDir
  52.         for name in glob.glob(pattern):
  53.             res.append(self._getLocaleCountryFromFileName(name))
  54.         
  55.         return res
  56.  
  57.     
  58.     def getCurrentConfig(self):
  59.         """ returns the current language configuration as a string (e.g. zh_CN)
  60.         
  61.             if the configfile is not a symlink it raises a
  62.              ExceptionNotSymlink exception
  63.             if the file dosn't exists raise a
  64.              ExceptionUnconfigured exception
  65.         """
  66.         pattern = '%s/conf.d/69-language-selector-*' % self.globalConfDir
  67.         current_config = glob.glob(pattern)
  68.         if len(current_config) == 0:
  69.             raise ExceptionUnconfigured()
  70.         len(current_config) == 0
  71.         if len(current_config) > 1:
  72.             raise ExceptionMultipleConfigurations()
  73.         len(current_config) > 1
  74.         return self._getLocaleCountryFromFileName(current_config[0])
  75.  
  76.     
  77.     def removeConfig(self):
  78.         ''' removes the current fontconfig-voodoo configuration
  79.             and do some sanity checking
  80.         '''
  81.         pattern = '%s/conf.d/*-language-selector-*' % self.globalConfDir
  82.         for f in glob.glob(pattern):
  83.             if os.path.exists(f):
  84.                 os.unlink(f)
  85.                 continue
  86.         
  87.  
  88.     
  89.     def setConfig(self, locale):
  90.         """ set the configuration for 'locale'. if locale can't be
  91.             found a NoConfigurationForLocale exception it thrown
  92.         """
  93.         if locale not in self.getAvailableConfigs():
  94.             raise ExceptionNoConfigForLocale()
  95.         locale not in self.getAvailableConfigs()
  96.         self.removeConfig()
  97.         (ll, cc) = locale.split('_')
  98.         basedir = '%s/conf.avail/' % self.globalConfDir
  99.         for pattern in [
  100.             '*-language-selector-%s-%s.conf' % (ll, cc.lower()),
  101.             '*-language-selector-%s.conf' % ll]:
  102.             for f in glob.glob(os.path.join(basedir, pattern)):
  103.                 fname = os.path.basename(f)
  104.                 from_link = os.path.join(self.globalConfDir, 'conf.avail', fname)
  105.                 to_link = os.path.join(self.globalConfDir, 'conf.d', fname)
  106.                 os.symlink(from_link, to_link)
  107.             
  108.         
  109.         return True
  110.  
  111.     
  112.     def setConfigBasedOnLocale(self):
  113.         """ set the configuration based on the locale in LocaleInfo. If
  114.             no configuration is found the fontconfig config is set to
  115.             'none'
  116.             Can throw a exception
  117.         """
  118.         lang = self.li.getDefaultLanguage()
  119.         self.setConfig(lang)
  120.  
  121.  
  122. if __name__ == '__main__':
  123.     fc = FontConfigHack()
  124.     print 'available: ', fc.getAvailableConfigs()
  125.     
  126.     try:
  127.         config = fc.getCurrentConfig()
  128.     except ExceptionUnconfigured:
  129.         print 'unconfigured'
  130.  
  131.     print 'set config: ', fc.setConfig('zh_CN')
  132.     print 'current: ', fc.getCurrentConfig()
  133.     
  134.     try:
  135.         print 'run auto mode: ', fc.setConfigBasedOnLocale()
  136.     except ExceptionNoConfigForLocale:
  137.         print 'no config for this locale'
  138.  
  139.     print 'removeConfig()'
  140.     fc.removeConfig()
  141.     
  142.     try:
  143.         config = fc.getCurrentConfig()
  144.         print 'ERROR: have config after calling removeConfig()'
  145.     except ExceptionUnconfigured:
  146.         print 'unconfigured (as expected)'
  147.     except:
  148.         None<EXCEPTION MATCH>ExceptionUnconfigured
  149.     
  150.  
  151. None<EXCEPTION MATCH>ExceptionUnconfigured
  152.