home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / LanguageSelector / ImSwitch.py < prev    next >
Encoding:
Python Source  |  2009-05-07  |  4.2 KB  |  120 lines

  1. # ImSwitch.py (c) 2006 Canonical, released under the GPL
  2. #
  3. # This file implements a interface to im-switch
  4. #
  5.  
  6. from LocaleInfo import LocaleInfo
  7. import os.path
  8. import sys
  9. import subprocess
  10.  
  11. class ImSwitch(object):
  12.     global_confdir = "/etc/X11/xinit/xinput.d/"
  13.     local_confdir = os.path.expanduser("~/.xinput.d/")
  14.     bin = "/usr/bin/im-switch"
  15.     default_method = "scim-bridge"
  16.  
  17.     def __init__(self):
  18.         pass
  19.  
  20.     def available(self):
  21.         " return True if im-switch is available at all "
  22.         return os.path.exists(self.bin)
  23.     
  24.     def enabledForLocale(self, locale):
  25.         " check if we have a config for this specifc locale (e.g. ja_JP) "
  26.         for dir in (self.local_confdir, self.global_confdir):
  27.             for name in (locale, "all_ALL"):
  28.                 target = os.path.join(dir,name)
  29.                 #print "checking im-switch config: ", target, os.path.basename(os.path.realpath(target))
  30.                 if os.path.exists(target):
  31.                     im_name = os.path.basename(os.path.realpath(target))
  32.                     if im_name in ("none", "default"):
  33.                         #print "points to none or default"
  34.                         return False
  35.                     #print "points to real config"
  36.                     return True
  37.         return False
  38.  
  39.     def enable(self, locale):
  40.         " enable input methods for locale"
  41.         # try auto first
  42.         subprocess.call(["im-switch","-z",locale,"-a"])
  43.         # if no config is set, force the default
  44.         if not self.enabledForLocale(locale):
  45.             subprocess.call(["im-switch","-z",locale,
  46.                              "-s", self.default_method])
  47.  
  48.     def disable(self, locale):
  49.         " disable input method for locale "
  50.         # remove local config first
  51.         if os.path.exists(os.path.join(self.local_confdir, locale)):
  52.             os.unlink(os.path.join(self.local_confdir, locale))
  53.         # see if we still have a input method and if so, force "none"
  54.         if self.enabledForLocale(locale):
  55.             subprocess.call(["im-switch","-z",locale,"-s","none"])
  56.  
  57.     def getInputMethodForLocale(self, locale):
  58.         for dir in (self.local_confdir, self.global_confdir):
  59.             for name in (locale, "all_ALL"):
  60.                 target = os.path.join(dir,name)
  61.                 if os.path.exists(target):
  62.                     return os.path.basename(os.path.realpath(target))
  63.         return None
  64.         
  65.     
  66.     # -------------------------- stuff below is NOT USED
  67.     def getAvailableInputMethods(self):
  68.         """ get the input methods available via im-switch """
  69.         inputMethods = []
  70.         for dentry in os.listdir(self.confdir):
  71.             if not os.path.islink(self.confdir+dentry):
  72.                 inputMethods.append(dentry)
  73.         inputMethods.sort()
  74.         return inputMethods
  75.  
  76.     def setDefaultInputMethod(self, method, locale="all_ALL"):
  77.         """ sets the default input method for the given locale
  78.             (in ll_CC form)
  79.         """
  80.         l = self.confdir+locale
  81.         if os.path.islink(l):
  82.             os.unlink(l)
  83.         os.symlink(self.confdir+method, l)
  84.         return True
  85.  
  86.     def resetDefaultInputMethod(self, locale="all_ALL"):
  87.         """ reset the default input method to auto (controlled by
  88.             im-switch
  89.         """
  90.         d = "/etc/alternatives/xinput-%s" % locale
  91.         l = self.confdir+locale
  92.         if os.path.islink(l):
  93.             os.unlink(l)
  94.         os.symlink(d, self.confdir+locale)
  95.         return True
  96.         
  97.     def getCurrentInputMethod(self, locale="all_ALL"):
  98.         """ get the current default input method for the selected
  99.             locale (in ll_CC form)
  100.         """
  101.         return os.path.basename(os.path.realpath(self.confdir+locale))
  102.         
  103. if __name__ == "__main__":
  104.     im = ImSwitch()
  105.     print im.getInputMethodForLocale("ja_JP")
  106.     print im.enabledForLocale("all_ALL")
  107.     sys.exit(1)
  108.     print "available input methods: "
  109.     print im.getAvailableInputMethods()
  110.     print "current method: ",
  111.     print im.getCurrentInputMethod()
  112.     print "switching to 'th-xim': ",
  113.     print im.setDefaultInputMethod("th-xim")
  114.     print "current method: ",
  115.     print im.getCurrentInputMethod()
  116.     print "reset default: ",
  117.     print im.resetDefaultInputMethod()
  118.     print "current method: ",
  119.     print im.getCurrentInputMethod()
  120.