forked from dmnfarrell/tkintertable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefs.py
More file actions
138 lines (116 loc) · 4 KB
/
Prefs.py
File metadata and controls
138 lines (116 loc) · 4 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
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
"""
Manages preferences for Table class.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
from __future__ import absolute_import, division, print_function
import os, pickle
class Preferences:
def __init__(self,program,defaults):
"""Find and load the preferences file"""
filename='.'+program+'_preferences'
dirs=self.get_dirs()
self.noprefs = False
try:
for ldir in dirs:
fn=os.path.join(ldir,filename)
if os.path.isfile(fn):
self.load_prefs(fn)
#self.save_prefs()
return
else:
self.noprefs = True
if self.noprefs == True:
raise
except:
# If we didn't find a file then set to default and save
print ('Did not find preferences!!!')
self.prefs=defaults.copy()
self.pref_file=os.path.join(dirs[0],filename)
self.prefs['_prefdir']=dirs[0]
self.prefs['_preffile']=self.pref_file
self.save_prefs()
# Defaults savedir?
if 'HOMEPATH' in os.environ:
self.prefs['datadir']=os.environ['HOMEPATH']
if 'HOME' in os.environ:
self.prefs['datadir']=os.environ['HOME']
if hasattr(self.prefs,'datadir'):
mydocs=os.path.join(self.prefs['datadir'],'My Documents')
if os.path.isdir(mydocs):
self.prefs['datadir']=mydocs
self.save_prefs()
return
def __del__(self):
# Make sure we save the file when killed
self.save_prefs()
return
def set(self,key,value):
# Set a key
self.prefs[key]=value
#self.save_prefs()
return
def get(self,key):
if key in self.prefs:
return self.prefs[key]
return
def delete(self,key):
if key in self.prefs:
del self.prefs[key]
else:
#raise 'No such key',key
pass
self.save_prefs()
return
def get_dirs(self):
"""Compile a prioritised list of all dirs"""
dirs=[]
keys=['HOME','HOMEPATH','HOMEDRIVE']
import os, sys
for key in keys:
if key in os.environ:
dirs.append(os.environ[key])
if 'HOMEPATH' in os.environ:
# windows
dirs.append(os.environ['HOMEPATH'])
# Drives
possible_dirs=["C:\\","D:\\","/"]
for pdir in possible_dirs:
if os.path.isdir(pdir):
dirs.append(pdir)
rdirs=[]
for dirname in dirs:
if os.path.isdir(dirname):
rdirs.append(dirname)
return rdirs
def load_prefs(self,filename):
"""Load prefs"""
self.pref_file = filename
try:
fd = open(filename,'rb')
self.prefs = pickle.load(fd)
#print (self.prefs)
fd.close()
except Exception as e:
print (e)
return
def save_prefs(self):
"""Save prefs"""
try:
fd = open(self.pref_file,'wb')
#print (self.prefs)
pickle.dump(self.prefs, fd, protocol=2)
fd.close()
except:
print ('could not save')
return
return