2020-09-30 11:23:25 +05:30
|
|
|
from io import StringIO
|
|
|
|
import abc
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
@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.
|
|
|
|
|
|
|
|
"""
|
2020-09-30 11:23:25 +05:30
|
|
|
try:
|
|
|
|
fhandle = open(fname)
|
|
|
|
except TypeError:
|
|
|
|
fhandle = fname
|
|
|
|
return cls(yaml.safe_load(fhandle))
|
|
|
|
|
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:
|
|
|
|
fhandle = open(fname,'w')
|
|
|
|
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
|
|
|
|
fhandle.write(yaml.dump(dict(self),Dumper=NiceDumper,**kwargs))
|
2020-09-30 11:23:25 +05:30
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_complete(self):
|
|
|
|
"""Check for completeness."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abc.abstractmethod
|
|
|
|
def is_valid(self):
|
|
|
|
"""Check for valid file layout."""
|
|
|
|
pass
|