Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_comment_patch(self):
nml = {'comment_nml': {'v_cmt_inline': 456}}
try:
f90nml.patch('comment.nml', nml, 'tmp.nml')
self.assert_file_equal('comment_patch.nml', 'tmp.nml')
finally:
os.remove('tmp.nml')
def test_repatch(self):
f90nml.patch('repatch.nml', self.repatch_nml, 'tmp.nml')
test_nml = f90nml.read('tmp.nml')
try:
self.assertEqual(test_nml, self.repatch_nml)
finally:
os.remove('tmp.nml')
def test_patch_paths(self):
patch_nml = f90nml.read('types_patch.nml')
f90nml.patch('types.nml', patch_nml, 'tmp.nml')
test_nml = f90nml.read('tmp.nml')
try:
self.assertEqual(test_nml, patch_nml)
finally:
os.remove('tmp.nml')
def test_patch_files(self):
patch_nml = f90nml.read('types_patch.nml')
with open('types.nml') as f_in:
with open('tmp.nml', 'w') as f_out:
f90nml.patch(f_in, patch_nml, f_out)
self.assertFalse(f_in.closed)
self.assertFalse(f_out.closed)
try:
test_nml = f90nml.read('tmp.nml')
self.assertEqual(test_nml, patch_nml)
finally:
os.remove('tmp.nml')
def test_default_patch(self):
patch_nml = f90nml.read('types_patch.nml')
f90nml.patch('types.nml', patch_nml)
test_nml = f90nml.read('types.nml~')
try:
self.assertEqual(test_nml, patch_nml)
finally:
os.remove('types.nml~')
# The above behavior is only for paths, not files
with open('types.nml') as nml_file:
self.assertRaises(ValueError, f90nml.patch, nml_file, patch_nml)
def test_patch_null(self):
try:
f90nml.patch('types.nml', {}, 'tmp.nml')
self.assert_file_equal('types.nml', 'tmp.nml')
finally:
os.remove('tmp.nml')
grp = args.group
update_nml_str = '&{0} {1} /\n'.format(grp, ', '.join(args.variable))
update_io = StringIO(update_nml_str)
update_nml = f90nml.read(update_io)
update_io.close()
# Target output
output_file = open(output_fname, 'w') if output_fname else sys.stdout
if args.patch:
# We have to read the file twice for a patch. The main reason is
# to identify the default group, in case this is not provided.
# It could be avoided if a group is provided, but logically that could
# a mess that I do not want to sort out right now.
f90nml.patch(input_fname, update_nml, output_file)
else:
# Update the input namelist directly
if update_nml:
try:
input_data[grp].update(update_nml[grp])
except KeyError:
input_data[grp] = update_nml[grp]
# Write to output
if not args.patch:
if output_fmt in ('json', 'yaml'):
if output_fmt == 'json':
input_data = input_data.todict(complex_tuple=True)
json.dump(input_data, output_file,
indent=4, separators=(',', ': '))
def patch_namelist(orig_nml_path: str,patch_nml_path: str,new_nml_path: str):
"""This function updates a larger orginal namelist with a file containing a smaller subset of
changes and writes out a new namelist to a file.
Args:
orig_nml_path: Path to the namelist file to be updated
patch_nml_path: Path to the file containing the namelist updates
new_nml_path: Path to write the new namelist file with updates applied.
Returns:
None
"""
# Read in namelist patch
patch_nml = f90nml.read(nml_path=patch_nml_path)
# Write new namelist to file
f90nml.patch(nml_path=orig_nml_path,
nml_patch=patch_nml,
out_path=new_nml_path)
#print('New namelist written to ' + new_nml_path)
return(None)