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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 173 - (hide annotations) (download) (as text)
Sat Jan 7 11:20:32 2006 UTC (14 years, 9 months ago) by johnpye
File MIME type: text/x-python
File size: 2650 byte(s)
PyGTK interface now remembers size of errors/warnings pane as well.
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     return tuple(int(i) for i in _m.groups())
41    
42 johnpye 173 def setGeometrySizePosition(self,displayname,key,width,height,top,left):
43 johnpye 172 if not self.ini.has_section("Geometry:"+displayname):
44     self.ini.add_section("Geometry:"+displayname)
45     self.ini.set("Geometry:"+displayname,key, "%dx%d+%d+%d" % (width, height, top, left) );
46    
47 johnpye 173 def getGeometryValue(self,displayname,key):
48     try:
49     _g = self.ini.get("Geometry:"+displayname,key)
50     except ConfigParser.NoSectionError:
51     return None
52     except ConfigParser.NoOptionError:
53     return None
54     return int(_g)
55    
56     def setGeometryValue(self,displayname,key,value):
57     if not self.ini.has_section("Geometry:"+displayname):
58     self.ini.add_section("Geometry:"+displayname)
59     self.ini.set("Geometry:"+displayname,key,str(value))
60    
61 johnpye 132 def getPreferredUnits(self,key):
62     try:
63     _u = self.ini.get("PreferredUnits",key);
64     except ConfigParser.NoSectionError:
65 johnpye 172 return None
66 johnpye 132 except ConfigParser.NoOptionError:
67 johnpye 172 return None
68     return _u
69 johnpye 132
70     def setPreferredUnits(self,key,val):
71     if not self.ini.has_section("PreferredUnits"):
72     self.ini.add_section("PreferredUnits")
73     self.ini.set("PreferredUnits",key,val)
74     #print "SET PREFERRED UNITS FOR %s TO %s" % ( key, val )
75    
76     # Test script:
77     def main():
78     x = Preferences();
79     y = Preferences();
80    
81     print "Units for length: ",x.getPreferredUnits("length");
82     print "Units for time: ",x.getPreferredUnits("time");
83    
84     x.setPreferredUnits("length","m");
85     y.setPreferredUnits("time","hr");
86     print "Units for length: ",y.getPreferredUnits("length");
87     print "Units for time: ",y.getPreferredUnits("time");
88    
89     print "About to delete x"
90     del x;
91     print "Deleted x"
92    
93     y.setPreferredUnits("length","cm");
94    
95     print "About to delete y"
96     del y;
97    
98     print "Deleted y"
99    
100     if __name__ == "__main__":
101     main()

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