allow user do control output style

(numerics.yaml looks strange otherwise)
This commit is contained in:
Martin Diehl 2020-09-30 12:32:37 +02:00
parent 5ad46ae021
commit 33685bc493
3 changed files with 18 additions and 6 deletions

View File

@ -43,7 +43,7 @@ class Config(dict):
fhandle = fname fhandle = fname
return cls(yaml.safe_load(fhandle)) return cls(yaml.safe_load(fhandle))
def save(self,fname): def save(self,fname,**kwargs):
""" """
Save to yaml file. Save to yaml file.
@ -51,13 +51,20 @@ class Config(dict):
---------- ----------
fname : file, str, or pathlib.Path fname : file, str, or pathlib.Path
Filename or file for writing. Filename or file for writing.
**kwargs : dict
Keyword arguments parsed to yaml.dump.
""" """
try: try:
fhandle = open(fname,'w') fhandle = open(fname,'w')
except TypeError: except TypeError:
fhandle = fname fhandle = fname
fhandle.write(yaml.dump(dict(self),width=256,default_flow_style=None,Dumper=NiceDumper))
if 'width' not in kwargs:
kwargs['width'] = 256
if 'default_flow_style' not in kwargs:
kwargs['default_flow_style'] = None
fhandle.write(yaml.dump(dict(self),Dumper=NiceDumper,**kwargs))
@property @property

View File

@ -9,7 +9,7 @@ from . import Rotation
class ConfigMaterial(Config): class ConfigMaterial(Config):
"""Material configuration.""" """Material configuration."""
def save(self,fname='material.yaml'): def save(self,fname='material.yaml',**kwargs):
""" """
Save to yaml file. Save to yaml file.
@ -17,9 +17,11 @@ class ConfigMaterial(Config):
---------- ----------
fname : file, str, or pathlib.Path, optional fname : file, str, or pathlib.Path, optional
Filename or file for writing. Defaults to 'material.yaml'. Filename or file for writing. Defaults to 'material.yaml'.
**kwargs : dict
Keyword arguments parsed to yaml.dump.
""" """
super().save(fname) super().save(fname,**kwargs)
@property @property
def is_complete(self): def is_complete(self):

View File

@ -1,12 +1,15 @@
import pytest
from damask import Config from damask import Config
class TestConfig: class TestConfig:
def test_load_save_str(self,tmp_path): @pytest.mark.parametrize('flow_style',[None,True,False])
def test_load_save_str(self,tmp_path,flow_style):
config = Config() config = Config()
config['A'] = 1 config['A'] = 1
config['B'] = [2,3] config['B'] = [2,3]
config.save(tmp_path/'config.yaml') config.save(tmp_path/'config.yaml',default_flow_style=flow_style)
assert Config.load(tmp_path/'config.yaml') == config assert Config.load(tmp_path/'config.yaml') == config
def test_load_save_file(self,tmp_path): def test_load_save_file(self,tmp_path):