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

« back to all changes in this revision

Viewing changes to src/lib/plugin/manager.py

  • Committer: Bazaar Package Importer
  • Author(s): Luke Faraone
  • Date: 2009-07-20 23:19:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090720231940-wzq85zwz7z3oxkkf
Tags: upstream-0.54.5
ImportĀ upstreamĀ versionĀ 0.54.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
import re
 
3
from plugins import *
 
4
 
 
5
TOKEN_RE = re.compile("(\$\(.*?\))", re.UNICODE)
 
6
 
 
7
class PluginManager:
 
8
    
 
9
    def __init__(self):
 
10
        self.plugins = {}
 
11
        self.actionMap = {}
 
12
        
 
13
        for className in PLUGINS:
 
14
            instance = eval(className)()
 
15
            self.plugins[instance.get_id()] = instance
 
16
            self.actionMap[instance.get_action()] = instance.get_id()
 
17
    
 
18
    def get_action_list(self):
 
19
        """
 
20
        Gets a list of strings, each describing the action of a plugin e.g. "Current Date/Time".
 
21
        """
 
22
        return self.actionMap.keys()
 
23
    
 
24
    def get_token(self, action, parentWindow):
 
25
        pluginId = self.actionMap[action]
 
26
        return self.plugins[pluginId].get_token(parentWindow)
 
27
        
 
28
    def process_expansion(self, expansion, buffer):
 
29
        """
 
30
        Tokenise the given expansion, and dispatch the tokens to the relevant plugins for replacement.
 
31
        Then return the finalised expansion.
 
32
        """
 
33
        tokens = TOKEN_RE.split(expansion.string)
 
34
        finalString = []
 
35
        
 
36
        for token in tokens:
 
37
            if TOKEN_RE.match(token):
 
38
                # Identify plugin
 
39
                pluginId = token.split(' ', 1)[0][2:].lower()
 
40
                try:
 
41
                    finalString.append(self.plugins[pluginId].get_string(token, buffer))
 
42
                    expansion.backspaces += self.plugins[pluginId].get_backspace_count() 
 
43
                except Exception, e:
 
44
                    raise PluginError(str(e))
 
45
            else:
 
46
                finalString.append(token)
 
47
                
 
48
        expansion.string = ''.join(finalString)
 
49
        
 
50
        
 
51
class PluginError(Exception):
 
52
    pass
 
 
b'\\ No newline at end of file'