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
|
#!/usr/bin/env python
###########################################################################
#
# xasyOptions provides a mechanism for storing and restoring a user's
# preferences.
#
#
# Author: Orest Shardt
# Created: June 29, 2007
#
###########################################################################
import pickle
import sys,os
import errno
defaultOptions = {
'asyPath':'asy',
'showDebug':False,
'showGrid':False,
'gridX':10,
'gridY':10,
'gridColor':'#eeeeee',
'showAxes':True,
'axisX':10,
'axisY':10,
'axesColor':'#cccccc',
'tickColor':'#eeeeee',
'defPenOptions':'',
'defPenColor':'#000000',
'defPenWidth':1.0,
'externalEditor':''
}
if sys.platform[:3] == "win":
defaultOptions['externalEditor'] = "%PROGRAMFILES%\Windows NT\Accessories\wordpad.exe"
else:
defaultOptions['externalEditor'] = "emacs"
options = defaultOptions.copy()
def settingsFileLocation():
folder = ""
try:
folder = os.path.expanduser("~/.asy/")
except:
pass
return os.path.normcase(os.path.join(folder,"xasy.conf"))
def setAsyPathFromWindowsRegistry():
try:
import _winreg as registry
#test both registry locations
try:
key = registry.OpenKey(registry.HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Asymptote")
options['asyPath'] = registry.QueryValueEx(key,"Path")[0]+"\\asy.exe"
registry.CloseKey(key)
except:
key = registry.OpenKey(registry.HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Asymptote")
options['asyPath'] = registry.QueryValueEx(key,"InstallLocation")[0]+"\\asy.exe"
registry.CloseKey(key)
except:
#looks like asy is not installed or this isn't Windows
pass
def setDefaults():
global options
options = defaultOptions.copy()
if sys.platform[:3] == 'win': #for windows, wince, win32, etc
setAsyPathFromWindowsRegistry()
save()
def load():
global options
fileName = settingsFileLocation()
if not os.path.exists(fileName):
#make folder
thedir = os.path.dirname(fileName)
if not os.path.exists(thedir):
try:
os.makedirs(thedir)
except:
raise Exception("Could not create configuration folder")
if not os.path.isdir(thedir):
raise Exception("Configuration folder path does not point to a folder")
setDefaults()
try:
f = open(fileName,"rb")
newOptions = pickle.load(f)
for key in options.keys():
if type(newOptions[key]) != type(options[key]):
raise Exception("Bad type for entry in xasy settings")
options = newOptions
except:
setDefaults()
def save():
global options
fileName = settingsFileLocation()
try:
f = open(fileName,"wb")
pickle.dump(options,f)
f.close()
except:
raise Exception("Error saving preferences")
load()
if __name__=='__main__':
print (settingsFileLocation())
print ("Current content")
load()
print ("Setting defaults")
setDefaults()
save()
load()
options['showAxes'] = options['showGrid'] = False
save()
print ("Set to False")
load()
options['showAxes'] = options['showGrid'] = True
save()
print ("Set to True")
load()
print (options)
|