home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / LanguageSelector / FontConfig.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  3.7 KB  |  104 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import glob
  5. import os.path as os
  6. from LocaleInfo import LocaleInfo
  7.  
  8. class ExceptionNotSymlink(Exception):
  9.     pass
  10.  
  11.  
  12. class ExceptionUnconfigured(Exception):
  13.     pass
  14.  
  15.  
  16. class ExceptionNoConfigForLocale(Exception):
  17.     pass
  18.  
  19.  
  20. class FontConfigHack(object):
  21.     ''' abstract the fontconfig hack '''
  22.     
  23.     def __init__(self, datadir = '/usr/share/language-selector/', globalConfDir = '/etc/fonts/'):
  24.         self.datadir = '%s/fontconfig' % datadir
  25.         self.globalConfDir = globalConfDir
  26.         self.configFile = '%s/language-selector.conf' % self.globalConfDir
  27.         self.li = LocaleInfo('%s/data/languages' % datadir, '%s/data/countries' % datadir, '%s/data/languagelist' % datadir)
  28.  
  29.     
  30.     def getAvailableConfigs(self):
  31.         """ get the configurations we have as a list of languages
  32.             (returns a list of ['zh_CN','zh_TW'])
  33.         """
  34.         res = []
  35.         for name in glob.glob('%s/*' % self.datadir):
  36.             res.append(os.path.basename(name))
  37.         
  38.         return res
  39.  
  40.     
  41.     def getCurrentConfig(self):
  42.         """ returns the current language configuration as a string (e.g. zh_CN)
  43.         
  44.             if the configfile is not a symlink it raises a
  45.              ExceptionNotSymlink exception
  46.             if the file dosn't exists raise a
  47.              ExceptionUnconfigured exception
  48.             if it's unconfigured return the string 'none'
  49.         """
  50.         f = self.configFile
  51.         if not os.path.exists(f):
  52.             raise ExceptionUnconfigured()
  53.         
  54.         if not os.path.islink(f):
  55.             raise ExceptionNotSymlink()
  56.         
  57.         realpath = os.path.realpath(f)
  58.         return os.path.basename(realpath)
  59.  
  60.     
  61.     def setConfig(self, locale):
  62.         """ set the configuration for 'locale'. if locale can't be
  63.             found a NoConfigurationForLocale exception it thrown
  64.         """
  65.         if locale not in self.getAvailableConfigs():
  66.             raise ExceptionNoConfigForLocale()
  67.         
  68.         if os.path.exists(self.configFile) and not os.path.islink(self.configFile):
  69.             raise ExceptionNotSymlink()
  70.         
  71.         if os.path.exists(self.configFile):
  72.             os.unlink(self.configFile)
  73.         
  74.         os.symlink(os.path.normpath('%s/%s' % (self.datadir, locale)), self.configFile)
  75.  
  76.     
  77.     def setConfigBasedOnLocale(self):
  78.         """ set the configuration based on the locale in LocaleInfo. If
  79.             no configuration is found the fontconfig config is set to
  80.             'none'
  81.             Can throw a exception
  82.         """
  83.         lang = self.li.getDefaultLanguage()
  84.         self.setConfig(lang)
  85.  
  86.  
  87. if __name__ == '__main__':
  88.     datadir = '/usr/share/language-selector/data'
  89.     li = LocaleInfo('%s/languages' % datadir, '%s/countries' % datadir, '%s/languagelist' % datadir)
  90.     fc = FontConfigHack()
  91.     os.unlink(fc.configFile)
  92.     print fc.getAvailableConfigs()
  93.     
  94.     try:
  95.         config = fc.getCurrentConfig()
  96.     except ExceptionNotSymlink:
  97.         print 'not symlink'
  98.     except ExceptionUnconfigured:
  99.         print 'unconfigured'
  100.  
  101.     print fc.setConfigBasedOnLocale()
  102.     print fc.getCurrentConfig()
  103.  
  104.