cryptonas-branches/pythonrewrite/bin2/CryptoBoxPreferences.py

132 lines
3.5 KiB
Python
Raw Normal View History

2006-08-16 09:17:44 +02:00
#!/usr/bin/env python
2006-08-16 13:07:57 +02:00
'''
read and write configuration
2006-08-16 09:17:44 +02:00
2006-08-16 13:07:57 +02:00
This Class is derived mainly from 'gimini' project.
Thanx to Dutoit! <dutoitc(at)hotmail.com>
'''
2006-08-16 09:17:44 +02:00
from ConfigParser import *
import sys, os
### Set the Preferences filename
2006-08-16 13:07:57 +02:00
2006-08-16 09:17:44 +02:00
class Preferences:
"""
2006-08-16 13:07:57 +02:00
This class handles preferences and stores them into given configfile
2006-08-16 09:17:44 +02:00
To use it :
- instanciate a Preferences object :
myPP=Preferences()
- to get a preference :
mypref=myPP["ma_preference"]
- to set a preference :
myPP["ma_preference"]=xxx
The preferences are automatically loaded on the first instanciation of this
class and are saved when a value is added or changed automatically, too.
"""
2006-08-16 13:07:57 +02:00
def __init__(self, filename):
2006-08-16 09:17:44 +02:00
"""
Constructor
@author C.Dutoit <dutoitc@hotmail.com>
"""
2006-08-16 13:07:57 +02:00
self.prefsfilename = filename
2006-08-16 09:17:44 +02:00
self._config = None
self.__loadConfig()
#>--------------------------------------------------------------------------
def __getitem__(self, name):
"""
Return the preferences for the given item
@param String name : Name of the item for which we return a value
@return String : value of the pref, or None if inexistant
@since 1.1.2.7
@author C.Dutoit <dutoitc@hotmail.com>
"""
if not self._config.has_section("Main"):
print "No section: \"[Main]\""
return None
try:
return self._config.get("Main", name)
except NoOptionError:
print "No such option: \"" + name +"\""
return None
#>--------------------------------------------------------------------------
def __setitem__(self, name, value):
"""
Return the preferences for the given item
@param String name : Name of the item WITHOUT SPACES
@param String Value : Value for the given name
@raises TypeError : if the name contains spaces
@since 1.1.2.7
@author C.Dutoit <dutoitc@hotmail.com>
"""
# Add 'Main' section ?
if not self._config.has_section("Main"):
self._config.add_section("Main")
if " " in list(name):
raise TypeError, "Name cannot contain a space"
# Save
self._config.set("Main", name, str(value))
self.__saveConfig()
#>--------------------------------------------------------------------------
def __saveConfig(self):
"""
Save datas to config file
@since 1.1.2.5
@author C.Dutoit <dutoitc@hotmail.com>
"""
2006-08-16 13:07:57 +02:00
f=open(self.prefsfilename, "w")
2006-08-16 09:17:44 +02:00
self._config.write(f)
f.close()
#>--------------------------------------------------------------------------
def __loadConfig(self):
"""
Load datas from config file
@since 1.1.2.5
@author C.Dutoit <dutoitc@hotmail.com>
"""
# Make sure that the configuration file exist
try:
2006-08-16 13:07:57 +02:00
f = open(self.prefsfilename, "r")
2006-08-16 09:17:44 +02:00
f.close()
except:
try:
2006-08-16 13:07:57 +02:00
f = open(self.prefsfilename, "w")
2006-08-16 09:17:44 +02:00
f.write("")
f.close()
except:
2006-08-16 13:07:57 +02:00
print "Can't make %s for saving preferences !" % self.prefsfilename
2006-08-16 09:17:44 +02:00
return
# Read datas
self._config=ConfigParser()
2006-08-16 13:07:57 +02:00
self._config.read(self.prefsfilename)
2006-08-16 09:17:44 +02:00
#>--------------------------------------------------------------------------
if __name__ == "__main__":
myPP=Preferences()
mypref=myPP["ma_preference"]
myPP["ma_preference"]="xxx"