Configuration defaults, read/write, and template file creation for Hooke.
We start with the system files, and work our way to the more specific user files, so the user can override the sysadmin who in turn overrides the developer defaults.
A wrapper around configparser.RawConfigParser.
You will probably only need .read and .write.
Examples
>>> import pprint
>>> import sys
>>> c = HookeConfigParser(default_settings=DEFAULT_SETTINGS)
>>> c.write(sys.stdout)
# Default environmental conditions in case they are not specified in
# the force curve data. Configuration options in this section are
# available to every plugin.
[conditions]
# Temperature in Kelvin
temperature = 301
<BLANKLINE>
# Configure loggers, see
# http://docs.python.org/library/logging.html#configuration-file-format
[loggers]
# Hooke only uses the hooke logger, but other included modules may
# also use logging and you can configure their loggers here as well.
keys = root, hooke
...
class:HookeConfigParser automatically converts typed settings.
>>> section = 'test conversion'
>>> c = HookeConfigParser(default_settings=[
... Setting(section),
... Setting(section, option='my string', value='Lorem ipsum', type='string'),
... Setting(section, option='my bool', value=True, type='bool'),
... Setting(section, option='my int', value=13, type='int'),
... Setting(section, option='my float', value=3.14159, type='float'),
... ])
>>> pprint.pprint(c.items(section))
[('my string', u'Lorem ipsum'),
('my bool', True),
('my int', 13),
('my float', 3.1415...)]
However, the regular .get() is not typed. Users are encouraged to use the standard .get*() methods.
>>> c.get('test conversion', 'my bool')
u'True'
>>> c.getboolean('test conversion', 'my bool')
True
Methods
add_section | |
defaults | |
get | |
getboolean | |
getfloat | |
getint | |
has_option | |
has_section | |
items | |
options | |
optionxform | |
read | |
readfp | |
remove_option | |
remove_section | |
sections | |
set | |
write |
Return a list of tuples with (name, value) for each option in the section.
Overrides lowercasing behaviour of ConfigParser.RawConfigParser.optionxform().
Read and parse a filename or a list of filenames.
If filenames is None, it defaults to ._config_paths. If a filename is not in ._config_paths, it gets appended to the list. We also run os.path.expanduser() on the input filenames internally so you don’t have to worry about it.
Files that cannot be opened are silently ignored; this is designed so that you can specify a list of potential configuration file locations (e.g. current directory, user’s home directory, systemwide directory), and all existing configuration files in the list will be read. A single filename may also be given.
Return list of successfully read files.
Set an option.
Write an .ini-format representation of the configuration state.
This expands on RawConfigParser.write() by optionally adding comments when they are known (i.e. for ._default_settings). However, comments are not read in during a read, so .read(x) .write(x) may not preserve comments.
Examples
>>> import sys, StringIO
>>> c = HookeConfigParser()
>>> instring = '''
... # Some comment
... [section]
... option = value
...
... '''
>>> c._read(StringIO.StringIO(instring), 'example.cfg')
>>> c.write(sys.stdout)
[section]
option = value
<BLANKLINE>
An entry (section or option) in HookeConfigParser.
Methods
is_option | |
is_section | |
write |
Methods
assertAlmostEqual | |
assertAlmostEquals | |
assertEqual | |
assertEquals | |
assertFalse | |
assertNotAlmostEqual | |
assertNotAlmostEquals | |
assertNotEqual | |
assertNotEquals | |
assertRaises | |
assertTrue | |
assert_ | |
countTestCases | |
debug | |
defaultTestResult | |
fail | |
failIf | |
failIfAlmostEqual | |
failIfEqual | |
failUnless | |
failUnlessAlmostEqual | |
failUnlessEqual | |
failUnlessRaises | |
failureException | |
id | |
run | |
setUp | |
shortDescription | |
tearDown | |
test_queue_safe |
Ensure HookeConfigParser is Queue-safe.
Return the first Setting object matching both match.section and match.option.