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

Annotation of /trunk/pygtk/interface/preferences.py

Parent Directory Parent Directory | Revision Log Revision Log


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

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