Skip to content

Commit

Permalink
Merge pull request #344 from citation-file-format/343-testing-order-bug
Browse files Browse the repository at this point in the history
testing order bug
  • Loading branch information
jspaaks committed Jul 21, 2023
2 parents 645dc46 + ee75881 commit 9aa0410
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
18 changes: 17 additions & 1 deletion src/cffconvert/cff_1_0_x/citation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 17 additions & 1 deletion src/cffconvert/cff_1_1_x/citation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 11 additions & 4 deletions src/cffconvert/cff_1_2_x/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
15 changes: 11 additions & 4 deletions src/cffconvert/cff_1_3_x/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit 9aa0410

Please sign in to comment.