Skip to content

Commit

Permalink
Extend the Specification interface to break out loading from streams. (
Browse files Browse the repository at this point in the history
…#198)

* Closes #198 

* Addition of loading specification "from_str".

* Updates to Specification docstrings.

* Updates to abstract Specification to change from str to stream.

* Updates to YAMLSpecification to use the new stream API.

* Removal of IOString

* Update to the YAMLSpecification load stream method.
  • Loading branch information
Francesco Di Natale authored and FrankD412 committed May 28, 2022
1 parent 32f5ff7 commit 281158a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
3 changes: 2 additions & 1 deletion maestrowf/abstracts/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def load_specification(cls, path):
:param path: Path to a study specification.
:returns: A specification object containing the information loaded
from path.
from path.
"""
pass

@abstractclassmethod
def load_specification_from_stream(cls, stream):
Expand Down
32 changes: 22 additions & 10 deletions maestrowf/specification/yamlspecification.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,7 @@ def load_specification(cls, path):
try:
# Load the YAML spec from the file.
with open(path, 'r') as data:
try:
spec = yaml.load(data, yaml.FullLoader)
except AttributeError:
logger.warning(
"*** PyYAML is using an unsafe version with a known "
"load vulnerability. Please upgrade your installation "
"to a more recent version! ***")
spec = yaml.load(data)

specification = cls.load_specification_from_stream(data)
except Exception as e:
logger.exception(e.args)
raise e
Expand Down Expand Up @@ -139,9 +131,29 @@ def load_specification_from_stream(cls, stream):
)
spec = yaml.load(stream)

return specification

@classmethod
def load_specification_from_stream(cls, stream):
"""
Load a study specification.
:param stream: Raw text stream to study YAML specification data.
:returns: A specification object containing the information from the
passed stream.
"""

try:
spec = yaml.load(stream, yaml.FullLoader)
except AttributeError:
logger.warning(
"*** PyYAML is using an unsafe version with a known "
"load vulnerability. Please upgrade your installation "
"to a more recent version! ***")
spec = yaml.load(stream)

logger.debug("Loaded specification -- \n%s", spec["description"])
specification = cls()
specification.path = None
specification.description = spec.pop("description", {})
specification.environment = spec.pop(
"env",
Expand Down

0 comments on commit 281158a

Please sign in to comment.