DAMASK_EICMD/python/damask/_config.py

174 lines
4.3 KiB
Python
Raw Normal View History

2021-01-03 16:33:40 +05:30
import copy
from io import StringIO
from collections.abc import Iterable
import abc
import numpy as np
import yaml
from . import Rotation
class NiceDumper(yaml.SafeDumper):
"""Make YAML readable for humans."""
def write_line_break(self, data=None):
super().write_line_break(data)
if len(self.indents) == 1:
super().write_line_break()
def increase_indent(self, flow=False, indentless=False):
return super().increase_indent(flow, False)
def represent_data(self, data):
2020-10-27 02:13:21 +05:30
"""Cast Config objects and its subclasses to dict."""
if isinstance(data, dict) and type(data) != dict:
return self.represent_data(dict(data))
if isinstance(data, np.ndarray):
return self.represent_data(data.tolist())
if isinstance(data, Rotation):
return self.represent_data(data.quaternion.tolist())
else:
return super().represent_data(data)
def ignore_aliases(self, data):
"""Do not use references to existing objects."""
return True
class Config(dict):
"""YAML-based configuration."""
def __repr__(self):
"""Show as in file."""
output = StringIO()
self.save(output)
output.seek(0)
return ''.join(output.readlines())
2021-01-03 16:33:40 +05:30
def __copy__(self):
"""Create deep copy."""
return copy.deepcopy(self)
copy = __copy__
def __or__(self,other):
"""
Update configuration with contents of other.
Parameters
----------
other : damask.Config or dict
Key-value pairs that update self.
2021-04-25 11:17:00 +05:30
Returns
-------
updated : damask.Config
Updated configuration.
"""
duplicate = self.copy()
duplicate.update(other)
return duplicate
def __ior__(self,other):
"""Update configuration with contents of other."""
return self.__or__(other)
def delete(self,keys):
"""
Remove configuration keys.
Parameters
----------
keys : iterable or scalar
Label of the key(s) to remove.
2021-04-25 11:17:00 +05:30
Returns
-------
updated : damask.Config
Updated configuration.
"""
duplicate = self.copy()
for k in keys if isinstance(keys, Iterable) and not isinstance(keys, str) else [keys]:
del duplicate[k]
return duplicate
@classmethod
def load(cls,fname):
2020-09-30 12:19:55 +05:30
"""
Load from yaml file.
Parameters
----------
fname : file, str, or pathlib.Path
Filename or file for writing.
2021-04-25 11:17:00 +05:30
Returns
-------
loaded : damask.Config
Configuration from file.
2020-09-30 12:19:55 +05:30
"""
try:
fhandle = open(fname)
except TypeError:
fhandle = fname
return cls(yaml.safe_load(fhandle))
2021-01-03 16:33:40 +05:30
def save(self,fname,**kwargs):
"""
Save to yaml file.
Parameters
----------
fname : file, str, or pathlib.Path
2020-09-30 12:19:55 +05:30
Filename or file for writing.
**kwargs : dict
Keyword arguments parsed to yaml.dump.
"""
try:
fhandle = open(fname,'w',newline='\n')
except TypeError:
fhandle = fname
if 'width' not in kwargs:
kwargs['width'] = 256
if 'default_flow_style' not in kwargs:
kwargs['default_flow_style'] = None
2020-10-27 02:13:21 +05:30
if 'sort_keys' not in kwargs:
kwargs['sort_keys'] = False
def array_representer(dumper, data):
"""Convert numpy array to list of native types."""
return dumper.represent_list([d.item() for d in data])
NiceDumper.add_representer(np.ndarray, array_representer)
try:
fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs))
except TypeError: # compatibility with old pyyaml
del kwargs['sort_keys']
fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs))
@property
@abc.abstractmethod
def is_complete(self):
"""Check for completeness."""
pass
2021-01-03 16:33:40 +05:30
@property
@abc.abstractmethod
def is_valid(self):
"""Check for valid file layout."""
pass