/usr/lib/python2.7/dist-packages/framework/configparse.py is in fso-frameworkd 0.10.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #!/usr/bin/env python
"""
freesmartphone.org Framework Daemon
(C) 2008 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
(C) 2008 Openmoko, Inc.
GPLv2 or later
Module: configparse
"""
__version__ = "1.0.0"
import ConfigParser
import types
#----------------------------------------------------------------------------#
class SmartConfigParser( ConfigParser.SafeConfigParser ):
#----------------------------------------------------------------------------#
"""
A smart config parser
"""
def __init__( self, filename = None ):
ConfigParser.SafeConfigParser.__init__( self )
self.filename = filename
if filename is not None:
self.read( filename )
def sync( self ):
# FIXME if we have a mainloop, collect sync calls and write deferred
assert self.filename is not None, "no filename given yet"
ConfigParser.SafeConfigParser.write( self, open(self.filename, "w" ) )
def getOptions( self, section ):
try:
options = self.options( section )
except ConfigParser.NoSectionError:
return []
else:
return options
def getValue( self, section, key, default=None, set=False, getmethod=ConfigParser.SafeConfigParser.get ):
try:
value = getmethod( self, section, key )
except ConfigParser.NoSectionError:
if set:
self.add_section( section )
self.set( section, key, str(default) )
self.sync()
return default
except ConfigParser.NoOptionError:
if set:
self.set( section, key, str(default) )
self.sync()
return default
else:
return value
def setValue( self, section, key, value ):
try:
self.set( section, key, str(value) )
except ConfigParser.NoSectionError:
self.add_section( section )
self.set( section, key, str(value) )
self.sync()
def getBool( self, section, key, default=None, set=False ):
return self.getValue( section, key, default, getmethod = ConfigParser.SafeConfigParser.getboolean )
def getFloat( self, section, key, default=None, set=False ):
return self.getValue( section, key, default, getmethod = ConfigParser.SafeConfigParser.getfloat )
def getInt( self, section, key, default=None, set=False ):
return self.getValue( section, key, default, getmethod = ConfigParser.SafeConfigParser.getint )
def getSetDefault( self, section, key, default ):
"""
The type to return is gathered from the type of the default value.
If the section does not exist, it is created.
If the key does not exist, it is created with the default value.
"""
value = self.getValue( section, key, default, True )
return self._typedValue( value, default )
def getDefault( self, section, key, default ):
"""
The type to return is gathered from the type of the default value.
"""
value = self.getValue( section, key, default, False )
#print "value =", value, "returning", self._typedValue( value, default )
return self._typedValue( value, default )
def _typedValue( self, value, default ):
valuetype = type( default )
if valuetype == types.StringType:
return str( value )
elif valuetype == types.IntType:
return int( value )
elif valuetype == types.BooleanType:
return bool( value )
elif valuetype == types.FloatType:
return float( value )
else: # can't guess type, return it unchanged
return value
#----------------------------------------------------------------------------#
if __name__ == "__main__":
#----------------------------------------------------------------------------#
FILENAME = "/tmp/test.ini"
import os
try:
os.unlink( FILENAME )
except OSError: # might not exist
pass
c = SmartConfigParser( FILENAME )
print "TESTING...",
# reading a non existent key
assert c.getDefault( "foo.bar", "key", None ) == None
assert c.getDefault( "foo.bar", "key", False ) == False
assert c.getDefault( "foo.bar", "key", 10 ) == 10
assert c.getDefault( "foo.bar", "key", 10.0 ) == 10.0
assert c.getDefault( "foo.bar", "key", "10" ) == "10"
# getset a key
assert c.getSetDefault( "foo.bar", "key", 500 ) == 500
assert c.getDefault( "foo.bar", "key", 100 ) == 500
print "OK."
|