Skip to content
Snippets Groups Projects
Commit 65ed5329 authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

mkexp: added 'setconfig' filter tool to alter config files via command line

parent a122f125
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,16 @@ Make Experiments!
Release Changes
---------------
Release 1.0.7
=============
Global
------
* Added 'setconfig' filter tool to alter config files via command line.
Supports adding, altering, and deleting of keys in arbitrary sections,
and amendments of header comment
Release 1.0.6
=============
......
......@@ -32,6 +32,21 @@ class MkexpArgumentParser(argparse.ArgumentParser):
self.add_argument('-g', '--getexp', action='store_true',
help='load flat config (from getexp -vv)')
def get_key_chain(key):
sections = []
pos = key.rfind('.')
while(pos >= 0):
if key[pos-1] == '.':
key = key[0:pos-1]+key[pos:]
pos = key.rfind('.', 0, pos-1)
else:
sections.append(key[pos+1:])
key = key[0:pos]
pos = key.rfind('.')
if key:
sections.append(key)
return sections
def assigns_to_dicts(args):
def value_to_list(value):
......@@ -42,17 +57,9 @@ def assigns_to_dicts(args):
def assign_to_dict(assign):
(key, value) = assign.split('=', 1)
chain = get_key_chain(key)
current = value_to_list(value)
pos = key.rfind('.')
while(pos >= 0):
if key[pos-1] == '.':
key = key[0:pos-1]+key[pos:]
pos = key.rfind('.', 0, pos-1)
else:
current = {key[pos+1:]: current}
key = key[0:pos]
pos = key.rfind('.')
if key:
for key in chain:
current = {key: current}
return current
......
......@@ -9,6 +9,7 @@ import argparse
import os
import sys
from expargparse import get_key_chain
from expconfig import ExpConfig, ExpConfigError
from feedback import setLevel, INFO, die, info
......@@ -19,18 +20,7 @@ from feedback import setLevel, INFO, die, info
# Get config value, parsing dotted keys
def get_value(config, key):
sections = []
pos = key.rfind('.')
while(pos >= 0):
if key[pos-1] == '.':
key = key[0:pos-1]+key[pos:]
pos = key.rfind('.', 0, pos-1)
else:
sections.append(key[pos+1:])
key = key[0:pos]
pos = key.rfind('.')
if key:
sections.append(key)
sections = get_key_chain(key)
sections.reverse()
key = sections.pop()
for section in sections:
......
#! /usr/bin/env python
#
# Change configuration using command line
#
# $Id$
#
import argparse
import re
import sys
from configobj import ConfigObj
from expargparse import assigns_to_dicts, get_key_chain
from feedback import die
import package_info
#
# Main routine
#
# Check command line
command_line = argparse.ArgumentParser(description=
'Change configuration using command line.')
command_line.add_argument('config', nargs='?', default='-',
help='original configuration file name [%(default)s]')
command_line.add_argument('assigns', metavar='key=value', nargs='*',
help='override configuration file settings')
command_line.add_argument('-V', '--version', action='version',
version=package_info.version)
command_line.add_argument('--delete', '-d', action='append', default=[],
help='remove given keys from configuration')
command_line.add_argument('--header', '-H', action='append', default=[],
help='append text to header (initial comment)')
args = command_line.parse_args()
data_arg = args.config
if data_arg == '-':
data_arg = sys.stdin
try:
config_data = ConfigObj(data_arg, interpolation=False, file_error=True,
write_empty_values=True)
except IOError as error:
die(error.message)
# Remove keys from --delete command line option
for current in args.delete:
config = config_data
chain = get_key_chain(current)
chain.reverse()
key = chain.pop()
for section in chain:
config = config[section]
del config[key]
# Merge key=value assignments from command line
if args.assigns:
for d in assigns_to_dicts(args):
config_data.merge(d)
# Add lines to header
back_plate = []
for line in reversed(config_data.initial_comment):
if re.match(r'^[\s#]*$', line):
back_plate.insert(0, config_data.initial_comment.pop())
else:
break
for line in args.header:
config_data.initial_comment.append(line)
for line in back_plate:
config_data.initial_comment.append(line)
# Ready to roll out
config_data.write(sys.stdout)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment