File: StyleSetter.py

package info (click to toggle)
pype 2.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 832 kB
  • ctags: 953
  • sloc: python: 9,007; makefile: 54
file content (150 lines) | stat: -rw-r--r-- 4,669 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
## from STCStyleEditor import initSTC
## initSTC(self, stylefile, language)

import wx
import wx.stc

cn = 'Courier New'
faces = {'times': cn, 'mono' : cn, 'helv' : cn, 'other': cn, 'lucd': cn,
         'size' : 10, 'size2': 10, 'lnsize': 10, "backcol": "#FFFFFF"}

def partition(st, sep):
    if sep not in st:
        return st, '', ''
    x = st.find(sep)
    return st[:x], sep, st[x+len(sep):]

def rpartition(st, sep):
    if sep not in st:
        return '', '', st
    x = st.rfind(sep)
    return st[:x], sep, st[x+len(sep):]

_default = {34:'fore:#0000FF,back:#FFFF88,bold', 35:'fore:#FF0000,back:#FFFF88,bold'}

def get_style(stylefile, language, profile=''):
    #gets the main styling information for [style.<language>[.profile]]
    found1 = 0
    dct = {}
    if profile == 'default':
        dct.update(_default)
    line_starts1 = 'style.%s.'%language
    line_starts2 = 'setting.%s.'%language
    if profile:
        profile = '.' + profile
    looking_for = '[style.%s%s]'%(language, profile)
    for line in open(stylefile):
        if line.strip() == looking_for:
            found1 = 1
        elif found1 and (line.startswith(line_starts1) or line.startswith(line_starts2)):
            left, _, right = partition(line, '=')
            if not _:
                continue
            toss, _, num = rpartition(left, '.')
            if toss and _ and num:
                try:
                    num = int(num, 10)
                except:
                    continue
            dct[num] = right.strip()%faces
        elif found1 and not line.strip():
            pass
        elif found1:
            break
    return dct

def get_extra(stylefile, language):
    found2 = 0
    dct = {}
    looking_for = '[%s]'%language
    for line in open(stylefile):
        if line.strip() == looking_for:
            found2 = 1
        elif found2 and not line.startswith('[') and line.strip():
            left, _, right = partition(line, '=')
            if left and _:
                dct[left] = right.strip()
        elif found2 and not line.strip():
            pass
        elif found2:
            break
    return dct

def initSTC(stc, stylefile, language, custom=''):
    #get the styling information
    styles = get_style(stylefile, language, 'default')
    styles.update(get_style(stylefile, language, custom))
    
    ## print "got styling information:", [i.strip() for i in lines1]
    
    #get any extra bits

    extra = get_extra(stylefile, language)
            
    #get the actual lexer
    lexer = wx.stc.STC_LEX_NULL
    if 'lexer' in extra:
        lex = extra['lexer']
        if lex.startswith('wx'):
            lex = lex[2:]
            lex.lstrip('.')
            if hasattr(wx.stc, lex):
                lexer = getattr(wx.stc, lex)
    
    stc.StyleResetDefault()
    stc.ClearDocumentStyle()
    stc.SetLexer(lexer)
    stc.SetKeyWords(0, extra.get('keywords', ''))
    stc.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, 'fore:#000000,face:%(mono)s,size:%(size)d'%faces)
    stc.StyleClearAll()
    
    for num, style in styles.iteritems():
        if num >= 0:
            stc.StyleSetSpec(num, style)
        elif num == -1:
            setSelectionColour(stc, style)
        elif num == -2:
            setCursorColour(stc, style)
        elif num == -3:
            setEdgeColour(stc, style)
    
    stc.Colourise(0, stc.GetTextLength())

#from STCStyleEditor.py

def strToCol(strCol):
    assert len(strCol) == 7 and strCol[0] == '#', 'Not a valid colour string'
    return wx.Colour(int(strCol[1:3], 16),
                     int(strCol[3:5], 16),
                     int(strCol[5:7], 16))

def setSelectionColour(stc, style):
    names, values = parseProp(style)
    if 'fore' in names:
        stc.SetSelForeground(True, strToCol(values['fore']))
    if 'back' in names:
        stc.SetSelBackground(True, strToCol(values['back']))

def setCursorColour(stc, style):
    names, values = parseProp(style)
    if 'fore' in names:
        stc.SetCaretForeground(strToCol(values['fore']))

def setEdgeColour(stc, style):
    names, values = parseProp(style)
    if 'fore' in names:
        stc.SetEdgeColour(strToCol(values['fore']))

def parseProp(prop):
    items = prop.split(',')
    names = []
    values = {}
    for item in items:
        nameVal = item.split(':')
        names.append(nameVal[0].strip())
        if len(nameVal) == 1:
            values[nameVal[0]] = ''
        else:
            values[nameVal[0]] = nameVal[1].strip()
    return names, values