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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 357 - (hide annotations) (download) (as text)
Mon Mar 6 06:30:21 2006 UTC (14 years, 7 months ago) by johnpye
File MIME type: text/x-python
File size: 3464 byte(s)
Added work for creating a windows installer
1 johnpye 132 # Preferences module for ASCPY.
2    
3     import ConfigParser, os
4 johnpye 172 import re
5 johnpye 357 import platform
6 johnpye 132
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 johnpye 357 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 johnpye 132 self.n = self.n + 1;
23    
24     if not hasattr(self,'ini'):
25     print "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 "WRITING CONFIG to",self.fname
33     f = open(self.fname, "w");
34     self.ini.write( f );
35    
36 johnpye 173 def getGeometrySizePosition(self,displayname,key):
37 johnpye 172 try:
38     _g = self.ini.get("Geometry:"+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 johnpye 190
48     _i = [int(i) for i in _m.groups()]
49     return tuple(_i)
50 johnpye 172
51 johnpye 173 def setGeometrySizePosition(self,displayname,key,width,height,top,left):
52 johnpye 172 if not self.ini.has_section("Geometry:"+displayname):
53     self.ini.add_section("Geometry:"+displayname)
54     self.ini.set("Geometry:"+displayname,key, "%dx%d+%d+%d" % (width, height, top, left) );
55    
56 johnpye 173 def getGeometryValue(self,displayname,key):
57     try:
58     _g = self.ini.get("Geometry:"+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:"+displayname):
67     self.ini.add_section("Geometry:"+displayname)
68     self.ini.set("Geometry:"+displayname,key,str(value))
69    
70 johnpye 132 def getPreferredUnits(self,key):
71     try:
72     _u = self.ini.get("PreferredUnits",key);
73     except ConfigParser.NoSectionError:
74 johnpye 172 return None
75 johnpye 132 except ConfigParser.NoOptionError:
76 johnpye 172 return None
77     return _u
78 johnpye 132
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 johnpye 319 def getBoolPref(self,sect,key,default=None):
86 johnpye 294 try:
87     _u = self.ini.get(sect,key)
88     except ConfigParser.NoSectionError:
89 johnpye 319 return default
90 johnpye 294 except ConfigParser.NoOptionError:
91 johnpye 319 return default
92 johnpye 294 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 johnpye 356
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 johnpye 294
114 johnpye 132 # Test script:
115     def main():
116     x = Preferences();
117     y = Preferences();
118    
119     print "Units for length: ",x.getPreferredUnits("length");
120     print "Units for time: ",x.getPreferredUnits("time");
121    
122     x.setPreferredUnits("length","m");
123     y.setPreferredUnits("time","hr");
124     print "Units for length: ",y.getPreferredUnits("length");
125     print "Units for time: ",y.getPreferredUnits("time");
126    
127     print "About to delete x"
128     del x;
129     print "Deleted x"
130    
131     y.setPreferredUnits("length","cm");
132    
133     print "About to delete y"
134     del y;
135    
136     print "Deleted y"
137    
138     if __name__ == "__main__":
139     main()

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