2021-01-03 16:33:40 +05:30
|
|
|
import copy
|
2020-09-30 11:23:25 +05:30
|
|
|
from io import StringIO
|
2021-03-10 00:45:15 +05:30
|
|
|
from collections.abc import Iterable
|
2020-09-30 11:23:25 +05:30
|
|
|
import abc
|
|
|
|
|
2020-10-27 02:08:24 +05:30
|
|
|
import numpy as np
|
2020-09-30 11:23:25 +05:30
|
|
|
import yaml
|
|
|
|
|
2021-03-25 20:00:31 +05:30
|
|
|
from . import Rotation
|
|
|
|
|
2020-09-30 11:23:25 +05:30
|
|
|
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)
|
|
|
|
|
2020-10-27 02:08:24 +05:30
|
|
|
def represent_data(self, data):
|
2020-10-27 02:13:21 +05:30
|
|
|
"""Cast Config objects and its subclasses to dict."""
|
2021-03-25 20:00:31 +05:30
|
|
|
if isinstance(data, dict) and type(data) != dict:
|
|
|
|
return self.represent_data(dict(data))
|
2021-05-26 02:01:54 +05:30
|
|
|
if isinstance(data, np.ndarray):
|
|
|
|
return self.represent_data(data.tolist())
|
|
|
|
if isinstance(data, Rotation):
|
|
|
|
return self.represent_data(data.quaternion.tolist())
|
2021-03-25 20:00:31 +05:30
|
|
|
else:
|
|
|
|
return super().represent_data(data)
|
2020-10-27 02:08:24 +05:30
|
|
|
|
2020-12-18 19:49:04 +05:30
|
|
|
def ignore_aliases(self, data):
|
2021-03-27 14:40:35 +05:30
|
|
|
"""Do not use references to existing objects."""
|
2020-12-18 19:49:04 +05:30
|
|
|
return True
|
2020-09-30 11:23:25 +05:30
|
|
|
|
|
|
|
class Config(dict):
|
|
|
|
"""YAML-based configuration."""
|
|
|
|
|
2021-12-03 04:50:27 +05:30
|
|
|
def __init__(self,yml=None,**kwargs):
|
|
|
|
"""Initialize from YAML, dict, or key=value pairs."""
|
|
|
|
if isinstance(yml,str):
|
|
|
|
kwargs.update(yaml.safe_load(yml))
|
|
|
|
elif isinstance(yml,dict):
|
|
|
|
kwargs.update(yml)
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
2020-09-30 11:23:25 +05:30
|
|
|
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__
|
|
|
|
|
|
|
|
|
2021-03-10 00:45:15 +05:30
|
|
|
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.
|
|
|
|
|
2021-03-10 00:45:15 +05:30
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
2021-03-10 00:45:15 +05:30
|
|
|
"""
|
|
|
|
duplicate = self.copy()
|
|
|
|
for k in keys if isinstance(keys, Iterable) and not isinstance(keys, str) else [keys]:
|
|
|
|
del duplicate[k]
|
|
|
|
return duplicate
|
|
|
|
|
|
|
|
|
2020-09-30 11:23:25 +05:30
|
|
|
@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
|
|
|
"""
|
2020-09-30 11:23:25 +05:30
|
|
|
try:
|
|
|
|
fhandle = open(fname)
|
|
|
|
except TypeError:
|
|
|
|
fhandle = fname
|
|
|
|
return cls(yaml.safe_load(fhandle))
|
|
|
|
|
2021-01-03 16:33:40 +05:30
|
|
|
|
2020-09-30 16:02:37 +05:30
|
|
|
def save(self,fname,**kwargs):
|
2020-09-30 11:23:25 +05:30
|
|
|
"""
|
|
|
|
Save to yaml file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
fname : file, str, or pathlib.Path
|
2020-09-30 12:19:55 +05:30
|
|
|
Filename or file for writing.
|
2020-09-30 16:02:37 +05:30
|
|
|
**kwargs : dict
|
|
|
|
Keyword arguments parsed to yaml.dump.
|
2020-09-30 11:23:25 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
2021-02-10 14:33:35 +05:30
|
|
|
fhandle = open(fname,'w',newline='\n')
|
2020-09-30 11:23:25 +05:30
|
|
|
except TypeError:
|
|
|
|
fhandle = fname
|
2020-09-30 16:02:37 +05:30
|
|
|
|
|
|
|
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
|
2020-10-27 02:08:24 +05:30
|
|
|
|
2020-10-27 11:04:05 +05:30
|
|
|
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))
|
2020-09-30 11:23:25 +05:30
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_complete(self):
|
|
|
|
"""Check for completeness."""
|
2021-05-29 14:24:34 +05:30
|
|
|
raise NotImplementedError
|
2020-09-30 11:23:25 +05:30
|
|
|
|
2021-01-03 16:33:40 +05:30
|
|
|
|
2020-09-30 11:23:25 +05:30
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_valid(self):
|
|
|
|
"""Check for valid file layout."""
|
2021-05-29 14:24:34 +05:30
|
|
|
raise NotImplementedError
|