1 """
2 Manages preferences for Table class.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """
18
20
22
23
24
25 import os
26 filename='.'+program+'_preferences'
27 dirs=self.get_dirs()
28 self.noprefs = False
29 try:
30 for ldir in dirs:
31 fn=os.path.join(ldir,filename)
32
33 if os.path.isfile(fn):
34 self.load_prefs(fn)
35 self.save_prefs()
36 return
37 else:
38 self.noprefs = True
39 if self.noprefs == True:
40 raise
41 except:
42
43
44
45 print 'Did not find preferences!!!'
46 self.prefs=defaults.copy()
47 print dirs
48 self.pref_file=os.path.join(dirs[0],filename)
49 self.prefs['_prefdir']=dirs[0]
50 self.prefs['_preffile']=self.pref_file
51 self.save_prefs()
52
53
54
55
56
57 if os.environ.has_key('HOMEPATH'):
58 self.prefs['datadir']=os.environ['HOMEPATH']
59 if os.environ.has_key('HOME'):
60 self.prefs['datadir']=os.environ['HOME']
61
62
63
64
65 if hasattr(self.prefs,'datadir'):
66 mydocs=os.path.join(self.prefs['datadir'],'My Documents')
67 if os.path.isdir(mydocs):
68 self.prefs['datadir']=mydocs
69
70
71
72
73 self.save_prefs()
74 return
75
76
77
78
79
86
87
88
89
90
91 - def set(self,key,value):
98
99
100
101
102
104
105
106
107 if self.prefs.has_key(key):
108 return self.prefs[key]
109 else:
110 raise NameError,'No such key'
111 return
112
113
114
115
116
118 if self.prefs.has_key(key):
119 del self.prefs[key]
120 else:
121 raise 'No such key',key
122 self.save_prefs()
123 return
124
125
126
127
128
130
131
132
133 dirs=[]
134 keys=['HOME','HOMEPATH','HOMEDRIVE']
135 import os, sys
136 for key in keys:
137 if os.environ.has_key(key):
138 dirs.append(os.environ[key])
139
140 if os.environ.has_key('HOMEPATH'):
141
142
143
144 dirs.append(os.environ['HOMEPATH'])
145
146
147
148 possible_dirs=["C:\\","D:\\","/"]
149 for pdir in possible_dirs:
150 if os.path.isdir(pdir):
151 dirs.append(pdir)
152
153
154
155 rdirs=[]
156 for dirname in dirs:
157 if os.path.isdir(dirname):
158 rdirs.append(dirname)
159 return rdirs
160
161
162
163
164
166
167
168
169 self.pref_file=filename
170 print "loading prefs from ",self.pref_file
171 import pickle
172 try:
173 fd=open(filename)
174 self.prefs=pickle.load(fd)
175 fd.close()
176 except:
177 fd.close()
178 fd=open(filename,'rb')
179 self.prefs=pickle.load(fd)
180 fd.close()
181 return
182
183
184
185
186
188
189
190
191 import pickle
192
193 try:
194 fd=open(self.pref_file,'w')
195 except:
196 print 'could not save'
197 return
198
199 pickle.dump(self.prefs,fd)
200 fd.close()
201 return
202