~ubuntu-branches/ubuntu/lucid/autokey/lucid

« back to all changes in this revision

Viewing changes to src/lib/scripting.py

  • Committer: Bazaar Package Importer
  • Author(s): Luke Faraone
  • Date: 2010-01-08 08:52:58 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20100108085258-czo4e8p007plrjxf
Tags: 0.61.2-1
* New upstream version:   
  - Bring back cut/copy/paste item menu options
  - Add 'engine' class to scripting framework to enable access to
    AutoKey internals
  - Add a configurable user module folder for import into scripts
  - Enable multiple selection mode in treeview and update all necessary
    interactions to work correctly
  - Enable inline renaming of items in treeview, get rid of title and
    description fields from the various pages

 

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import subprocess, threading, time, re
20
20
from PyQt4.QtGui import QClipboard, QApplication
 
21
import model
21
22
 
22
23
class Keyboard:
23
24
    """
529
530
        """
530
531
        self.__runWmctrl(["-r", title, "-b" + action + ',' + prop])
531
532
        
 
533
    def get_active_geometry(self):
 
534
        """
 
535
        Get the geometry of the currently active window
 
536
        
 
537
        Usage: C{window.get_active_geometry()}
 
538
        
 
539
        Returns a 4-tuple containing the x-origin, y-origin, width and height of the window (in pixels)
 
540
        """
 
541
        active = self.mediator.interface.get_window_title()
 
542
        result, output = self.__runWmctrl(["-l", "-G"])
 
543
        matchingLine = None
 
544
        for line in output.split('\n'):
 
545
            if active in line[34:].split(' ', 1)[-1]:
 
546
                matchingLine = line
 
547
                
 
548
        if matchingLine is not None:
 
549
            output = matchingLine[14:].split(' ')[0:3]
 
550
            return map(int, output)
 
551
        else:
 
552
            return None
 
553
        
532
554
    def __runWmctrl(self, args):
533
555
        p = subprocess.Popen(["wmctrl"] + args, stdout=subprocess.PIPE)
534
556
        retCode = p.wait()
535
557
        output = p.stdout.read()[:-1] # Drop trailing newline
536
558
        
537
559
        return (retCode, output)
 
560
        
 
561
        
 
562
class Engine:
 
563
    """
 
564
    Provides access to the internals of AutoKey.
 
565
    
 
566
    Note that any configuration changes made using this API while the configuration window
 
567
    is open will not appear until it is closed and re-opened.
 
568
    """
 
569
    
 
570
    def __init__(self, configManager, runner):
 
571
        self.configManager = configManager
 
572
        self.runner = runner
 
573
        
 
574
    def get_folder(self, title):
 
575
        """
 
576
        Retrieve a folder by its title
 
577
        
 
578
        Usage: C{engine.get_folder(title)}
 
579
        
 
580
        Note that if more than one folder has the same title, only the first match will be
 
581
        returned.
 
582
        """
 
583
        for folder in self.configManager.allFolders:
 
584
            if folder.title == title:
 
585
                return folder
 
586
        return None
 
587
        
 
588
    def create_phrase(self, folder, description, contents):
 
589
        """
 
590
        Create a text phrase
 
591
        
 
592
        Usage: C{engine.create_phrase(folder, description, contents)}
 
593
        
 
594
        A new phrase with no abbreviation or hotkey is created in the specified folder
 
595
        
 
596
        @param folder: folder to place the abbreviation in, retrieved using C{engine.get_folder()}
 
597
        @param description: description for the phrase
 
598
        @param contents: the expansion text
 
599
        """
 
600
        p = model.Phrase(description, contents)
 
601
        folder.add_item(p)
 
602
        self.configManager.config_altered()            
 
603
        
 
604
    def create_abbreviation(self, folder, description, abbr, contents):
 
605
        """
 
606
        Create a text abbreviation
 
607
        
 
608
        Usage: C{engine.create_abbreviation(folder, description, abbr, contents)}
 
609
        
 
610
        When the given abbreviation is typed, it will be replaced with the given
 
611
        text.
 
612
        
 
613
        @param folder: folder to place the abbreviation in, retrieved using C{engine.get_folder()}
 
614
        @param description: description for the phrase
 
615
        @param abbr: the abbreviation that will trigger the expansion
 
616
        @param contents: the expansion text
 
617
        @raises Exception: if the specified abbreviation is not unique
 
618
        """
 
619
        if not self.configManager.check_abbreviation_unique(abbr, None):
 
620
            raise Exception("The specified abbreviation is already in use")
 
621
        
 
622
        p = model.Phrase(description, contents)
 
623
        p.modes.append(model.TriggerMode.ABBREVIATION)
 
624
        p.abbreviation = abbr
 
625
        folder.add_item(p)
 
626
        self.configManager.config_altered()
 
627
        
 
628
    def create_hotkey(self, folder, description, modifiers, key, contents):
 
629
        """
 
630
        Create a text hotkey.
 
631
        
 
632
        Usage: C{engine.create_hotkey(folder, description, modifiers, key, contents)}
 
633
        
 
634
        When the given hotkey is pressed, it will be replaced with the given
 
635
        text. Modifiers must be given as a list of strings, with the following
 
636
        values permitted:
 
637
        
 
638
        <control>
 
639
        <alt>
 
640
        <super>
 
641
        <shift>
 
642
        
 
643
        The key must be an unshifted character (i.e. lowercase)
 
644
        
 
645
        @param folder: folder to place the abbreviation in, retrieved using C{engine.get_folder()}
 
646
        @param description: description for the phrase
 
647
        @param modifiers: modifiers to use with the hotkey (as a list)
 
648
        @param key: the hotkey
 
649
        @param contents: the expansion text
 
650
        @raises Exception: if the specified hotkey is not unique
 
651
        """
 
652
        modifiers.sort()
 
653
        if not self.configManager.check_hotkey_unique(modifiers, key, None):
 
654
            raise Exception("The specified hotkey and modifier combination is already in use")
 
655
        
 
656
        p = model.Phrase(description, contents)
 
657
        p.modes.append(model.TriggerMode.HOTKEY)
 
658
        p.set_hotkey(modifiers, key)
 
659
        folder.add_item(p)
 
660
        self.configManager.config_altered()
 
661
 
 
662
    def run_script(self, description):
 
663
        """
 
664
        Run an existing script using its description to look it up
 
665
        
 
666
        Usage: C{engine.run_script(description)}
 
667
        
 
668
        @param description: description of the script to run
 
669
        @raises Exception: if the specified script does not exist
 
670
        """
 
671
        targetScript = None
 
672
        for item in self.configManager.allItems:
 
673
            if item.description == description and isinstance(item, Script):
 
674
                targetScript = item
 
675
 
 
676
        if targetScript is not None:
 
677
            self.runner.execute(targetScript, "")
 
678
        else:
 
679
            raise Exception("No script with description '%s' found" % description)
 
680
            
 
681