Wiki

Clone wiki

confpatcher / Home

Dependencies

To use these tools, you will need :

  • PyYAML : a parser/emitter for the YAML format
  • mako : a template engine

Basic use

Supposing you have a default configuration file called myApp.conf containing the following lines:

# myApp v1.2 configuration file
# this version sets 'hello' as a default value for param1
# it also adds param12
param1=hello
param2=hi
param3=goodbye
param12=0

Make a patch file you call myApp.conf.yaml :

param2: goodbye
param3: goodbye
param4: bye-bye

or if you prefer in cfg format:

param2=goodbye
param3=goodbye
param4=bye-bye

Then you may call :

from confpatcher import confpatcher

confpatcher.patch_file('myApp.conf', confpatcher.config_from_file('myApp.conf.yaml'))

note that config_from_file() gets a dictionary from a yaml file or a cfg file.

myApp.conf will then look like this :

# myApp v1.2 configuration file
# this version sets 'hello' as a default value for param1
# it also adds param12
param1=hello
param2=goodbye
param3=goodbye

# added configuration options
param4=bye-bye

Automatic patch generation

Once you have a set of patch files, they may share common values (eg. a host name, ip address etc.). So it may be wise to make templates out of them with placeholders for these values. Then you can render the templates, feeding them with appropriate values. To render these templates, you can use a BatchTemplate object. Templates are written in mako format.

First, add your templates paths:

import confpatcher.batch_template

b = confpatcher.batch_template.BatchTemplate()
b.add_template_file("myApp.yaml.mako")

Then, add files that contain your input data (in yaml format) :

b.add_data_file("myApp.yaml.mako")

...and set the output path where the rendered templates will be written:

b.set_output_path("./output")

Finally, you can render the templates :

b.render()

Updated