-
Notifications
You must be signed in to change notification settings - Fork 25
/
config.py
220 lines (177 loc) · 6.03 KB
/
config.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# The Admin4 Project
# (c) 2013-2022 Andreas Pflug
#
# Licensed under the Apache License,
# see LICENSE.TXT for conditions of usage
import wx
import logger
from wh import StringType, evalAsPython
ignoreStoredPositions=False
class Config(wx.Config):
"""
OSX: ~/Library/Preferences/<name> Preferences
"""
def __init__(self, name):
wx.Config.__init__(self, name, style = wx.CONFIG_USE_LOCAL_FILE)
def getServers(self):
return self.Read("Servers", [])
def Decorate(self, name, obj=None, subname=None):
if subname:
return "%s/%s" % (name, subname)
if obj:
return "%s/%s" % (obj.__module__, name)
else:
return name
def Read(self, name, default="", obj=None, subname=None):
"""
Read(name, default="", obj=None)
Read a config value <name>
config name might be decorated with <obj> module name
"""
val=super(Config, self).Read(self.Decorate(name, obj, subname))
if not val:
return default
if not isinstance(default, StringType):
py=evalAsPython(val)
if py != None:
val=py
else:
logger.debug("Couldn't pythonize '%s'", val)
if val == None:
return default
return val
def Write(self, name, val, obj=None, subname=None):
"""
Write(name, value, obj=None)
Write a config value <name>
config name might be decorated with <obj> module name
"""
super(Config, self).Write(self.Decorate(name, obj, subname), str(val))
self.Flush()
def getName(self, aspect, module, name):
if not isinstance(module, StringType):
if not name:
if hasattr(module, 'name'):
name=module.name
elif hasattr(module, 'resname'):
name=module.resname
module=module.__module__
name="%s/%s:%s" % (module, name, aspect)
return name.replace('.', '/')
def getWinName(self, win):
if isinstance(win, wx.Frame):
cls="Frame"
else:
cls="Dialog"
return self.getName(cls, win.__module__, win.__class__.__name__)
def storeWindowPositions(self, win):
name=self.getWinName(win)
size=win.GetSize()
pos=win.GetPosition()
if win.GetParent():
pos -= win.GetParent().GetPosition()
self.Write("%sPosition" % name, ((size.x, size.y), (pos.x, pos.y)))
if hasattr(win, "manager"):
txt=win.manager.SavePerspective()
self.Write("%sPerspective" % name, txt)
def GetPerspective(self, win):
if ignoreStoredPositions:
return ""
name=self.getWinName(win)
return self.Read("%sPerspective" % name, "")
def getWindowPositions(self, win):
if ignoreStoredPositions:
return None, None
name=self.getWinName(win)
size, pos=self.Read("%sPosition" % name, (None, None))
xmax = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)
ymax = wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)
if size and (size[0] < 30 or size[1] < 30):
size=None
if size:
xmax -= size[0]
ymax -= size[1]
else:
xmax -= 30
ymax -= 30
if pos and win.GetParent():
pp = win.GetParent().GetPosition()
pos = (pos[0]+pp.x, pos[1]+pp.y)
if pos and (pos[0] < 0 or pos[0] > xmax or pos[1] < 0 or pos[1] > ymax):
pos=None
return size, pos
def restoreGridPositions(self, grid, module=None, name='Grid'):
if ignoreStoredPositions:
return
if not module:
module=grid
name=self.getName("ColumnWidths", module, name)
colWidths=evalAsPython(self.Read(name, "{}"), {})
for i in range(grid.GetNumberCols()):
colname=grid.GetColLabelValue(i)
width=colWidths.get(colname)
if width:
grid.SetColSize(i, width)
def storeGridPositions(self, grid, module=None, name='Grid'):
if not module:
module=grid
name=self.getName("ColumnWidths", module, name)
colWidths=evalAsPython(self.Read(name, "{}"), {})
for i in range(grid.GetNumberCols()):
colname=grid.GetColLabelValue(i)
width=grid.GetColSize(i)
colWidths[colname] = width
self.Write(name, colWidths)
def restoreListviewPositions(self, listview, module, name=None):
if ignoreStoredPositions:
return
colcount=listview.GetColumnCount()
if colcount > 1:
colWidths=self.Read(self.getName("ColumnWidths", module, name), None)
if not colWidths:
return
if isinstance(colWidths, list):
for col in range(colcount):
if col >= len(colWidths):
return
listview.SetColumnWidth(col, colWidths[col])
elif isinstance(colWidths, dict):
for col in range(colcount):
colname = listview.GetColumn(col).GetText()
w=colWidths.get(colname)
if w != None:
listview.SetColumnWidth(col, w)
else:
logger.debug("Strange ColumnWidths format %s", str(colWidths))
def storeListviewPositions(self, listview, module, name=None):
colcount=listview.GetColumnCount()
if colcount > 1:
colWidths={}
for col in range(colcount):
colname = listview.GetColumn(col).GetText()
colWidths[colname] = listview.GetColumnWidth(col)
self.Write(self.getName("ColumnWidths", module, name), colWidths)
def existsServer(self, dlg, name):
cls=dlg.moduleClass()
servers=self.getServers()
return "%s/%s"%(cls,name) in servers
def storeServerSettings(self, dlg, settings):
settings=settings
cls=dlg.moduleClass()
name="%s/%s" % (cls, settings['name'])
settings['class'] = cls
servers=self.getServers()
if name not in servers:
servers.append(name)
self.Write("Servers", str(servers))
self.Write("Server-%s" % name, settings)
def getServerSettings(self, sname):
settings=self.Read("Server-%s" % sname, {})
return settings
def getHintCfg(self, hint, module):
h=module.__module__.split('.')
return "%s/%s" % ("/".join(h[:-1]), hint)
def GetWantHint(self, hint, module):
return self.Read("Hints", True, None, self.getHintCfg(hint, module))
def SetWantHint(self, hint, module, how):
self.Write("Hints", how, None, self.getHintCfg(hint, module))