Module LanguageScript
[hide private]
[frames] | no frames]

Source Code for Module LanguageScript

  1  ''' 
  2  Allows a user to switch between up to three languages of their choosing as  
  3  supported by the current speech engine. 
  4   
  5  @author: Peter Parente 
  6  @organization: IBM Corporation 
  7  @copyright: Copyright (c) 2005, 2007 IBM Corporation 
  8   
  9  @license: I{The BSD License} 
 10  All rights reserved. This program and the accompanying materials are made  
 11  available under the terms of the BSD license which accompanies 
 12  this distribution, and is available at 
 13  U{http://www.opensource.org/licenses/bsd-license.php} 
 14  ''' 
 15  # import useful modules for Scripts 
 16  from AccessEngine import AEScript, AccessEngineAPI 
 17  from AccessEngine import AEConstants 
 18  #from AccessEngine.AEPor import AEPor 
 19  from Tools.i18n import bind, _ 
 20   
 21  # metadata describing this Script 
 22  __uie__ = dict(kind='script', tier=None, all_tiers=False) 
 23   
24 -class LanguageScriptState(AEScript.ScriptState):
25 ''' 26 Defines three enumeration fields which allow the user to select three 27 different languages among which to choose. 28 29 Alt1Lang (enum): First language, defaults to current device setting 30 31 Alt2Lang (enum): Second language, defaults to current device setting 32 33 Alt3Lang (enum): Third language, defaults to current device setting 34 '''
35 - def init(self):
36 ''' 37 Create L{AEState} settings for this L{AEScript <AEScript.AEScript>}. 38 ''' 39 self.newEnum('Alt1Lang', None, _('First language'), {_('None') : None}, 40 _('First alternative language'), False) 41 self.newEnum('Alt2Lang', None, _('Second language'), {_('None') : None}, 42 _('Second alternative language'), False) 43 self.newEnum('Alt3Lang', None, _('Third language'), {_('None') : None}, 44 _('Third alternative language'), False)
45
46 - def getGroups(self):
47 ''' 48 Gets configurable settings for this L{AEScript <AEScript.AEScript>}. 49 50 @return: Group of all configurable settings 51 @rtype: L{AEState.Setting.Group} 52 ''' 53 g = self.newGroup() 54 g.extend(['Alt1Lang', 'Alt2Lang', 'Alt3Lang']) 55 return g
56
57 -class LanguageScript(AEScript.AEScript):
58 ''' 59 Enables switching the active speech engine among up to three languages using 60 a hotkey press in the current application. 61 It defines the hotkey I{(Caps-Lock+L)} that switch the language. 62 ''' 63 STATE = LanguageScriptState 64
65 - def init(self):
66 ''' 67 Registers L{tasks <AEScript.registered_tasks>} that can be mapped to 68 L{AEInput.Gesture}s. 69 ''' 70 kbd = AccessEngineAPI.getInputDevice(None, 'keyboard') 71 self.registerTask('language switch speech', self.switchSpeech) 72 73 self.registerCommand(kbd, 'language switch speech', 74 _('language switch speech'), 75 False, [kbd.AEK_CAPS_LOCK, kbd.AEK_L]) 76 77 # get the languages supported by the current device 78 try: 79 lang = AccessEngineAPI.getStyleSetting(self, 'Language', **kwargs) 80 except Task.InvalidStyleError: 81 # language not supported, do nothing 82 return 83 84 # populate the lists with the current available languages and set the 85 # current language as the default 86 for s in self.state: 87 s.update(lang) 88 s.value = lang.value
89
90 - def getName(self):
91 ''' 92 Provides the localized name of this L{AEScript <AEScript.AEScript>}. 93 94 @return: Human readable name of this script. 95 @rtype: string 96 ''' 97 return _('Language switcher')
98
99 - def getDescription(self):
100 ''' 101 Describe what this L{AEScript <AEScript.AEScript>} do. 102 103 @return: Human readable translated description of this script. 104 @rtype: string 105 ''' 106 return _('Enables switching the active speech engine among up to three ' 107 'languages using a hotkey press in the current application.')
108 109 ############## 110 ## InputTask 111 ##############
112 - def switchSpeech(self, **kwargs):
113 ''' 114 Cycles through the alternative languages. 115 116 @param kwargs: Arbitrary keyword arguments to pass to the task 117 @type kwargs: dictionary 118 ''' 119 AccessEngineAPI.stopNow(self, cap='audio', role='output', **kwargs) 120 # get the current language setting 121 lang = AccessEngineAPI.getStyleVal(self, 'Language', **kwargs) 122 # get all setting names, ordered 123 names = [s.name for s in self.state] 124 names.sort() 125 # get all setting values 126 values = [s.value for s in (self.getScriptSetting(n) for n in names)] 127 128 # find the current value and start searching for a new value from there 129 try: 130 start = values.index(lang) 131 except ValueError: 132 start = 0 133 134 for i in xrange(start+1, start+len(names)): 135 n = i % len(names) 136 val = values[n] 137 if val != lang: 138 # switch to the first one that the active lang doesn't match 139 AccessEngineAPI.setStyleVal(self, 'Language', val, **kwargs) 140 text = _('speaking in %s')%self.getScriptSetting(names[n]).getLabel(val) 141 self.doTask('read message', 'BasicSpeechScript', text=text, **kwargs) 142 break
143