Library: fix setting modes.
[nephilim.git] / clSettings.py
blobbabb69df0593fb226f53cef9dbe01e0d26af4930
1 from traceback import print_exc
2 from PyQt4 import QtCore
4 class Settings:
5 fileName=None
6 lines=[]
8 def __init__(self, file='settings.txt'):
9 self.fileName=file
10 self.read()
12 def __del__(self):
13 self.write()
15 def get(self, name, default=None):
16 for line in self.lines:
17 if line[0:len(name)]==name:
18 return line[len(name):].strip()
19 return default
21 def set(self, name, value):
22 newvalue="%s\t%s"%(name,value)
23 for i in xrange(len(self.lines)):
24 line=self.lines[i]
25 if line[0:len(name)]==name:
26 self.lines[i]=newvalue
27 return
28 # new value to write
29 self.lines.append(newvalue)
31 def getIntTuple(self, name):
32 """Note this might return an exception!"""
33 val=self.get(name)
34 i=val.find(' ')
35 x,y=int(val[0:i]), int(val[i:])
36 return x,y
38 def setIntTuple(self, name, val1, val2):
39 self.set(name, "%i %i"%(val1, val2))
41 def read(self):
42 try:
43 self.lines=[]
44 f=open(self.fileName)
45 while True:
46 line=f.readline()
47 if line=='': break
48 self.lines.append(line.strip().replace('$NEWLINE', '\n'))
49 f.close()
50 except IOError:
51 pass
52 except:
53 print_exc()
54 def write(self):
55 f=open(self.fileName, 'wb')
56 for line in self.lines:
57 f.write("%s\n"%(line.replace('\n', '$NEWLINE')))
58 f.close()
61 settings=Settings()