diff --git a/src/cffconvert/cff_1_0_x/citation.py b/src/cffconvert/cff_1_0_x/citation.py index 2c3b21a9..ee6f231f 100644 --- a/src/cffconvert/cff_1_0_x/citation.py +++ b/src/cffconvert/cff_1_0_x/citation.py @@ -1,5 +1,6 @@ import os from pykwalify.core import Core +from ruamel.yaml import SafeConstructor from ruamel.yaml import YAML from cffconvert.cff_1_0_x.apalike import ApalikeObject from cffconvert.cff_1_0_x.bibtex import BibtexObject @@ -32,9 +33,24 @@ def _get_schema(self): return YAML(typ="safe").load(fid.read()) def _parse(self): - cffobj = YAML(typ="safe").load(self.cffstr) + # instantiate the YAML module: + yaml = YAML(typ="safe") + + # store the current value of the timestamp parser + tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp") + + # Configure YAML to load timestamps as timestamps: + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_timestamp + + try: + cffobj = yaml.load(self.cffstr) + finally: + # restore the old value + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = tmp + if not isinstance(cffobj, dict): raise ValueError("Provided CITATION.cff does not seem valid YAML.") + return cffobj def as_apalike(self): diff --git a/src/cffconvert/cff_1_1_x/citation.py b/src/cffconvert/cff_1_1_x/citation.py index 80aa209c..d00b2e0f 100644 --- a/src/cffconvert/cff_1_1_x/citation.py +++ b/src/cffconvert/cff_1_1_x/citation.py @@ -1,5 +1,6 @@ import os from pykwalify.core import Core +from ruamel.yaml import SafeConstructor from ruamel.yaml import YAML from cffconvert.cff_1_1_x.apalike import ApalikeObject from cffconvert.cff_1_1_x.bibtex import BibtexObject @@ -30,9 +31,24 @@ def _get_schema(self): return YAML(typ="safe").load(fid.read()) def _parse(self): - cffobj = YAML(typ="safe").load(self.cffstr) + # instantiate the YAML module: + yaml = YAML(typ="safe") + + # store the current value of the timestamp parser + tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp") + + # Configure YAML to load timestamps as timestamps: + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_timestamp + + try: + cffobj = yaml.load(self.cffstr) + finally: + # restore the old value + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = tmp + if not isinstance(cffobj, dict): raise ValueError("Provided CITATION.cff does not seem valid YAML.") + return cffobj def as_apalike(self): diff --git a/src/cffconvert/cff_1_2_x/citation.py b/src/cffconvert/cff_1_2_x/citation.py index 630828fc..d089ab18 100644 --- a/src/cffconvert/cff_1_2_x/citation.py +++ b/src/cffconvert/cff_1_2_x/citation.py @@ -2,6 +2,7 @@ import os import jsonschema from jsonschema.exceptions import ValidationError +from ruamel.yaml import SafeConstructor from ruamel.yaml import YAML from cffconvert.cff_1_2_x.apalike import ApalikeObject from cffconvert.cff_1_2_x.bibtex import BibtexObject @@ -35,11 +36,17 @@ def _parse(self): # instantiate the YAML module: yaml = YAML(typ="safe") - # while loading, convert timestamps to string - yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = \ - yaml.constructor.yaml_constructors["tag:yaml.org,2002:str"] + # store the current value of the timestamp parser + tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp") - cffobj = yaml.load(self.cffstr) + # Configure YAML to load timestamps as strings: + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_str + + try: + cffobj = yaml.load(self.cffstr) + finally: + # restore the old value + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = tmp if not isinstance(cffobj, dict): raise ValueError("Provided CITATION.cff does not seem valid YAML.") diff --git a/src/cffconvert/cff_1_3_x/citation.py b/src/cffconvert/cff_1_3_x/citation.py index 8fb50dfc..7f3fa10c 100644 --- a/src/cffconvert/cff_1_3_x/citation.py +++ b/src/cffconvert/cff_1_3_x/citation.py @@ -2,6 +2,7 @@ import os import jsonschema from jsonschema.exceptions import ValidationError +from ruamel.yaml import SafeConstructor from ruamel.yaml import YAML from cffconvert.cff_1_3_x.apalike import ApalikeObject from cffconvert.cff_1_3_x.bibtex import BibtexObject @@ -35,11 +36,17 @@ def _parse(self): # instantiate the YAML module: yaml = YAML(typ="safe") - # while loading, convert timestamps to string - yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = \ - yaml.constructor.yaml_constructors["tag:yaml.org,2002:str"] + # store the current value of the timestamp parser + tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp") - cffobj = yaml.load(self.cffstr) + # Configure YAML to load timestamps as strings: + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_str + + try: + cffobj = yaml.load(self.cffstr) + finally: + # restore the old value + yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = tmp if not isinstance(cffobj, dict): raise ValueError("Provided CITATION.cff does not seem valid YAML.")