Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

shlib: drop shelve in lieu of simpler pickle file

No more silly locking problems. It uses Mercurial's atomictempfile class
to manage overwriting the configuration file (write to temp, close, rename).
Uses cPickle to dump/parse the config data, this is simple and efficient.

Changeset 7db597d06ead

Parent bea2cda18f91

by Steve Borho

Changes to one file · Browse files at 7db597d06ead Showing diff from parent bea2cda18f91 Diff from another changeset...

Change 1 of 2 Show Changes Only hggtk/​shlib.py Stacked
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
221
222
223
224
225
226
227
228
229
230
231
232
233
 """  shlib.py - TortoiseHg shell utilities   Copyright (C) 2007 TK Soh <teekaysoh@gmail.com>    This software may be used and distributed according to the terms  of the GNU General Public License, incorporated herein by reference.    """   -import dumbdbm, anydbm -anydbm._defaultmod = dumbdbm -  import os  import sys  import gtk -import shelve +import cPickle  import time  import hgtk  import gobject  from mercurial.i18n import _ +from mercurial import util    class SimpleMRUList(object):   def __init__(self, size=10, reflist=[], compact=True):   self._size = size   self._list = reflist   if compact:   self.compact()     def __iter__(self):   for elem in self._list:   yield elem     def add(self, val):   if val in self._list:   self._list.remove(val)   self._list.insert(0, val)   self.flush()     def get_size(self):   return self._size     def set_size(self, size):   self._size = size   self.flush()     def flush(self):   while len(self._list) > self._size:   del self._list[-1]     def compact(self):   ''' remove duplicate in list '''   newlist = []   for v in self._list:   if v not in newlist:   newlist.append(v)   self._list[:] = newlist      class Settings(object):   version = 1.0     def __init__(self, appname, path=None):   self._appname = appname   self._data = {}   self._path = path and path or self._get_path(appname)   self._audit()   self.read()     def get_value(self, key, default=None, create=False):   if key in self._data:   return self._data[key]   elif create:   self._data[key] = default   return default     def set_value(self, key, value):   self._data[key] = value     def mrul(self, key, size=10):   ''' wrapper method to create a most-recently-used (MRU) list '''   ls = self.get_value(key, [], True)   ml = SimpleMRUList(size=size, reflist=ls)   return ml     def get_keys(self):   return self._data.keys()     def get_appname(self):   return self._appname     def read(self):   self._data.clear() - if not os.path.exists(self._path+'.dat'): - return - dbase = shelve.open(self._path) - try: - self._dbappname = dbase['APPNAME'] - self.version = dbase['VERSION'] - self._data.update(dbase.get('DATA', {})) - except KeyError: - pass - dbase.close() + if os.path.exists(self._path): + try: + f = file(self._path, 'rb') + self._data = cPickle.loads(f.read()) + f.close() + except Exception: + pass     def write(self):   self._write(self._path, self._data)     def _write(self, appname, data): - dbase = shelve.open(self._get_path(appname)) - dbase['VERSION'] = Settings.version - dbase['APPNAME'] = appname - dbase['DATA'] = data - try: - dbase.close() - except IOError: - pass # Don't care too much about permission errors - + s = cPickle.dumps(data) + f = util.atomictempfile(appname, 'wb', None) + f.write(s) + f.rename()     def _get_path(self, appname):   if os.name == 'nt': - return os.path.join(os.environ.get('APPDATA'), 'TortoiseHg', appname) + return os.path.join(os.environ.get('APPDATA'), 'TortoiseHg', + appname)   else:   return os.path.join(os.path.expanduser('~'), '.tortoisehg', - 'settings', appname) + appname)     def _audit(self):   if os.path.exists(os.path.dirname(self._path)):   return   os.makedirs(os.path.dirname(self._path))    def get_system_times():   t = os.times()   if t[4] == 0.0: # Windows leaves this as zero, so use time.clock()   t = (t[0], t[1], t[2], t[3], time.clock())   return t    def set_tortoise_icon(window, thgicon):   ico = get_tortoise_icon(thgicon)   if ico: window.set_icon_from_file(ico)    def get_thg_modifier():   if sys.platform == 'darwin':   return '<Mod1>'   else:   return '<Control>'    def set_tortoise_keys(window):   'Set default TortoiseHg keyboard accelerators'   if sys.platform == 'darwin':   mask = gtk.accelerator_get_default_mod_mask()   mask |= gtk.gdk.MOD1_MASK;   gtk.accelerator_set_default_mod_mask(mask)   mod = get_thg_modifier()   accelgroup = gtk.AccelGroup()   window.add_accel_group(accelgroup)   key, modifier = gtk.accelerator_parse(mod+'w')   window.add_accelerator('thg-close', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)   key, modifier = gtk.accelerator_parse(mod+'q')   window.add_accelerator('thg-exit', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)   key, modifier = gtk.accelerator_parse('F5')   window.add_accelerator('thg-refresh', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)   key, modifier = gtk.accelerator_parse(mod+'Return')   window.add_accelerator('thg-accept', accelgroup, key, modifier,   gtk.ACCEL_VISIBLE)     # connect ctrl-w and ctrl-q to every window   window.connect('thg-close', thgclose)   window.connect('thg-exit', thgexit)    def thgexit(window):   if thgclose(window):   gobject.idle_add(hgtk.thgexit, window)    def thgclose(window):   if hasattr(window, 'should_live'):   if window.should_live():   return False   window.destroy()   return True    def get_tortoise_icon(thgicon):   '''Find a tortoise icon, apply to PyGtk window'''   # The context menu should set this variable   var = os.environ.get('THG_ICON_PATH', None)   paths = var and [ var ] or []   try:   # Else try relative paths from hggtk, the repository layout   fdir = os.path.dirname(__file__)   paths.append(os.path.join(fdir, '..', 'icons'))   # ... or the unix installer layout   paths.append(os.path.join(fdir, '..', '..', '..',   'share', 'pixmaps', 'tortoisehg', 'icons'))   paths.append(os.path.join(fdir, '..', '..', '..', '..',   'share', 'pixmaps', 'tortoisehg', 'icons'))   except NameError: # __file__ is not always available   pass   for p in paths:   path = os.path.join(p, 'tortoise', thgicon)   if os.path.isfile(path):   return path   else:   print _('icon not found'), thgicon   return None    def version():   try:   import __version__   return __version__.version   except ImportError:   return _('unknown')    if os.name == 'nt':   def shell_notify(paths):   try:   from win32com.shell import shell, shellcon   import pywintypes   except ImportError:   return   dirs = []   for path in paths:   abspath = os.path.abspath(path)   if not os.path.isdir(abspath):   abspath = os.path.dirname(abspath)   if abspath not in dirs:   dirs.append(abspath)   # send notifications to deepest directories first   dirs.sort(lambda x, y: len(y) - len(x))   for dir in dirs:   try:   pidl, ignore = shell.SHILCreateFromPath(dir, 0)   except pywintypes.com_error:   return   if pidl is None:   continue   shell.SHChangeNotify(shellcon.SHCNE_UPDATEITEM,   shellcon.SHCNF_IDLIST | shellcon.SHCNF_FLUSH,   pidl, None)  else:   def shell_notify(paths):   pass