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