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

Globals: added tool to restore global vars from a base config (unmergeconfig)

parent e24dac62
No related branches found
No related tags found
No related merge requests found
......@@ -19,8 +19,8 @@ Global
* namelist2config: re-enable here-doc detection to remove spurious data,
use global variables in namelists and output, remove array slice syntax
* Fixes related to python3, perl-5.26, and jinja2-3.0 compatibility
* Added tools to find differences/similarities between config files
(compconfig.py, diffconfig.py)
* Added tools to manage differences/similarities between config files
(compconfig.py, diffconfig.py, unmergeconfig)
Release 1.1.2
=============
......
#! /usr/bin/env python
'''\
Create top level variables based on a higher level config
$Id$
'''
from __future__ import print_function
import sys
import io
import argparse
import package_info
import re
from feedback import die, warn
from configobj import Section
from expconfig import ConfigObj
#
# Helper routines
#
# Get base config keys that contain globals
global_matcher = re.compile(r'\$(\w+)')
def get_globals(config, result):
for key in config.sections:
get_globals_section(config[key], key, config.scalars, result)
def get_globals_section(section, path, global_keys, result):
for key in section.scalars:
value = section[key]
if not isinstance(value, (list, tuple)):
match = global_matcher.match(value)
if match:
global_key = match.group(1)
if True: ### global_key in global_keys:
result[path+'/'+key] = global_key
for key in section.sections:
get_globals_section(section[key], path+'/'+key, global_keys, result)
def eval_globals(config, global_dict):
for key in config.sections:
eval_globals_section(config[key], key, global_dict)
def eval_globals_section(section, path, global_dict):
for key in section.scalars:
value = section[key]
if not isinstance(value, (list, tuple)):
global_key = path+'/'+key
if global_key in global_dict:
global_var = global_dict[global_key]
if global_var in section.main:
if section.main[global_var] != value:
warn("global '%s' already set to '%s', "
"keeping '%s' at '%s'", global_var,
section.main[global_var], global_key, value)
else:
section[key] = '$' + global_var
else:
section.main[global_var] = value
section[key] = '$' + global_var
for key in section.sections:
eval_globals_section(section[key], path+'/'+key, global_dict)
#
# Main routine
#
# Check command line
command_line = argparse.ArgumentParser(description=__doc__.split('\n', 1)[0])
# TODO: print differences option, ...
command_line.add_argument('--indent-string', default=' ', help='set indent string [%(default)s]')
command_line.add_argument('--inline-comments' , '-c', action='store_true',
help='compact white space before inline comments'
' (BETA)')
command_line.add_argument('--trailing-space' , '-t', action='store_true',
help='remove white space at end of lines')
command_line.add_argument('base_config', help='name of high level config')
command_line.add_argument('config', help='name of low level/expanded config')
command_line.add_argument('-V', '--version', action='version',
version=package_info.version)
args = command_line.parse_args()
# File handling
try:
base_config = ConfigObj(args.base_config, interpolation=False,
file_error=True)
config = ConfigObj(args.config, interpolation=False, file_error=True)
except IOError as error:
die(error.args[0])
global_dict = {}
get_globals(base_config, global_dict)
### for k, v in global_dict.items():
### print(k+':', v)
### exit()
eval_globals(config, global_dict)
### exit()
# Ready to roll out
lines = io.BytesIO()
config.write(lines)
lines.seek(0)
for line in io.TextIOWrapper(lines):
if args.inline_comments: line = re.sub(r' = (.*?) #', r' = \1 #', line)
if args.trailing_space:
print(line.rstrip())
else:
print(line, end='')
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