2 Wrapper around ConfigParser to manage testcases configuration.
4 @author rsalveti@linux.vnet.ibm.com (Ricardo Salveti de Araujo)
7 from ConfigParser
import ConfigParser
8 from StringIO
import StringIO
10 import types
, re
, string
11 from autotest_lib
.client
.common_lib
import utils
13 __all__
= ['config_loader']
17 Base class of the configuration parser
19 def __init__(self
, cfg
, tmpdir
='/tmp', raise_errors
=False):
21 Instantiate ConfigParser and provide the file like object that we'll
22 use to read configuration data from.
23 @param cfg: Where we'll get configuration data. It can be either:
24 * A URL containing the file
25 * A valid file path inside the filesystem
26 * A string containing configuration data
27 @param tmpdir: Where we'll dump the temporary conf files.
28 @param raise_errors: Whether config value absences will raise
29 ValueError exceptions.
32 self
.parser
= ConfigParser()
33 # Raise errors when lacking values
34 self
.raise_errors
= raise_errors
35 # File is already a file like object
36 if hasattr(cfg
, 'read'):
38 self
.parser
.readfp(self
.cfg
)
39 elif isinstance(cfg
, types
.StringTypes
):
40 # Config file is a URL. Download it to a temp dir
41 if cfg
.startswith('http') or cfg
.startswith('ftp'):
42 self
.cfg
= path
.join(tmpdir
, path
.basename(cfg
))
43 utils
.urlretrieve(cfg
, self
.cfg
)
44 self
.parser
.read(self
.cfg
)
45 # Config is a valid filesystem path to a file.
46 elif path
.exists(path
.abspath(cfg
)):
48 self
.cfg
= path
.abspath(cfg
)
49 self
.parser
.read(self
.cfg
)
51 e_msg
= 'Invalid config file path: %s' % cfg
53 # Config file is just a string, convert it to a python file like
54 # object using StringIO
56 self
.cfg
= StringIO(cfg
)
57 self
.parser
.readfp(self
.cfg
)
60 def get(self
, section
, option
, default
=None):
62 Get the value of a option.
64 Section of the config file and the option name.
65 You can pass a default value if the option doesn't exist.
67 @param section: Configuration file section.
68 @param option: Option we're looking after.
69 @default: In case the option is not available and raise_errors is set
70 to False, return the default.
72 if not self
.parser
.has_option(section
, option
):
74 raise ValueError('No value for option %s. Please check your '
75 'config file "%s".' % (option
, self
.cfg
))
79 return self
.parser
.get(section
, option
)
82 def set(self
, section
, option
, value
):
86 This change is not persistent unless saved with 'save()'.
88 if not self
.parser
.has_section(section
):
89 self
.parser
.add_section(section
)
90 return self
.parser
.set(section
, option
, value
)
93 def remove(self
, section
, option
):
97 if self
.parser
.has_section(section
):
98 self
.parser
.remove_option(section
, option
)
103 Save the configuration file with all modifications
107 fileobj
= file(self
.cfg
, 'w')
109 self
.parser
.write(fileobj
)
114 def check(self
, section
):
116 Check if the config file has valid values
118 if not self
.parser
.has_section(section
):
119 return False, "Section not found: %s"%(section)
121 options
= self
.parser
.items(section
)
122 for i
in range(options
.__len
__()):
123 param
= options
[i
][0]
124 aux
= string
.split(param
, '.')
127 return False, "Invalid parameter syntax at %s"%(param)
129 if not self
.check_parameter(aux
[0], options
[i
][1]):
130 return False, "Invalid value at %s"%(param)
135 def check_parameter(self
, param_type
, parameter
):
137 Check if a option has a valid value
139 if parameter
== '' or parameter
== None:
141 elif param_type
== "ip" and self
.__isipaddress
(parameter
):
143 elif param_type
== "int" and self
.__isint
(parameter
):
145 elif param_type
== "float" and self
.__isfloat
(parameter
):
147 elif param_type
== "str" and self
.__isstr
(parameter
):
153 def __isipaddress(self
, parameter
):
155 Verify if the ip address is valid
157 @param ip String: IP Address
158 @return True if a valid IP Address or False
160 octet1
= "([1-9][0-9]{,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
161 octet
= "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
162 pattern
= "^" + octet1
+ "\.(" + octet
+ "\.){2}" + octet
+ "$"
163 if re
.match(pattern
, parameter
) == None:
169 def __isint(self
, parameter
):
172 except Exception, e_stack
:
177 def __isfloat(self
, parameter
):
180 except Exception, e_stack
:
185 def __isstr(self
, parameter
):
188 except Exception, e_stack
: