replacing bash
This commit is contained in:
parent
e0ec6cb9a4
commit
c473798003
8 changed files with 321 additions and 0 deletions
132
pythonrewrite/bin2/CryptoBoxPreferences.py
Normal file
132
pythonrewrite/bin2/CryptoBoxPreferences.py
Normal file
|
@ -0,0 +1,132 @@
|
|||
#!/usr/bin/env python
|
||||
"""
|
||||
This Class is derived mainly from 'gimini' project.
|
||||
__version__ = "$Revision: 1.1 $"
|
||||
__author__ = "C.Dutoit <dutoitc@hotmail.com>"
|
||||
__date__ = "2003-2-14"
|
||||
|
||||
Thanx to Dutoit!
|
||||
"""
|
||||
|
||||
from ConfigParser import *
|
||||
import sys, os
|
||||
|
||||
### Set the Preferences filename
|
||||
PREFS_FILENAME="cbx.conf"
|
||||
|
||||
class Preferences:
|
||||
"""
|
||||
This class handle preferences and store them into 'PREFS_FILENAME'
|
||||
|
||||
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.
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Constructor
|
||||
|
||||
@author C.Dutoit <dutoitc@hotmail.com>
|
||||
"""
|
||||
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>
|
||||
"""
|
||||
f=open(PREFS_FILENAME, "w")
|
||||
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:
|
||||
f = open(PREFS_FILENAME, "r")
|
||||
f.close()
|
||||
except:
|
||||
try:
|
||||
f = open(PREFS_FILENAME, "w")
|
||||
f.write("")
|
||||
f.close()
|
||||
except:
|
||||
print "Can't make %s for saving preferences !" % PREFS_FILENAME
|
||||
return
|
||||
|
||||
|
||||
# Read datas
|
||||
self._config=ConfigParser()
|
||||
self._config.read(PREFS_FILENAME)
|
||||
|
||||
#>--------------------------------------------------------------------------
|
||||
|
||||
if __name__ == "__main__":
|
||||
myPP=Preferences()
|
||||
mypref=myPP["ma_preference"]
|
||||
myPP["ma_preference"]="xxx"
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue