/[ascend]/trunk/pygtk/preferences.py
ViewVC logotype

Contents of /trunk/pygtk/preferences.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 580 - (show annotations) (download) (as text)
Tue May 9 06:56:55 2006 UTC (13 years, 7 months ago) by johnpye
File MIME type: text/x-python
File size: 3684 byte(s)
Fixed a problem with GTK+ 2.6 on Windows.
1 # Preferences module for ASCPY.
2
3 import ConfigParser, os
4 import re
5 import platform
6
7 class Preferences:
8 __sharedstate = {}
9
10 def __init__(self):
11 self.__dict__ = self.__sharedstate
12
13 if not hasattr(self,'n'):
14 self.n = 0;
15 if platform.system()=="Windows":
16 self.fname = os.path.join(os.environ['APPDATA'],".ascend.ini")
17 else:
18 self.fname = os.path.expanduser("~/.ascend.ini");
19
20 #print "PREFERENCES FILE =",self.fname
21
22 self.n = self.n + 1;
23
24 if not hasattr(self,'ini'):
25 #print "READING/CREATING CONFIG"
26 self.ini = ConfigParser.SafeConfigParser();
27 self.ini.read( [ self.fname ] );
28
29 def __del__(self):
30 self.n = self.n - 1;
31 if self.n==0:
32 print "\rSaving preferences in ",self.fname
33 f = open(self.fname, "w");
34 self.ini.write( f );
35
36 def getGeometrySizePosition(self,displayname,key):
37 try:
38 _g = self.ini.get("Geometry:"+str(displayname),key)
39 except ConfigParser.NoSectionError:
40 return None
41 except ConfigParser.NoOptionError:
42 return None
43 _p = re.compile('^\s*(\d+)[Xx](\d+)\+(-?\d+)\+(-?\d+)\s*$');
44
45 _m = _p.match(_g)
46 #print "MATCH: ",_m.groups()
47 if not _m:
48 return None
49 return [int(i) for i in _m.groups()]
50
51 def setGeometrySizePosition(self,displayname,key,width,height,top,left):
52 if not self.ini.has_section("Geometry:"+str(displayname)):
53 self.ini.add_section("Geometry:"+str(displayname))
54 self.ini.set("Geometry:"+str(displayname),key, "%dx%d+%d+%d" % (width, height, top, left) );
55
56 def getGeometryValue(self,displayname,key):
57 try:
58 _g = self.ini.get("Geometry:"+str(displayname),key)
59 except ConfigParser.NoSectionError:
60 return None
61 except ConfigParser.NoOptionError:
62 return None
63 return int(_g)
64
65 def setGeometryValue(self,displayname,key,value):
66 if not self.ini.has_section("Geometry:"+str(displayname)):
67 self.ini.add_section("Geometry:"+str(displayname))
68 self.ini.set("Geometry:"+str(displayname),key,str(value))
69
70 def getPreferredUnits(self,key):
71 try:
72 _u = self.ini.get("PreferredUnits",key);
73 except ConfigParser.NoSectionError:
74 return None
75 except ConfigParser.NoOptionError:
76 return None
77 return _u
78
79 def setPreferredUnits(self,key,val):
80 if not self.ini.has_section("PreferredUnits"):
81 self.ini.add_section("PreferredUnits")
82 self.ini.set("PreferredUnits",key,val)
83 #print "SET PREFERRED UNITS FOR %s TO %s" % ( key, val )
84
85 def getBoolPref(self,sect,key,default=None):
86 try:
87 _u = self.ini.get(sect,key)
88 except ConfigParser.NoSectionError:
89 return default
90 except ConfigParser.NoOptionError:
91 return default
92 if _u:
93 return True
94 return False
95
96 def setBoolPref(self,sect,key,val):
97 if not self.ini.has_section(sect):
98 self.ini.add_section(sect)
99 if val:
100 val = "1"
101 else:
102 val = ""
103 self.ini.set(sect,key,val)
104
105 def getStringPref(self,sect,key,default=None):
106 try:
107 _u = self.ini.get(sect,key)
108 except ConfigParser.NoSectionError:
109 return default
110 except ConfigParser.NoOptionError:
111 return default
112 return _u;
113
114 def setStringPref(self,sect,key,value):
115 if not self.ini.has_section(sect):
116 self.ini.add_section(sect)
117 self.ini.set(sect,key,str(value))
118
119
120
121 # Test script:
122 def main():
123 x = Preferences();
124 y = Preferences();
125
126 print "Units for length: ",x.getPreferredUnits("length");
127 print "Units for time: ",x.getPreferredUnits("time");
128
129 x.setPreferredUnits("length","m");
130 y.setPreferredUnits("time","hr");
131 print "Units for length: ",y.getPreferredUnits("length");
132 print "Units for time: ",y.getPreferredUnits("time");
133
134 print "About to delete x"
135 del x;
136 print "Deleted x"
137
138 y.setPreferredUnits("length","cm");
139
140 print "About to delete y"
141 del y;
142
143 print "Deleted y"
144
145 if __name__ == "__main__":
146 main()

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22