oskusalerma / blyte

Screenplay writing program

This URL has Read+Write access

blyte / autocompletion.py
100644 97 lines (69 sloc) 2.402 kb
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import config
import mypickle
import screenplay
import util
 
# manages auto completion information for a single script.
class AutoCompletion:
    def __init__(self):
        # type configs, key = line type, value = Type
        self.types = {}
 
        # element types
        t = Type(screenplay.SCENE)
        self.types[t.ti.lt] = t
 
        t = Type(screenplay.CHARACTER)
        self.types[t.ti.lt] = t
 
        t = Type(screenplay.TRANSITION)
        t.items = [
            "CUT TO:",
            "DISSOLVE TO:",
            "FADE IN:",
            "FADE OUT",
            "FADE TO BLACK",
            "MATCH CUT TO:"
            ]
        self.types[t.ti.lt] = t
 
        self.refresh()
 
    # load config from string 's'. does not throw any exceptions, silently
    # ignores any errors, and always leaves config in an ok state.
    def load(self, s):
        vals = mypickle.Vars.makeVals(s)
 
        for t in self.types.itervalues():
            t.load(vals, "AutoCompletion/")
 
        self.refresh()
        
    # save config into a string and return that.
    def save(self):
        s = ""
 
        for t in self.types.itervalues():
            s += t.save("AutoCompletion/")
 
        return s
            
    # fix up invalid values and uppercase everything.
    def refresh(self):
        for t in self.types.itervalues():
            tmp = []
 
            for v in t.items:
                v = util.upper(util.toInputStr(v)).strip()
 
                if len(v) > 0:
                    tmp.append(v)
 
            t.items = tmp
 
    # get type's Type, or None if it doesn't exist.
    def getType(self, lt):
        return self.types.get(lt)
 
# auto completion info for one element type
class Type:
    cvars = None
 
    def __init__(self, lt):
 
        # pointer to TypeInfo
        self.ti = config.lt2ti(lt)
 
        if not self.__class__.cvars:
            v = self.__class__.cvars = mypickle.Vars()
 
            v.addBool("enabled", True, "Enabled")
            v.addList("items", [], "Items",
                      mypickle.StrLatin1Var("", "", ""))
 
            v.makeDicts()
            
        self.__class__.cvars.setDefaults(self)
 
    def save(self, prefix):
        prefix += "%s/" % self.ti.name
 
        return self.cvars.save(prefix, self)
 
    def load(self, vals, prefix):
        prefix += "%s/" % self.ti.name
        
        self.cvars.load(vals, prefix, self)