Module ConfigPersist
[hide private]
[frames] | no frames]

Module ConfigPersist
source code

Functions for doing data persistence with ConfigObj.

It requires access to the validate module and ConfigObj.



Functions [hide private]
  add_configspec(config)
A function that adds a configspec to a ConfigObj.
  write_configspec(config)
Return the configspec (of a ConfigObj) as a list of lines.
  add_typeinfo(config)
Turns the configspec attribute of each section into a member of the section.
  typeinfo_to_configspec(config)
Turns the '__types__' member of each section into a configspec.
  store(config)
" Passed a ConfigObj instance add type info and save.
  restore(stored)
Restore a ConfigObj saved using the store function.
  save_configspec(config)
Creates a configspec and returns it as a list of lines.
  _test()
A dummy function for the sake of doctest.

Variables [hide private]
__version__  
vtor  

Imports: configobj.ConfigObj, validate.Validator, doctest

Function Details [hide private]

add_configspec(config)

source code 

A function that adds a configspec to a ConfigObj.

Will only work for ConfigObj instances using basic datatypes :

  • floats
  • strings
  • ints
  • booleans
  • Lists of the above

write_configspec(config)

source code 
Return the configspec (of a ConfigObj) as a list of lines.

add_typeinfo(config)

source code 

Turns the configspec attribute of each section into a member of the section. (Called __types__).

You must have already called add_configspec on the ConfigObj.

typeinfo_to_configspec(config)

source code 
Turns the '__types__' member of each section into a configspec.

store(config)

source code 

" Passed a ConfigObj instance add type info and save.

Returns the result of calling config.write().

restore(stored)

source code 

Restore a ConfigObj saved using the store function.

Takes a filename or list of lines, returns the ConfigObj instance.

Uses the built-in Validator instance of this module (vtor).

Raises an ImportError if the validate module isn't available

save_configspec(config)

source code 
Creates a configspec and returns it as a list of lines.

_test()

source code 

A dummy function for the sake of doctest.

First test add_configspec >>> from configobj import ConfigObj >>> from validate import Validator >>> vtor = Validator() >>> config = ConfigObj() >>> config['member 1'] = 3 >>> config['member 2'] = 3.0 >>> config['member 3'] = True >>> config['member 4'] = [3, 3.0, True] >>> add_configspec(config) >>> assert config.configspec == { 'member 2': 'float', ... 'member 3': 'boolean', 'member 1': 'integer', ... 'member 4': "mixed_list('integer', 'float', 'boolean')"} >>> assert config.validate(vtor) == True

Next test write_configspec - including a nested section >>> config['section 1'] = config.copy() >>> add_configspec(config) >>> a = config.write() >>> configspec = write_configspec(config) >>> b = ConfigObj(a, configspec=configspec) >>> assert b.validate(vtor) == True >>> assert b == config

Next test add_typeinfo and typeinfo_to_configspec >>> orig = ConfigObj(config) >>> add_typeinfo(config) >>> a = ConfigObj(config.write()) >>> typeinfo_to_configspec(a) >>> assert a.validate(vtor) == True >>> assert a == orig >>> typeinfo_to_configspec(config) >>> assert config.validate(vtor) == True >>> assert config == orig

Test store and restore >>> a = store(config) >>> b = restore(a) >>> assert b == orig

Test save_configspec >>> a = save_configspec(orig) >>> b = ConfigObj(b, configspec=a) >>> b.validate(vtor) 1


Variables Details [hide private]

__version__

Value:
'0.1.0'                                                                
      

vtor

Value:
<validate.Validator object at 0x00D6EF90>