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

Configuration: simplified commented namelist settings at the end of a group by...

Configuration: simplified commented namelist settings at the end of a group by adding a special variable '.end'
parent 5a110159
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,8 @@ Configuration
* Added new way to disable namelist groups by setting the special variable
'.hide' to 'true'. This allows to selectively re-enable groups in higher
order config files by setting '.hide' to 'false'
* Simplified commented namelist settings at the end of a group by adding a
special variable '.end'
Templates
---------
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
No preview for this file type
......@@ -149,6 +149,17 @@ def get_remove_list(section, key):
remove_list = [key] + section[key]
return remove_list
def format_namelist_comment(line):
match = re.match(r'^#\s*(\w+)\s*=\s*(.*?)\s*(#\s*(.*))?$', line)
if match:
key = match.group(1)
value = match.group(2)
comment = ''
if match.group(4):
comment = ' ! '+match.group(4)
return '! '+key+' = '+format_value(value, 0)+comment+'\n'
return re.sub(r'^#', '!', line)+'\n'
def format_namelist(section, group=None):
'''Format config section as a namelist.
If given, just format a single namelist group'''
......@@ -158,6 +169,7 @@ def format_namelist(section, group=None):
return ''
# Settings
base_indent = 4
end_key = '.end'
# Create list of removed namelist groups.
# Support old keyword for backward compatibility
remove_list = get_remove_list(section, '.remove')
......@@ -174,6 +186,9 @@ def format_namelist(section, group=None):
if not hidden and not any(map(lambda x: re.match(x, group_id), black_list)):
# Create list of removed keys
remove_keys = get_remove_list(contents, '.remove')
# Start namelist group
for line in section.comments.get(group, []):
lines.write(format_namelist_comment(line))
group_names = group_id.split(' ', 1)
if len(group_names) == 1:
lines.write('&'+group_names[0]+'\n')
......@@ -184,9 +199,8 @@ def format_namelist(section, group=None):
key = key.lower()
indent = base_indent + len(key) + 3
for line in contents.comments.get(key, []):
if line:
lines.write(' '*base_indent+
re.sub(r'^#', '!', line)+'\n')
lines.write(' '*base_indent)
lines.write(format_namelist_comment(line))
line = contents.inline_comments[key]
if not line:
line = ''
......@@ -194,16 +208,10 @@ def format_namelist(section, group=None):
lines.write(' '*base_indent+key+' = '+
format_value(value, indent)+
line+'\n')
for line in section.comments.get(group, []):
if line:
match = re.match(r'#\s*(\w+)\s*=\s*(.*?)\s*$', line)
if match:
key = match.group(1)
value = match.group(2)
indent = base_indent + 2 + len(key) + 3
lines.write(' '*base_indent+'! '+key+' = '+
format_value(value, indent)+
'\n')
if end_key in contents:
for line in contents.comments[end_key]:
lines.write(' '*base_indent)
lines.write(format_namelist_comment(line))
lines.write('/\n')
return lines.getvalue()
......
......@@ -416,24 +416,29 @@ class NamelistTestCase(MkexpSimpleTestCase):
self.run_test("""
%{NAMELIST}
""","""
! Comment group 1
! var_1c = 'test'
&group_1
! Comment for var 1a
var_1a = 42 ! Inline comment for var 1a
! var_1b = 84
! var_1b = .true.
/
! var_1d = 10.5
&group_2
! Comment for var 2b
var_2b = 21 ! Inline comment for var 2b
! var_2a = 10.5
/
""","""
[namelists]
[[namelist]]
# var_1b = 84
# Comment group 1
# var_1c = test
[[[group_1]]]
# Comment for var 1a
var_1a = 42 # Inline comment for var 1a
# var_2a = 10.5
# var_1b = true
.end =
# var_1d = 10.5
[[[group_2]]]
# Comment for var 2b
var_2b = 21 # Inline comment for var 2b
......
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