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
|
2023-02-01 18:44:54 +05:30
|
|
|
import platform
|
2022-11-23 02:56:15 +05:30
|
|
|
from typing import Optional, Union, Dict, Any, Type, TypeVar
|
2020-09-30 11:23:25 +05:30
|
|
|
|
2020-10-27 02:08:24 +05:30
|
|
|
import numpy as np
|
2020-09-30 11:23:25 +05:30
|
|
|
import yaml
|
2022-04-30 23:14:50 +05:30
|
|
|
try:
|
|
|
|
from yaml import CSafeLoader as SafeLoader
|
2022-12-07 01:47:23 +05:30
|
|
|
from yaml import CSafeDumper as SafeDumper
|
2022-04-30 23:14:50 +05:30
|
|
|
except ImportError:
|
2022-05-01 02:45:21 +05:30
|
|
|
from yaml import SafeLoader # type: ignore
|
2022-12-07 01:47:23 +05:30
|
|
|
from yaml import SafeDumper # type: ignore
|
2020-09-30 11:23:25 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
from ._typehints import FileHandle
|
2021-03-25 20:00:31 +05:30
|
|
|
from . import Rotation
|
2022-03-27 12:33:51 +05:30
|
|
|
from . import util
|
2021-03-25 20:00:31 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
MyType = TypeVar('MyType', bound='Config')
|
|
|
|
|
2022-12-07 01:47:23 +05:30
|
|
|
class NiceDumper(SafeDumper):
|
2020-09-30 11:23:25 +05:30
|
|
|
"""Make YAML readable for humans."""
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def write_line_break(self,
|
2022-11-23 02:56:15 +05:30
|
|
|
data: Optional[str] = None):
|
2022-12-07 01:47:23 +05:30
|
|
|
super().write_line_break(data) # type: ignore
|
2020-09-30 11:23:25 +05:30
|
|
|
|
2022-12-07 01:47:23 +05:30
|
|
|
if len(self.indents) == 1: # type: ignore
|
|
|
|
super().write_line_break() # type: ignore
|
2020-09-30 11:23:25 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def increase_indent(self,
|
|
|
|
flow: bool = False,
|
|
|
|
indentless: bool = False):
|
2022-12-07 01:47:23 +05:30
|
|
|
return super().increase_indent(flow, False) # type: ignore
|
2020-09-30 11:23:25 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def represent_data(self,
|
|
|
|
data: Any):
|
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())
|
2023-01-24 12:45:03 +05:30
|
|
|
if isinstance(data, np.generic):
|
2022-12-06 00:59:08 +05:30
|
|
|
return self.represent_data(data.item())
|
|
|
|
|
|
|
|
return super().represent_data(data)
|
2020-10-27 02:08:24 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def ignore_aliases(self,
|
|
|
|
data: Any) -> bool:
|
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."""
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def __init__(self,
|
2023-01-24 12:45:03 +05:30
|
|
|
config: Optional[Union[str, Dict[str, Any]]] = None,
|
2022-02-01 13:00:00 +05:30
|
|
|
**kwargs):
|
2023-01-24 12:45:03 +05:30
|
|
|
"""
|
|
|
|
New YAML-based configuration.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
config : dict or str, optional
|
|
|
|
Configuration. String needs to be valid YAML.
|
|
|
|
**kwargs: arbitray keyword-value pairs, optional
|
|
|
|
Top level entries of the configuration.
|
|
|
|
|
2023-02-01 18:44:54 +05:30
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
Values given as keyword-value pairs take precedence
|
|
|
|
over entries with the same keyword in 'config'.
|
|
|
|
|
2023-01-24 12:45:03 +05:30
|
|
|
"""
|
2023-02-01 18:44:54 +05:30
|
|
|
if int(platform.python_version_tuple()[1]) >= 9:
|
|
|
|
if isinstance(config,str):
|
|
|
|
kwargs = yaml.load(config, Loader=SafeLoader) | kwargs
|
|
|
|
elif isinstance(config,dict):
|
|
|
|
kwargs = config | kwargs # type: ignore
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
else:
|
|
|
|
if isinstance(config,str):
|
|
|
|
c = yaml.load(config, Loader=SafeLoader)
|
|
|
|
elif isinstance(config,dict):
|
|
|
|
c = config.copy()
|
|
|
|
else:
|
|
|
|
c = {}
|
|
|
|
c.update(kwargs)
|
|
|
|
|
|
|
|
super().__init__(**c)
|
2021-12-03 04:50:27 +05:30
|
|
|
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def __repr__(self) -> str:
|
2022-07-08 21:36:41 +05:30
|
|
|
"""
|
|
|
|
Return repr(self).
|
|
|
|
|
|
|
|
Show as in file.
|
|
|
|
|
|
|
|
"""
|
2020-09-30 11:23:25 +05:30
|
|
|
output = StringIO()
|
|
|
|
self.save(output)
|
|
|
|
output.seek(0)
|
|
|
|
return ''.join(output.readlines())
|
|
|
|
|
2021-01-03 16:33:40 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def __copy__(self: MyType) -> MyType:
|
2022-07-08 21:36:41 +05:30
|
|
|
"""
|
|
|
|
Return deepcopy(self).
|
|
|
|
|
|
|
|
Create deep copy.
|
|
|
|
|
|
|
|
"""
|
2021-01-03 16:33:40 +05:30
|
|
|
return copy.deepcopy(self)
|
|
|
|
|
|
|
|
copy = __copy__
|
|
|
|
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def __or__(self: MyType,
|
|
|
|
other) -> MyType:
|
2021-03-10 00:45:15 +05:30
|
|
|
"""
|
2022-07-08 21:36:41 +05:30
|
|
|
Return self|other.
|
|
|
|
|
2021-03-10 00:45:15 +05:30
|
|
|
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.
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
Note
|
|
|
|
----
|
|
|
|
This functionality is a backport for Python 3.8
|
|
|
|
|
2021-03-10 00:45:15 +05:30
|
|
|
"""
|
|
|
|
duplicate = self.copy()
|
|
|
|
duplicate.update(other)
|
|
|
|
return duplicate
|
|
|
|
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def __ior__(self: MyType,
|
|
|
|
other) -> MyType:
|
2022-07-08 21:36:41 +05:30
|
|
|
"""
|
|
|
|
Return self|=other.
|
|
|
|
|
2022-08-11 00:21:50 +05:30
|
|
|
Update configuration with contents of other (in-place).
|
2022-07-08 21:36:41 +05:30
|
|
|
|
|
|
|
"""
|
2021-03-10 00:45:15 +05:30
|
|
|
return self.__or__(other)
|
|
|
|
|
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def delete(self: MyType,
|
|
|
|
keys: Union[Iterable, str]) -> MyType:
|
2021-03-10 00:45:15 +05:30
|
|
|
"""
|
|
|
|
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
|
2022-02-01 13:00:00 +05:30
|
|
|
def load(cls: Type[MyType],
|
|
|
|
fname: FileHandle) -> MyType:
|
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
|
|
|
"""
|
2022-04-30 23:14:50 +05:30
|
|
|
return cls(yaml.load(util.open_text(fname), Loader=SafeLoader))
|
2020-09-30 11:23:25 +05:30
|
|
|
|
2022-02-01 13:00:00 +05:30
|
|
|
def save(self,
|
|
|
|
fname: FileHandle,
|
|
|
|
**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
|
|
|
|
|
|
|
"""
|
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
|
|
|
|
2022-03-27 12:33:51 +05:30
|
|
|
fhandle = util.open_text(fname,'w')
|
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
|