Package AccessEngine :: Package AccessEngineAPI :: Module System
[hide private]
[frames] | no frames]

Source Code for Module AccessEngine.AccessEngineAPI.System

   1  ''' 
   2  Defines L{AccessEngineAPI} for manging L{AETier}s and L{AEScript 
   3  <AEScript.AEScript>}s. 
   4   
   5  @author: Peter Parente 
   6  @author: Scott Haeger 
   7  @organization: IBM Corporation 
   8  @copyright: Copyright (c) 2005, 2007 IBM Corporation 
   9   
  10  @author: Frank Zenker 
  11  @author: Ramona Bunk 
  12  @organization: IT Science Center Ruegen gGmbH, Germany 
  13  @copyright: Copyright (c) 2007, 2008 ITSC Ruegen 
  14   
  15  @license: I{The BSD License} 
  16  All rights reserved. This program and the accompanying materials are made  
  17  available under the terms of the BSD license which accompanies 
  18  this distribution, and is available at 
  19  U{http://www.opensource.org/licenses/bsd-license.php} 
  20  ''' 
  21   
  22  import weakref, sys 
  23  import AccessEngine 
  24  import Output 
  25  from AccessEngine import AERegistrar, AEEvent 
  26  from AEApiError import * 
  27  from AccessEngine import AEConstants 
  28   
29 -def getProfileName():
30 ''' 31 Gets the name of the profile SUE is running under. 32 33 @return: Profile name 34 @rtype: string 35 ''' 36 return AccessEngine.AESettingsManager.getProfileName()
37
38 -def pushScript(tier, *names):
39 ''' 40 Adds one or more L{AEScript <AEScript.AEScript>}s to the top of the script 41 stack in the tier. 42 43 If more than one L{AEScript <AEScript.AEScript>} is specified, the last 44 specified L{AEScript <AEScript.AEScript>} will be at the top of the stack 45 when this method completes. That is, the behavior of pushing more than one 46 L{AEScript <AEScript.AEScript>} at a time is the same as if each 47 L{AEScript <AEScript.AEScript>} were pushed individually. 48 49 @param tier: The tier to which the scripts should be added. 50 @type tier: L{AETier} 51 @param names: Names of L{AEScript <AEScript.AEScript>}s to add 52 @type names: list of string 53 ''' 54 reg = AERegistrar 55 scripts = [] 56 for name in names: 57 script = reg.loadOne(name) 58 if script is not None: 59 scripts.append(script) 60 tier.pushScript(*scripts)
61
62 -def insertScript(tier, index, script_name):
63 ''' 64 Adds one L{AEScript <AEScript.AEScript>} to the stack at the insertion index. 65 Negative indices are valid per Python list convention. 66 67 @param tier: tier to which the script should be added 68 @type tier: L{AETier} 69 @param index: Index at which the L{AEScript <AEScript.AEScript>} should be 70 inserted into the stack where zero is the bottom 71 @type index: integer 72 @param script_name: Name of the Script to add 73 @type script_name: string 74 ''' 75 reg = AERegistrar 76 script = reg.loadOne(script_name) 77 if script is not None: 78 tier.insertScript(index, script)
79
80 -def popScript(tier, *indices):
81 ''' 82 Removes one or more L{AEScript <AEScript.AEScript>}s from the stack given 83 their indices. 84 85 @param tier: AETier from which the scripts should be removed 86 @type tier: L{AETier} 87 @param indices: Indices of L{AEScript <AEScript.AEScript>}s to remove 88 @type indices: list of integer 89 @raise IndexError: When the stack is empty 90 @raise ValueError: When specified L{AEScript <AEScript.AEScript>} is not found 91 ''' 92 tier.popScript(*indices)
93
94 -def reloadScripts(tier):
95 ''' 96 Reloads all L{AEScript <AEScript.AEScript>}s in the current L{AETier}. Any 97 changes made to the L{AEScript <AEScript.AEScript>} logic will take effect 98 immediately after the reload. This method does not refer to the L{AEScript 99 <AEScript.AEScript>}s associated with the current profile. It simply reloads 100 all currently loaded L{AEScript <AEScript.AEScript>}s. 101 102 @param tier: The scripts will be reloaded for this tier. 103 @type tier: L{AETier} 104 ''' 105 reg = AERegistrar 106 # get official registered Script names for all existing Scripts in this AETier 107 names = getScriptClassNames(tier) 108 names.reverse() 109 # clear out existing Scripts 110 tier.clearScripts() 111 # push all new Scripts 112 pushScript(tier, *names)
113
114 -def _getUIEInstances(kind, which, tier = None):
115 ''' 116 Internal method for getting instances of all installed or associated 117 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>}s for the 118 given kind. 119 120 @note: Installed UIEs that are not importable because of missing 121 dependencies or other errors are not included in the list. 122 @note: Getting loaded chooser names is not currently supported. 123 124 @param tier: tier to get the scripts from. Must be set, when kind== 125 L{reg.SCRIPT <AERegistrar.SCRIPT>}. 126 @type tier: L{AETier} 127 @param kind: Kind of 128 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>} to fetch 129 metadata for, one of L{AERegistrar.ALL_KINDS} 130 @type kind: string 131 @param which: Which names to get, one of the UIE_ constants in 132 L{AccessEngine.AEConstants.API} 133 @type which: integer 134 @return: All descriptions 135 @rtype: list of string 136 ''' 137 reg = AERegistrar 138 if which == AEConstants.UIE_LOADED: 139 # handle each type serparately, as loaded instances are stored in diverse 140 # locations in SUE 141 if kind == reg.SCRIPT: 142 if tier is None: 143 uies = [] 144 else: 145 # get Scripts in this AETier 146 uies = tier.getScripts() 147 elif kind == reg.DEVICE: 148 # get devices 149 uies = AccessEngine.AEDeviceManager.getDevices() 150 elif kind == reg.CHOOSER: 151 # not supported at present 152 uies = [] 153 elif kind == reg.MONITOR: 154 imons, omons = AccessEngine.AEDeviceManager.getMonitors() 155 uies = list(set(imons) + set(omons) + set(AccessEngine.AETierManager.getMonitors()) 156 + set(AccessEngine.AEEventManager.getMonitors())) 157 # we already have live instances here, so just return them 158 return uies 159 160 # use the AERegistrar to look up class names 161 if which == AEConstants.UIE_INSTALLED: 162 names = reg.listInstalled(kind) 163 elif which == AEConstants.UIE_ALL_ASSOCIATED: 164 names = reg.listAssociated(kind, AccessEngine.AESettingsManager.getProfileName()) 165 elif which == AEConstants.UIE_TIER_ASSOCIATED and kind == reg.SCRIPT: 166 if tier is None: 167 names = [] 168 else: 169 names = reg.listAssociated(kind, AccessEngine.AESettingsManager.getProfileName(), 170 tier.getName()) 171 172 # here we need to contruct live instances because all we have is a class 173 # name from the AERegistrar 174 l = [] 175 for name in names: 176 uie = reg.loadOne(name) 177 if uie is not None: 178 l.append(uie) 179 return l
180
181 -def _getClassNames(kind, which, tier = None):
182 ''' 183 Internal method for getting class names of all installed or associated 184 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>}s for the 185 given kind. 186 187 @note: Installed UIEs that are not importable because of missing 188 dependencies are not listed as installed to maintain parity with 189 L{_getNames} and L{_getDescriptions}. 190 191 @param tier: tier to get the scripts from. Must be set, when kind== 192 L{reg.SCRIPT <AERegistrar.SCRIPT>} and which== 193 L{AEConstants.UIE_TIER_ASSOCIATED 194 <AccessEngine.AEConstants.API.UIE_TIER_ASSOCIATED>}. 195 @type tier: L{AETier} 196 @param kind: Kind of 197 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>} to fetch 198 metadata for, one of L{AERegistrar.ALL_KINDS} 199 @type kind: string 200 @param which: Which names to get, one of the UIE_ constants in 201 L{AccessEngine.AEConstants.API} 202 @type which: integer 203 @return: All descriptions 204 @rtype: list of string 205 ''' 206 reg = AERegistrar 207 if which == AEConstants.UIE_INSTALLED: 208 names = reg.listInstalled(kind) 209 elif which == AEConstants.UIE_ALL_ASSOCIATED: 210 names = reg.listAssociated(kind, AccessEngine.AESettingsManager.getProfileName()) 211 elif which == AEConstants.UIE_TIER_ASSOCIATED and kind == reg.SCRIPT: 212 if tier is None: 213 names = [] 214 else: 215 names = reg.listAssociated(kind, AccessEngine.AESettingsManager.getProfileName(), 216 tier.getName()) 217 # make sure the UIE is loadable before listing it 218 return filter(lambda x: reg.loadOne(x) is not None, names)
219
220 -def _getNames(kind, which, tier = None):
221 ''' 222 Internal method for getting names of all installed or associated 223 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>}s for the 224 given kind. 225 226 @param kind: Kind of 227 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>} to fetch 228 metadata for, one of L{AERegistrar.ALL_KINDS} 229 @type kind: string 230 @param which: Which names to get, one of the UIE_ constants in 231 L{AccessEngine.AEConstants.API} 232 @type which: integer 233 @param tier: If your are looking for names within a tier, you can specify one. 234 @type tier: L{AETier} 235 @return: All descriptions 236 @rtype: list of string 237 ''' 238 reg = AERegistrar 239 if which == AEConstants.UIE_INSTALLED: 240 return [uie.getName() for uie in (reg.loadOne(name) for name in 241 reg.listInstalled(kind)) 242 if uie is not None] 243 elif which in (AEConstants.UIE_ALL_ASSOCIATED, 244 AEConstants.UIE_TIER_ASSOCIATED): 245 names = _getClassNames(kind, which, tier) 246 if names is None: 247 return None 248 return [uie.getName() for uie in (reg.loadOne(name) for name in names) 249 if uie is not None]
250
251 -def _getDescriptions(kind, which, tier = None):
252 ''' 253 Internal method for getting descriptions of all installed or associated 254 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>}s for the 255 given kind. 256 257 @param kind: Kind of 258 L{AEUserInterface <AccessEngine.AEUserInterface.AEUserInterface>} to fetch 259 metadata for, one of L{AERegistrar.ALL_KINDS} 260 @type kind: string 261 @param which: Which names to get, one of the UIE_ constants in 262 L{AccessEngine.AEConstants.API} 263 @type which: integer 264 @param tier: If you are looking for a description within a tier, you can 265 specify one. 266 @type tier: L{AETier} 267 @return: All descriptions 268 @rtype: list of string 269 ''' 270 reg = AERegistrar 271 if which == AEConstants.UIE_INSTALLED: 272 return [uie.getDescription() for uie in (reg.loadOne(name) for name in 273 reg.listInstalled(kind)) 274 if uie is not None] 275 elif which in (AEConstants.UIE_ALL_ASSOCIATED, 276 AEConstants.UIE_TIER_ASSOCIATED): 277 names = _getClassNames(kind, which, tier) 278 if names is None: 279 return None 280 return [uie.getDescription() for uie in (reg.loadOne(name) 281 for name in names) 282 if uie is not None]
283
284 -def getScriptMetadata(tier, which=AEConstants.UIE_LOADED):
285 ''' 286 Gets metadata for all L{AEScript <AEScript.AEScript>}s in the L{AETier}, all 287 installed Scripts, Scripts associated with the L{AETier}, or all Scripts 288 associated with this profile depending on the which flag. Includes 289 Script class names, human readable names, and descriptions. 290 291 The default value is used if which is invalid. 292 293 @param which: Which names to get, one of the UIE_ constants in 294 L{AccessEngine.AEConstants.API} 295 @type which: integer 296 @param tier: The tier which contains the scripts. 297 @type tier: L{AETier} 298 @return: Class names, names, descriptions 299 @rtype: list of 3-tuple of string 300 ''' 301 uies = _getUIEInstances(AERegistrar.SCRIPT, which, tier) 302 303 return zip((uie.getClassName() for uie in uies), 304 (uie.getName() for uie in uies), 305 (uie.getDescription() for uie in uies))
306
307 -def getDeviceMetadata(which=AEConstants.UIE_LOADED):
308 ''' 309 Gets metadata for all L{AEOutput <AccessEngine.AEDevice.AEOutput.AEOutput>} 310 and L{AEInput <AccessEngine.AEDevice.AEInput.AEInput>} devices currently 311 loaded in, installed in, or associated with the active profile depending on 312 the which flag. 313 314 The default value is used if which is invalid. 315 316 @param which: Which names to get, one of the UIE_ constants in 317 L{AccessEngine.AEConstants.API} 318 @type which: integer 319 @return: Class names, names, descriptions 320 @rtype: list of 3-tuple of string 321 ''' 322 uies = _getUIEInstances(AERegistrar.DEVICE, which) 323 return zip((uie.getClassName() for uie in uies), 324 (uie.getName() for uie in uies), 325 (uie.getDescription() for uie in uies))
326
327 -def getMonitorMetadata(which=AEConstants.UIE_LOADED):
328 ''' 329 Gets metadata for all L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>}s 330 currently loaded in, installed in, or associated with the active profile 331 depending on the which flag. 332 333 The default value is used if which is invalid. 334 335 @param which: Which names to get, one of the UIE_ constants in 336 L{AccessEngine.AEConstants.API} 337 @type which: integer 338 @return: Class names, names, descriptions 339 @rtype: list of 3-tuple of string 340 ''' 341 uies = _getUIEInstances(AERegistrar.MONITOR, which) 342 return zip((uie.getClassName() for uie in uies), 343 (uie.getName() for uie in uies), 344 (uie.getDescription() for uie in uies))
345
346 -def getChooserMetadata(which=AEConstants.UIE_ALL_ASSOCIATED):
347 ''' 348 Gets metadata for all L{AEChooser <AEChooser.AEChooser>}s installed in or 349 associated with the active profile depending on the which flag. 350 351 The default value is used if which is invalid. 352 353 @note: Getting loaded chooser names is not currently supported. 354 355 @param which: Which names to get, one of the UIE_ constants in 356 L{AccessEngine.AEConstants.API} 357 @type which: integer 358 @return: Class names, names, descriptions 359 @rtype: list of 3-tuple of string 360 ''' 361 uies = _getUIEInstances(AERegistrar.CHOOSER, which) 362 return zip((uie.getClassName() for uie in uies), 363 (uie.getName() for uie in uies), 364 (uie.getDescription() for uie in uies))
365
366 -def getDeviceClassNames(which=AEConstants.UIE_LOADED):
367 ''' 368 Gets the programmatic class names of all L{AEOutput 369 <AccessEngine.AEDevice.AEOutput.AEOutput>} and L{AEInput 370 <AccessEngine.AEDevice.AEInput.AEInput>} devices currently loaded in, 371 installed in, or associated with the active profile depending on the which 372 flag. 373 374 The default value is used if which is invalid. 375 376 @param which: Which names to get, one of the UIE_ constants in 377 L{AccessEngine.AEConstants.API} 378 @type which: integer 379 @return: Names of all L{AEOutput <AccessEngine.AEDevice.AEOutput.AEOutput>} 380 and L{AEInput <AccessEngine.AEDevice.AEInput.AEInput>} classes 381 @rtype: list of string 382 ''' 383 if which != AEConstants.UIE_LOADED: 384 return _getClassNames(AERegistrar.DEVICE, which) 385 else: 386 return [dev.getClassName() for dev in AccessEngine.AEDeviceManager.getDevices()]
387
388 -def getDeviceNames(which=AEConstants.UIE_LOADED):
389 ''' 390 Gets the human readable names of all L{AEOutput 391 <AccessEngine.AEDevice.AEOutput.AEOutput>} and L{AEInput 392 <AccessEngine.AEDevice.AEInput.AEInput>} devices currently loaded in, 393 installed in, or associated with the active profile depending on the which 394 flag. 395 396 The default value is used if which is invalid. 397 398 @param which: Which names to get, one of the UIE_ constants in 399 L{AccessEngine.AEConstants.API} 400 @type which: integer 401 @return: Names of all L{AEOutput <AccessEngine.AEDevice.AEOutput.AEOutput>} 402 and L{AEInput <AccessEngine.AEDevice.AEInput.AEInput>} devices 403 @rtype: list of string 404 ''' 405 if which != AEConstants.UIE_LOADED: 406 return _getNames(AERegistrar.DEVICE, which) 407 else: 408 return [dev.getName() for dev in AccessEngine.AEDeviceManager.getDevices()]
409
410 -def getDeviceDescriptions(which=AEConstants.UIE_LOADED):
411 ''' 412 Gets the descriptions of all L{AEOutput 413 <AccessEngine.AEDevice.AEOutput.AEOutput>} and L{AEInput 414 <AccessEngine.AEDevice.AEInput.AEInput>} devices currently loaded in, 415 installed in, or associated with the active profile depending on the which 416 flag. 417 418 The default value is used if which is invalid. 419 420 @param which: Which names to get, one of the UIE_ constants in 421 L{AccessEngine.AEConstants.API} 422 @type which: integer 423 @return: Names of all L{AEOutput <AccessEngine.AEDevice.AEOutput.AEOutput>} 424 and L{AEInput <AccessEngine.AEDevice.AEInput.AEInput>} devices 425 @rtype: list of string 426 ''' 427 if which != AEConstants.UIE_LOADED: 428 return _getDescriptions(AERegistrar.DEVICE, which) 429 else: 430 return [dev.getDescription() for dev in AccessEngine.AEDeviceManager.getDevices()]
431
432 -def getMonitorClassNames(which=AEConstants.UIE_LOADED):
433 ''' 434 Gets the programmatic class names of all L{AEMonitor 435 <AccessEngine.AEMonitor.AEMonitor>}s currently loaded in, installed in, or 436 associated with the active profile depending on the which flag. 437 438 The default value is used if which is invalid. 439 440 @param which: Which names to get, one of the UIE_ constants in 441 L{AccessEngine.AEConstants.API} 442 @type which: integer 443 @return: Names of all L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>} classes 444 @rtype: list of string 445 ''' 446 if which != AEConstants.UIE_LOADED: 447 return _getClassNames(AERegistrar.MONITOR, which) 448 else: 449 return [mon.getClassName() for mon in 450 _getUIEInstances(AERegistrar.MONITOR, which)]
451
452 -def getMonitorNames(which=AEConstants.UIE_LOADED):
453 ''' 454 Gets the human readable names of all L{AEMonitor 455 <AccessEngine.AEMonitor.AEMonitor>}s currently loaded in, installed in, or 456 associated with the active profile depending on the which flag. 457 458 The default value is used if which is invalid. 459 460 @param which: Which names to get, one of the UIE_ constants in 461 L{AccessEngine.AEConstants.API} 462 @type which: integer 463 @return: Names of all L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>}s 464 @rtype: list of string 465 ''' 466 if which != AEConstants.UIE_LOADED: 467 return _getNames(AERegistrar.MONITOR, which) 468 else: 469 return [mon.getName() for mon in 470 _getUIEInstances(AERegistrar.MONITOR, which)]
471
472 -def getMonitorDescriptions(which=AEConstants.UIE_LOADED):
473 ''' 474 Gets the human readable descriptions of all L{AEMonitor 475 <AccessEngine.AEMonitor.AEMonitor>}s currently loaded in, installed in, or 476 associated with the active profile depending on the which flag. 477 478 The default value is used if which is invalid. 479 480 @param which: Which names to get, one of the UIE_ constants in 481 L{AccessEngine.AEConstants.API} 482 @type which: integer 483 @return: Descriptions of all L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>}s 484 @rtype: list of string 485 ''' 486 if which != AEConstants.UIE_LOADED: 487 return _getDescriptions(AERegistrar.MONITOR, which) 488 else: 489 return [mon.getDescription() for mon in 490 _getUIEInstances(AERegistrar.MONITOR, which)]
491
492 -def getChooserClassNames(which=AEConstants.UIE_ALL_ASSOCIATED):
493 ''' 494 Gets the programmatic class names of all L{AEChooser <AEChooser.AEChooser>}s 495 installed in or associated with the active profile depending on the which flag. 496 497 The default value is used if which is invalid. 498 499 @note: Getting loaded chooser names is not currently supported. 500 @param which: Which names to get, one of the UIE_ constants in 501 L{AccessEngine.AEConstants.API} 502 @type which: integer 503 @return: Names of all L{AEChooser <AEChooser.AEChooser>} classes 504 @rtype: list of string 505 ''' 506 if which != AEConstants.UIE_LOADED: 507 return _getClassNames(AERegistrar.CHOOSER, which)
508
509 -def getChooserNames(which=AEConstants.UIE_ALL_ASSOCIATED):
510 ''' 511 Gets the human readable names of all L{AEChooser <AEChooser.AEChooser>}s 512 installed in or associated with the active profile depending on the which flag. 513 514 The default value is used if which is invalid. 515 516 @note: Getting loaded chooser names is not currently supported. 517 @param which: Which names to get, one of the UIE_ constants in 518 L{AccessEngine.AEConstants.API} 519 @type which: integer 520 @return: Names of all L{AEChooser <AEChooser.AEChooser>}s 521 @rtype: list of string 522 ''' 523 if which != AEConstants.UIE_LOADED: 524 return _getNames(AERegistrar.CHOOSER, which)
525
526 -def getChooserDescriptions(which=AEConstants.UIE_ALL_ASSOCIATED):
527 ''' 528 Gets the descriptions of all L{AEChooser <AEChooser.AEChooser>}s installed in 529 or associated with the active profile depending on the which flag. 530 531 The default value is used if which is invalid. 532 533 @note: Getting loaded chooser names is not currently supported. 534 @param which: Which names to get, one of the UIE_ constants in 535 L{AccessEngine.AEConstants.API} 536 @type which: integer 537 @return: Descriptions of all L{AEChooser <AEChooser.AEChooser>}s 538 @rtype: list of string 539 ''' 540 if which != AEConstants.UIE_LOADED: 541 return _getDescriptions(AERegistrar.CHOOSER, which)
542
543 -def getScriptClassNames(tier, which=AEConstants.UIE_LOADED):
544 ''' 545 Gets the programmatic class names of all L{AEScript <AEScript.AEScript>}s in 546 this L{AETier}, all installed Scripts, Scripts associated with this L{AETier}, 547 or all Scripts associated with this profile depending on the which flag. 548 549 The default value is used if which is invalid. 550 551 @param which: Which names to get, one of the UIE_ constants in 552 L{AccessEngine.AEConstants.API} 553 @type which: integer 554 @param tier: The tier that contains the scripts. 555 @type tier: L{AETier} 556 @return: Names of all L{AEScript <AEScript.AEScript>} classes 557 @rtype: list of string 558 ''' 559 if which == AEConstants.UIE_LOADED: 560 return [script.getClassName() for script in tier.getScripts()] 561 else: 562 return _getClassNames(AERegistrar.SCRIPT, which, tier)
563
564 -def getScriptNames(tier, which=AEConstants.UIE_LOADED):
565 ''' 566 Gets the human readable names of all L{AEScript <AEScript.AEScript>}s in this 567 L{AETier}, all installed Scripts, Scripts associated with this L{AETier}, or 568 all Scripts associated with this profile depending on the which flag. 569 570 The default value is used if which is invalid. 571 572 @param tier: The tier that contains the scripts. 573 @type tier: L{AETier} 574 @param which: Which names to get, one of the UIE_ constants in 575 L{AccessEngine.AEConstants.API} 576 @type which: integer 577 @return: Names of all L{AEScript <AEScript.AEScript>}s 578 @rtype: list of string 579 ''' 580 if which != AEConstants.UIE_LOADED: 581 return _getNames(AERegistrar.SCRIPT, which, tier) 582 else: 583 return [script.getName() for script in tier.getScripts()]
584
585 -def getScriptDescriptions(tier, which=AEConstants.UIE_LOADED):
586 ''' 587 Gets the human readable descriptions of all L{AEScript <AEScript.AEScript>}s 588 in this L{AETier}, all installed Scripts, Scripts associated with this 589 L{AETier}, or all Scripts associated with this profile depending on the which 590 flag. 591 592 The default value is used if which is invalid. 593 594 @param tier: The tier that contains the scripts. 595 @type tier: L{AETier} 596 @param which: Which names to get, one of the UIE_ constants in 597 L{AccessEngine.AEConstants.API} 598 @type which: integer 599 @return: Descriptions of all L{AEScript <AEScript.AEScript>}s 600 @rtype: list of string 601 ''' 602 if which != AEConstants.UIE_LOADED: 603 return _getDescriptions(AERegistrar.SCRIPT, which, tier) 604 else: 605 return [script.getDescription() for script in tier.getScripts()]
606
607 -def getAETierNamedTasks(tier, name):
608 ''' 609 Gets the Tasks with the given name if they are registered anywhere in the 610 L{AETier}. If no Task is registered under the given name, returns None. 611 612 @param tier: The tier, in which to look for the tasks. 613 @type tier: L{AETier} 614 @param name: Name of the Tasks to locate 615 @type name: string 616 @return: A list with the found task keys or None. 617 @rtype: list of tuples ('Task Identity', script.getClassName()) 618 ''' 619 # TODO: NA: Methode findNamedTasks() nicht vorhanden? umbenannt? oder raus? 620 return tier.findNamedTasks(name)
621
622 -def getAETierEventTasks(event_type, task_layer, tier):
623 ''' 624 Gets all registered Tasks, which are registered to handle the given 625 L{AEEvent <AEEvent.AEEvent>} type on the given layer from the L{AETier}. 626 627 Calls L{AETier.getEventTasks <AETier.AETier.getEventTasks>} to search across 628 all L{AEScript <AEScript.AEScript>}s in the owning L{AETier}. 629 630 @param event_type: Desired type of L{AEEvent <AEEvent.AEEvent>} 631 @type event_type: L{AEEvent <AEEvent.AEEvent>} class 632 @param task_layer: Layer on which the desired Tasks are registered 633 @type task_layer: integer 634 @return: A list of Tasks that handle the given event type and on the given 635 layer in the L{AETier} which owns this L{AEScript <AEScript.AEScript>}. 636 @rtype: list of tuples ('Task Identity', script.getClassName()) 637 ''' 638 return tier.getEventTasks(event_type, task_layer)
639
640 -def getScriptNamedTask(script, task_name, task_script_name = None):
641 ''' 642 Checks whether the task with the given name is registered in the 643 L{AEScript <AEScript.AEScript>}. If no Task is registered under the given name 644 in the L{AEScript <AEScript.AEScript>}, returns None. 645 646 @param script: The script in which the task should be registered. 647 @type script: L{AEScript <AEScript.AEScript>} 648 @param task_name: Name of the Task to locate 649 @type task_name: string 650 @param task_script_name: The name of the script in which the task is 651 implemented in. If this parameter is None, it will be assumed the task is 652 implemented in script. 653 @type task_script_name: string 654 @return: A tuple with the task key information or None 655 @rtype: tuple ('Task Identity', script.getClassName()) 656 @see: L{getAETierNamedTasks} 657 ''' 658 if script is not None: 659 if task_script_name is None: 660 task_script_name = script.getClassName() 661 key = (task_name, task_script_name) 662 if script.getRegisteredTask(key) is not None: 663 return key 664 return None
665
666 -def getScriptNamedTaskFunctions(script, task_name, task_script_name = None):
667 ''' 668 Gets the Task functions with the given name if it is registered B{in this 669 L{AEScript <AEScript.AEScript>} only}. If no Task is registered under the 670 given name in this L{AEScript <AEScript.AEScript>}, returns None. 671 672 @param script: The script in which the task should be registered. 673 @type script: L{AEScript <AEScript.AEScript>} 674 @param task_name: Name of the Task to locate 675 @type task_name: string 676 @param task_script_name: The name of the script in which the task is 677 implemented in. If this parameter is None, it will be assumed the task is 678 implemented in script. 679 @type task_script_name: string 680 @return: tuple with the execute and update function of the task with the 681 given name or None 682 @rtype: tuple (exec_function, update_function) 683 @see: L{getAETierNamedTasks} 684 ''' 685 if script is not None: 686 if task_script_name is None: 687 task_script_name = script.getClassName() 688 key = (task_name, task_script_name) 689 return script.getRegisteredTask(key) 690 else: 691 return None
692
693 -def getScriptEventTasks(event_type, task_layer, script):
694 ''' 695 Get all registered Tasks registered to handle the given L{AEEvent 696 <AEEvent.AEEvent>} type in the L{AEScript <AEScript.AEScript>}. 697 698 @param event_type: Desired type of L{AEEvent <AEEvent.AEEvent>} 699 @type event_type: L{AEEvent <AEEvent.AEEvent>} class 700 @param task_layer: Layer on which the desired Tasks are registered 701 @type task_layer: integer 702 @param script: the script in which to look for the tasks 703 @type script: L{AEScript <AEScript.AEScript>} 704 @return: List of the keys to all tasks that handle the given event type on 705 the given layer in the L{AEScript <AEScript.AEScript>} 706 @rtype: list of tuple ('Task Identity', script.getClassName()) 707 @see: L{getAETierEventTasks} 708 ''' 709 return script.getEventTasks(event_type, task_layer)
710
711 -def getDeviceState(device_name):
712 ''' 713 Gets the default L{AEState <AccessEngine.AEState.AEState>} associated with an 714 L{AEOutput <AccessEngine.AEDevice.AEOutput.AEOutput>} or 715 L{AEInput <AccessEngine.AEDevice.AEInput.AEInput>} device currently loaded in 716 the L{AEDeviceManager}. 717 718 @param device_name: Name of the device 719 @type device_name: string 720 @return: Loaded state for the device 721 @rtype: L{AEState <AccessEngine.AEState.AEState>} 722 ''' 723 try: 724 dev = Output.getOutputDevice(device_name) 725 except InvalidDeviceError: 726 return None 727 return dev.getDefaultStyle()
728
729 -def getScriptState(tier, script_name, reload=True):
730 ''' 731 Gets the L{AEState <AccessEngine.AEState.AEState>} associated with a 732 L{AEScript <AEScript.AEScript>}. If the state was previously persisted to 733 disk, it is loaded from disk. If not, a new state object is created for the 734 named Script and immediately persisted in the user's profile. 735 736 @param tier: The tier that contains the script. 737 @type tier: L{AETier} 738 @param script_name: Name of the L{AEScript <AEScript.AEScript>} 739 @type script_name: string 740 @return: Loaded state for the L{AEScript <AEScript.AEScript>} 741 @rtype: L{AEState <AccessEngine.AEState.AEState>} 742 ''' 743 if not reload: 744 try: 745 script = getScriptByName(tier, script_name) 746 except KeyError: 747 pass 748 else: 749 return script.getState() 750 751 # TODO: Is the reloaded script referenced somewhere in a tier? 752 753 # load an instance of the script 754 script = AERegistrar.loadOne(script_name) 755 if script is None: 756 # quit if we couldn't load the script 757 return None 758 # create a new empty state object 759 state = script.getState() 760 # try to load previously persisted state 761 try: 762 return AccessEngine.AESettingsManager.loadState(script_name, state) 763 except KeyError: 764 pass 765 # persist a new state instance if it does not exist 766 AccessEngine.AESettingsManager.saveState(script_name, state) 767 return state
768
769 -def loadAllMonitors():
770 ''' 771 Loads and shows all L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>}s 772 associated with this profile. 773 774 @return: Was at least one monitor loaded? 775 @rtype: boolean 776 ''' 777 loaded = False 778 reg = AERegistrar 779 mons = reg.loadAssociated(AERegistrar.MONITOR, 780 AccessEngine.AESettingsManager.getProfileName()) 781 emons = AccessEngine.AEEventManager.getMonitors() 782 imons, omons = AccessEngine.AEDeviceManager.getMonitors() 783 tmons = AccessEngine.AETierManager.getMonitors() 784 if not len(emons): 785 AccessEngine.AEEventManager.addMonitors(*mons) 786 loaded = True 787 if not len(tmons): 788 AccessEngine.AETierManager.addMonitors(*mons) 789 loaded = True 790 if not (len(imons) + len(omons)): 791 AccessEngine.AEDeviceManager.addMonitors(*mons) 792 loaded = True 793 return loaded
794
795 -def unloadAllMonitors():
796 ''' 797 Hides and unloads all L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>}s 798 associated with this profile. 799 800 @return: Was at least one monitor unloaded? 801 @rtype: boolean 802 ''' 803 unloaded = False 804 emons = AccessEngine.AEEventManager.getMonitors() 805 imons, omons = AccessEngine.AEDeviceManager.getMonitors() 806 tmons = AccessEngine.AETierManager.getMonitors() 807 for mon in (emons, imons, omons, tmons): 808 if len(mon): 809 mon.clear() 810 unloaded = True 811 return unloaded
812
813 -def loadMonitor(name):
814 ''' 815 Loads and shows one L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>}. 816 817 @param name: Class name of the L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>} 818 @type name: string 819 ''' 820 mon = AERegistrar.loadOne(name) 821 AccessEngine.AEEventManager.addMonitors(mon) 822 AccessEngine.AETierManager.addMonitors(mon) 823 AccessEngine.AEDeviceManager.addMonitors(mon)
824
825 -def unloadMonitor(name):
826 ''' 827 Hides and unloads one L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>}. 828 829 @param name: Class name of the L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>} 830 @type name: string 831 ''' 832 emons = AccessEngine.AEEventManager.getMonitors() 833 tmons = AccessEngine.AETierManager.getMonitors() 834 imons, omons = AccessEngine.AEDeviceManager.getMonitors() 835 for mons in (emons, tmons, imons, omons): 836 mons.removeByClassName(name)
837
838 -def loadChooser(script, name, **kwargs):
839 ''' 840 Loads an L{AEChooser <AEChooser.AEChooser>} dialog with the given name from 841 disk. Passes the keyword arguments to the init method of the chooser. This 842 method should typically only be invoked by a L{AEScript.EventScript} subclass, 843 which implements one or more of the following methods: 844 L{EventScript.onChooserStart <AEScript.EventScript.onChooserStart>}, 845 L{EventScript.onChooserSignal <AEScript.EventScript.onChooserSignal>}, 846 L{EventScript.onChooserEnd <AEScript.EventScript.onChooserEnd>}. 847 848 @param script: the script which invokes this method. 849 @type script: L{AEScript.EventScript} 850 @param name: UIE name of the chooser 851 @type name: string 852 @param kwargs: Keyword arguments to initialize the chooser 853 @type kwargs: dictionary 854 @return: Reference to the chooser if it was created. If it already existed, 855 C{None} is returned instead. 856 @rtype: L{AEChooser <AEChooser.AEChooser>} 857 ''' 858 reg = AERegistrar 859 chooser = reg.loadOne(name) 860 if chooser is None: 861 return 862 try: 863 # call the chooser to initialize it with a reference to the event manager 864 chooser(script.getAETier().getIdentity()) 865 except ValueError, ex: 866 # singleton already exists, reactivate 867 ex.args[0].activate(**kwargs) 868 return None 869 else: 870 # call init to let the chooser initialize itself 871 chooser.init(**kwargs) 872 # register to handle events from this chooser 873 script.registerChooserTask(chooser, kwargs['task_name']) 874 return chooser
875
876 -def unloadChooser(script, chooser):
877 ''' 878 Unloads the given L{AEChooser <AEChooser.AEChooser>} dialog. This method 879 should typically only be invoked by a by a L{AEScript.EventScript} subclass, 880 which implements one or more of the following methods: 881 L{EventScript.onChooserStart <AEScript.EventScript.onChooserStart>}, 882 L{EventScript.onChooserSignal <AEScript.EventScript.onChooserSignal>}, 883 L{EventScript.onChooserEnd <AEScript.EventScript.onChooserEnd>}. 884 885 @param script: the script which invokes this method. 886 @type script: L{AEScript.EventScript} 887 @param chooser: The chooser to unload 888 @type chooser: L{AEChooser <AEChooser.AEChooser>} 889 ''' 890 # unregister the task observing this chooser 891 script.unregisterChooserTask(chooser)
892
893 -def blockNTasks(n, task_type, condition=lambda **x: True):
894 ''' 895 Blocks tasks of the given type from executing in response to the next 896 n events. This is a convenience shortcut to avoid registering one-shot 897 tasks in L{AEScript <AEScript.AEScript>}s to do exactly the same thing. 898 899 @param n: Number of events to block 900 @type n: integer 901 @param task_type: Kind of task to block 902 @type task_type: task class 903 @param condition: Condition statement to check if the event is actually 904 one to be consumed. The callable is called with the keyword arguments 905 passed to the task execute method. 906 @type condition: callable 907 ''' 908 # TODO: This function needs to be completly reworked 909 raise NotImplementedError('AccessEngineAPI.System.System.blockNTasks is not implemented')
910 911 ## define an anonymous class of the given type 912 #class consumer(task_type): 913 #def init(self): 914 #self.count = n 915 #def execute(self, **kwargs): 916 #if not condition(**kwargs): 917 #return 918 #self.count -= 1 919 #if self.count == 0: 920 #self.unregisterTask(self) 921 #return False 922 ## register an instance as a blocker 923 #self.registerTask(consumer(None)) 924
925 -def associateScript(name, tier=None, all_tiers=False, index=None):
926 ''' 927 Associates a L{AEScript <AEScript.AEScript>} with the active profile. 928 929 @param name: Name of the UIE to associate 930 @type name: string 931 @param tier: Name of the L{AETier} with which the 932 L{AEScript <AEScript.AEScript>} will be associated. 933 @type tier: string 934 @param all_tiers: Should the L{AEScript <AEScript.AEScript>} be loaded on 935 every L{AETier}? Defaults to the value specified by the Script itself. 936 @type all_tiers: boolean 937 @param index: Load order index of the L{AEScript <AEScript.AEScript>}. Lower 938 means sooner to load but also later to handle events (i.e. bottom of the 939 Script stack). 940 @type index: integer 941 ''' 942 AERegistrar.associate(name, [getProfileName()], tier, all_tiers, index)
943
944 -def disassociateScript(name, tier=None, all_tiers=False):
945 ''' 946 Disassociates a L{AEScript <AEScript.AEScript>} from the active profile. 947 948 @param name: Name of the UIE to disassociate 949 @type name: string 950 @param tier: Name of the L{AETier} from which the 951 L{AEScript <AEScript.AEScript>} will be disassociated. Defaults to the name 952 of the L{AETier} specified by the Script itself. 953 @type tier: string 954 @param all_tiers: Should the L{AEScript <AEScript.AEScript>} be removed from 955 the list of Scripts to load for every L{AETier}? Defaults to the value 956 specified by the Script itself. 957 @type all_tiers: boolean 958 ''' 959 AERegistrar.disassociate(name, [getProfileName()], tier, all_tiers)
960
961 -def associateMonitor(name):
962 ''' 963 Associates a L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>} with the active 964 profile. Loads it immediately. 965 966 @param name: Name of the UIE to associate 967 @type name: string 968 ''' 969 AERegistrar.associate(name, [getProfileName()]) 970 loadMonitor(name)
971
972 -def disassociateMonitor(name):
973 ''' 974 Associates a L{AEMonitor <AccessEngine.AEMonitor.AEMonitor>} with the active 975 profile. Unloads it immediately. 976 977 @param name: Name of the UIE to associate 978 @type name: string 979 ''' 980 unloadMonitor(name) 981 AERegistrar.disassociate(name, [getProfileName()])
982
983 -def associateDevice(name, index=None):
984 ''' 985 Associates a L{AEOutput <AccessEngine.AEDevice.AEOutput.AEOutput>} or 986 L{AEInput <AccessEngine.AEDevice.AEInput.AEInput>} device with the active 987 profile. Does not load it immediately. See L{refreshDevices} for that 988 functionality. 989 990 @param name: Name of the UIE to associate 991 @type name: string 992 @param index: Load order index of the device where lower means sooner, None 993 means last. 994 @type index: integer 995 ''' 996 AERegistrar.associate(name, [getProfileName()], index=index)
997
998 -def disassociateDevice(name):
999 ''' 1000 Disassociates a L{AEOutput <AccessEngine.AEDevice.AEOutput.AEOutput>} or 1001 L{AEInput <AccessEngine.AEDevice.AEInput.AEInput>} device from the active 1002 profile. Does not unload it immediately. See L{refreshDevices} for that 1003 functionality. 1004 1005 @param name: Name of the UIE to associate 1006 @type name: string 1007 ''' 1008 AERegistrar.disassociate(name, [getProfileName()])
1009
1010 -def refreshDevices():
1011 ''' 1012 Unloads all existing devices then immediately loads all devices associated 1013 with the active profile. 1014 ''' 1015 AccessEngine.AEDeviceManager.unloadDevices() 1016 AccessEngine.AEDeviceManager.loadDevices()
1017
1018 -def registerConstants(scope, *names):
1019 ''' 1020 Adds new constants to L{AccessEngine.AEConstants.API} such that they are 1021 visible to other L{AEScript <AEScript.AEScript>}s. If any of the named 1022 constants do not exist, an exception will be raised. If any of the named 1023 constants are already registered, they will be silently ignored by 1024 L{AEConstants.register} method. 1025 1026 @param scope: Scope in which the constants are defined (e.g. globals()) 1027 @type scope: dictionary 1028 @param names: Names of constants to register 1029 @type names: list of string 1030 @raise KeyError: When one of the named constants does not exist in the 1031 scope 1032 ''' 1033 AEConstants.register(dict(((name, scope[name]) for name in names)))
1034
1035 -def getScriptByName(tier, name):
1036 ''' 1037 Gets the L{AEScript <AEScript.AEScript>} with the given name from the L{AETier}. 1038 1039 If no script is registered under the given name, KeyError is raised. 1040 1041 @param tier: The tier in which the script may be registered. 1042 @type tier: L{AETier} 1043 @param name: Name of the script 1044 @type name: string 1045 @return: Script with the given name 1046 @rtype: L{AEScript <AEScript.AEScript>} 1047 @raise KeyError: When the script with the given name is not found 1048 ''' 1049 for script in tier.getScripts(): 1050 if script.getClassName() == name: 1051 return script 1052 raise KeyError
1053
1054 -def getScriptVar(tier, script_name, var_name):
1055 ''' 1056 Gets the L{AEScript <AEScript.AEScript>} instance variable with the given name. 1057 1058 If no script is registered under the given name a KeyError is raised, 1059 if the script does not contain var_name an AttributeError is raised. 1060 1061 @param tier: The tier in which the script may be registered. 1062 @type tier: L{AETier} 1063 @param script_name: Name of the script 1064 @type script_name: immutable 1065 @param var_name: Name of the instance variable 1066 @type var_name: immutable 1067 @return: script variable with the given name 1068 @rtype: object 1069 @raise KeyError: When the script or variable with it's given name is not 1070 found 1071 @raise AttributeError: When the attribute is not found 1072 ''' 1073 script = getScriptByName(tier, script_name) 1074 return getattr(script, var_name)
1075
1076 -def setScriptVar(tier, script_name, var_name, value):
1077 ''' 1078 Sets the L{AEScript <AEScript.AEScript>} instance variable with the given name 1079 to value. 1080 1081 If no script is registered under the given name a KeyError is raised, 1082 if the script does not contain var_name an AttributeError is raised. 1083 1084 @param tier: The tier in which the script may be registered. 1085 @type tier: L{AETier} 1086 @param script_name: Name of the script 1087 @type script_name: immutable 1088 @param var_name: Name of the instance variable 1089 @type var_name: immutable 1090 @param value: value to set instance variable 1091 @type value: immutable 1092 @return: script variable with the given name 1093 @rtype: object 1094 @raise KeyError: When the script or variable with it's given name 1095 @raise AttributeError: When attribute is not found 1096 ''' 1097 script = getScriptByName(tier, script_name) 1098 setattr(script, var_name, value)
1099
1100 -def getScriptSetting(tier, script_name, setting_name):
1101 ''' 1102 Gets the L{AEScript <AEScript.AEScript>} L{Setting} state variable with the 1103 given name. 1104 1105 This method should be used when a script needs to access the settings of 1106 another script. If a script wants to access its own settings use the 1107 L{AEScript.getScriptSetting <AEScript.AEScript.getScriptSetting>} method. 1108 1109 If no script is registered under the given name or setting_name is not found, 1110 KeyError is raised. 1111 1112 @param tier: The tier in which the script may be registered. 1113 @type tier: L{AETier} 1114 @param script_name: Name of the script 1115 @type script_name: immutable 1116 @param setting_name: Name of the script 1117 @type setting_name: immutable 1118 @return: L{Setting} object for given script. 1119 @rtype: object 1120 @raise KeyError: When the script is not found 1121 ''' 1122 state = getScriptState(tier, script_name, reload=False) 1123 return state.getSettingObj(setting_name)
1124
1125 -def getScriptSettingVal(tier, script_name, setting_name):
1126 ''' 1127 Gets the value of the L{AEScript <AEScript.AEScript>} L{Setting} state 1128 variable with the given name. 1129 1130 This method should be used when a script needs to access the settings of 1131 another script. If a script wants to access its own settings use the 1132 L{AEScript.getScriptSettingVal <AEScript.AEScript.getScriptSettingVal>} method. 1133 1134 If no script is registered under the given name or if setting_name is not 1135 found, KeyError is raised. 1136 1137 @param tier: The tier in which the script may be registered. 1138 @type tier: L{AETier} 1139 @param script_name: Name of the script 1140 @type script_name: immutable 1141 @param setting_name: Name of the script 1142 @type setting_name: immutable 1143 @return: value contained in L{AEState <AccessEngine.AEState.AEState>} 1144 attribute with given name 1145 @rtype: object 1146 @raise KeyError: When the script is not found 1147 ''' 1148 state = getScriptState(tier, script_name, reload=False) 1149 return state.getSettingVal(setting_name)
1150
1151 -def setScriptSettingVal(tier, script_name, setting_name, value):
1152 ''' 1153 Sets the value of the L{AEScript <AEScript.AEScript>} L{Setting} state 1154 variable with the given name. 1155 1156 This method should be used when a script needs to set the settings of another 1157 script. If a script wants to set its own settings use the 1158 L{AEScript.setScriptSettingVal <AEScript.AEScript.setScriptSettingVal>} method. 1159 1160 If no script is registered under the given name or if setting_name is not 1161 found, KeyError is raised. 1162 1163 @param tier: The tier in which the script may be registered. 1164 @type tier: L{AETier} 1165 @param script_name: Name of the script 1166 @type script_name: immutable 1167 @param setting_name: Name of the script 1168 @type setting_name: immutable 1169 @param value: value contained in L{AEState <AccessEngine.AEState.AEState>} 1170 attribute with given name 1171 @type value: object 1172 @raise KeyError: When the L{AEScript <AEScript.AEScript>} is not found 1173 ''' 1174 state = getScriptState(tier, script_name, reload=False) 1175 state.setSettingVal(setting_name, value)
1176