DAMASK_EICMD/python/damask/_config.py

213 lines
5.7 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
2022-11-23 02:56:15 +05:30
from typing import Optional, Union, Dict, Any, Type, TypeVar
import numpy as np
import yaml
try:
from yaml import CSafeLoader as SafeLoader
from yaml import CSafeDumper as SafeDumper
except ImportError:
from yaml import SafeLoader # type: ignore
from yaml import SafeDumper # type: ignore
2022-02-01 13:00:00 +05:30
from ._typehints import FileHandle
from . import Rotation
from . import util
2022-02-01 13:00:00 +05:30
MyType = TypeVar('MyType', bound='Config')
class NiceDumper(SafeDumper):
"""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):
super().write_line_break(data) # type: ignore
if len(self.indents) == 1: # type: ignore
super().write_line_break() # type: ignore
2022-02-01 13:00:00 +05:30
def increase_indent(self,
flow: bool = False,
indentless: bool = False):
return super().increase_indent(flow, False) # type: ignore
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."""
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)
2022-02-01 13:00:00 +05:30
def ignore_aliases(self,
data: Any) -> bool:
"""Do not use references to existing objects."""
return True
class Config(dict):
"""YAML-based configuration."""
2022-02-01 13:00:00 +05:30
def __init__(self,
2022-11-23 02:56:15 +05:30
yml: Union[None, str, Dict[str, Any]] = None,
2022-02-01 13:00:00 +05:30
**kwargs):
"""Initialize from YAML, dict, or key=value pairs."""
if isinstance(yml,str):
kwargs.update(yaml.load(yml, Loader=SafeLoader))
elif isinstance(yml,dict):
kwargs.update(yml)
super().__init__(**kwargs)
2022-02-01 13:00:00 +05:30
def __repr__(self) -> str:
"""
Return 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
2022-02-01 13:00:00 +05:30
def __copy__(self: MyType) -> MyType:
"""
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:
"""
Return 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.
2022-02-01 13:00:00 +05:30
Note
----
This functionality is a backport for Python 3.8
"""
duplicate = self.copy()
duplicate.update(other)
return duplicate
2022-02-01 13:00:00 +05:30
def __ior__(self: MyType,
other) -> MyType:
"""
Return self|=other.
2022-08-11 00:21:50 +05:30
Update configuration with contents of other (in-place).
"""
return self.__or__(other)
2022-02-01 13:00:00 +05:30
def delete(self: MyType,
keys: Union[Iterable, str]) -> MyType:
"""
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
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
"""
return cls(yaml.load(util.open_text(fname), Loader=SafeLoader))
2022-02-01 13:00:00 +05:30
def save(self,
fname: FileHandle,
**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.
"""
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
fhandle = util.open_text(fname,'w')
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."""
raise NotImplementedError
2021-01-03 16:33:40 +05:30
@property
@abc.abstractmethod
def is_valid(self):
"""Check for valid file layout."""
raise NotImplementedError