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.py < prev    next >
Encoding:
Python Source  |  2006-03-20  |  3.8 KB  |  110 lines

  1. # FontConfig.py (c) 2006 Canonical, released under the GPL
  2. #
  3. # This file implements the fontconfig hack
  4. # The problem is that different languages have different needs for
  5. # fontconfig preferences. While it would be really good to have a single
  6. # config file it seems to be not feasible right now for practial purposes
  7. # (see https://wiki.ubuntu.com/DapperL10nSprint for more information)
  8. #
  9. # so this file implements a hack to add prefered languages based on the
  10. # configuration we got from the CJK community
  11.  
  12. import glob
  13. import os.path
  14.  
  15. from LocaleInfo import LocaleInfo
  16.  
  17. class ExceptionNotSymlink(Exception):
  18.     pass
  19. class ExceptionUnconfigured(Exception):
  20.     pass
  21. class ExceptionNoConfigForLocale(Exception):
  22.     pass
  23.  
  24. class FontConfigHack(object):
  25.     """ abstract the fontconfig hack """
  26.     def __init__(self,
  27.                  datadir="/usr/share/language-selector/",
  28.                  globalConfDir="/etc/fonts/"):
  29.         self.datadir="%s/fontconfig" % datadir
  30.         self.globalConfDir=globalConfDir
  31.         self.configFile = "%s/language-selector.conf" % self.globalConfDir
  32.         self.li = LocaleInfo("%s/data/languages" % datadir,
  33.                              "%s/data/countries" % datadir,
  34.                              "%s/data/languagelist" % datadir)
  35.     def getAvailableConfigs(self):
  36.         """ get the configurations we have as a list of languages
  37.             (returns a list of ['zh_CN','zh_TW'])
  38.         """
  39.         res = []
  40.         for name in glob.glob("%s/*" % self.datadir):
  41.             res.append(os.path.basename(name))
  42.         return res
  43.     def getCurrentConfig(self):
  44.         """ returns the current language configuration as a string (e.g. zh_CN)
  45.         
  46.             if the configfile is not a symlink it raises a
  47.              ExceptionNotSymlink exception
  48.             if the file dosn't exists raise a
  49.              ExceptionUnconfigured exception
  50.             if it's unconfigured return the string 'none'
  51.         """
  52.         f = self.configFile
  53.         if not os.path.exists(f):
  54.             raise ExceptionUnconfigured()
  55.         if not os.path.islink(f):
  56.             raise ExceptionNotSymlink()
  57.         realpath = os.path.realpath(f)
  58.         return os.path.basename(realpath)
  59.  
  60.     def setConfig(self, locale):
  61.         """ set the configuration for 'locale'. if locale can't be
  62.             found a NoConfigurationForLocale exception it thrown
  63.         """
  64.         # check if we have a config
  65.         if locale not in self.getAvailableConfigs():
  66.             raise ExceptionNoConfigForLocale()
  67.         # do sanity checking (is it really a symlink?)
  68.         if os.path.exists(self.configFile) and not os.path.islink(self.configFile):
  69.             raise ExceptionNotSymlink()
  70.         # remove existing symlink
  71.         if os.path.exists(self.configFile):
  72.             os.unlink(self.configFile)
  73.         # do the actual symlink
  74.         os.symlink(os.path.normpath("%s/%s"% (self.datadir, locale)),
  75.                    self.configFile)
  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.  
  90.     li = LocaleInfo("%s/languages" % datadir,
  91.                     "%s/countries" % datadir,
  92.                     "%s/languagelist" % datadir)
  93.  
  94.     fc = FontConfigHack()
  95.  
  96.     # clean up
  97.     os.unlink(fc.configFile)
  98.     
  99.     print fc.getAvailableConfigs()
  100.     try:
  101.         config = fc.getCurrentConfig()
  102.     except ExceptionNotSymlink:
  103.         print "not symlink"
  104.     except ExceptionUnconfigured:
  105.         print "unconfigured"
  106.  
  107.     print fc.setConfigBasedOnLocale()
  108.     print fc.getCurrentConfig()
  109.