File: shortcutsSet.py

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (63 lines) | stat: -rw-r--r-- 1,824 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import re


class ShortcutsSet:

    def __init__(self):

        self.shortcuts = {}

    def applyTo(self, text):

        for shortcut in self.shortcuts.keys():
            text = re.sub('(?<!\\\)"' + shortcut + '"', self.shortcuts[shortcut], text)
            
        text = text.replace('\\"', '"')
            
        return text

    def addShortcut(self, name, data):
        """Add's a single shortcut to the shortcuts list."""
        self.shortcuts[name] = data

    def delShortcut(self, name):
        """Delete's a shortcut from the list."""
        del self.shortcuts[name]

    def loadShortcutsFile(self, filename):
        """Load's in a set of shortcuts from a shortcuts file."""

        try:
            shortcutsFile = open(filename, "r")
        except IOError, e:
            # we don't want to print messages about non found shortcut files because many users
            # won't have them anyway
            return

        data = shortcutsFile.read().split("\n")
        shortcutsFile.close()
        
        for i in range(0,len(data),2):
            if i+1 < len(data):
                self.addShortcut(data[i], data[i+1])

    def saveShortcutsFile(self, filename):
        """Save's a set of shortcuts."""

        try:
            shortcutsFile = open(filename, "w")
        except IOError, e:
            # we don't want to print messages about non found shortcut files because many users
            # won't have them anyway
            return

        for k in self.shortcuts.keys():
            shortcutsFile.write(k + "\n")
            shortcutsFile.write(self.shortcuts[k] + "\n")

        shortcutsFile.close()

    def printShortcuts(self):
        """Prints out all of the shortcuts."""
        for key in self.shortcuts.keys():
            print key + " = " + self.shortcuts[key]