def __init__(self, data=None, converts_none_to_str=True):
"""Could be a JSON or a YAML file
:param str filename: filename to a config file in json or YAML format.
SEQUANA config files must have some specific fields::
input_directory
input_samples...
"""
# Create a dummy YAML code to hold data in case the input is a json
# or a dictionary structure. We use a CommentedMap that works like
# a dictionary. Be aware that the update method will lose the comments
if data is None:
self.config = AttrDict()
self._yaml_code = comments.CommentedMap()
elif isinstance(data, str): # else is it a filename ?
if os.path.exists(data):
if data.endswith(".yaml") or data.endswith(".yml"):
with open(data, "r") as fh:
self._yaml_code = ruamel.yaml.load(
fh.read(), ruamel.yaml.RoundTripLoader)
else:
# read a JSON
import yaml
with open(data, "r") as fh:
self._yaml_code = yaml.load(json.dumps(
json.loads(fh.read())))
config = load_configfile(data)
else:
raise IOError("input string must be an existing file (%s)" % data)
self.config = AttrDict(**config)
elif isinstance(data, SequanaConfig): # else maybe a SequanaConfig ?
self.config = AttrDict(**data.config)
self._yaml_code = comments.CommentedMap(self.config.copy())
else: # or a pure dictionary ?
self.config = AttrDict(**data)
self._yaml_code = comments.CommentedMap(self.config.copy())
self.cleanup_config()
评论列表
文章目录