Skip to content

Commit

Permalink
replaced repeated literal string with a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
jspaaks committed Jul 24, 2023
1 parent 8c45212 commit 0d89c42
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/cffconvert/lib/cff_1_0_x/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from cffconvert.lib.cff_1_0_x.ris import RisObject
from cffconvert.lib.cff_1_0_x.schemaorg import SchemaorgObject
from cffconvert.lib.cff_1_0_x.zenodo import ZenodoObject
from cffconvert.lib.constants import YAML_TIMESTAMP_TYPE
from cffconvert.lib.contracts.citation import Contract
from cffconvert.root import get_package_root



class Citation_1_0_x(Contract): # noqa

supported_cff_versions = [
Expand All @@ -37,16 +39,16 @@ def _parse(self):
yaml = YAML(typ="safe")

# store the current value of the timestamp parser
tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp")
tmp = yaml.constructor.yaml_constructors.get(YAML_TIMESTAMP_TYPE)

# Configure YAML to load timestamps as timestamps:
yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_timestamp
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = 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
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = tmp

if not isinstance(cffobj, dict):
raise ValueError("Provided CITATION.cff does not seem valid YAML.")
Expand Down
7 changes: 4 additions & 3 deletions src/cffconvert/lib/cff_1_1_x/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from cffconvert.lib.cff_1_1_x.ris import RisObject
from cffconvert.lib.cff_1_1_x.schemaorg import SchemaorgObject
from cffconvert.lib.cff_1_1_x.zenodo import ZenodoObject
from cffconvert.lib.constants import YAML_TIMESTAMP_TYPE
from cffconvert.lib.contracts.citation import Contract
from cffconvert.root import get_package_root

Expand All @@ -35,16 +36,16 @@ def _parse(self):
yaml = YAML(typ="safe")

# store the current value of the timestamp parser
tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp")
tmp = yaml.constructor.yaml_constructors.get(YAML_TIMESTAMP_TYPE)

# Configure YAML to load timestamps as timestamps:
yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_timestamp
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = 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
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = tmp

if not isinstance(cffobj, dict):
raise ValueError("Provided CITATION.cff does not seem valid YAML.")
Expand Down
7 changes: 4 additions & 3 deletions src/cffconvert/lib/cff_1_2_x/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cffconvert.lib.cff_1_2_x.ris import RisObject
from cffconvert.lib.cff_1_2_x.schemaorg import SchemaorgObject
from cffconvert.lib.cff_1_2_x.zenodo import ZenodoObject
from cffconvert.lib.constants import YAML_TIMESTAMP_TYPE
from cffconvert.lib.contracts.citation import Contract
from cffconvert.root import get_package_root

Expand All @@ -37,16 +38,16 @@ def _parse(self):
yaml = YAML(typ="safe")

# store the current value of the timestamp parser
tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp")
tmp = yaml.constructor.yaml_constructors.get(YAML_TIMESTAMP_TYPE)

# Configure YAML to load timestamps as strings:
yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_str
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = 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
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = tmp

if not isinstance(cffobj, dict):
raise ValueError("Provided CITATION.cff does not seem valid YAML.")
Expand Down
7 changes: 4 additions & 3 deletions src/cffconvert/lib/cff_1_3_x/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from cffconvert.lib.cff_1_3_x.ris import RisObject
from cffconvert.lib.cff_1_3_x.schemaorg import SchemaorgObject
from cffconvert.lib.cff_1_3_x.zenodo import ZenodoObject
from cffconvert.lib.constants import YAML_TIMESTAMP_TYPE
from cffconvert.lib.contracts.citation import Contract
from cffconvert.root import get_package_root

Expand All @@ -37,16 +38,16 @@ def _parse(self):
yaml = YAML(typ="safe")

# store the current value of the timestamp parser
tmp = yaml.constructor.yaml_constructors.get("tag:yaml.org,2002:timestamp")
tmp = yaml.constructor.yaml_constructors.get(YAML_TIMESTAMP_TYPE)

# Configure YAML to load timestamps as strings:
yaml.constructor.yaml_constructors["tag:yaml.org,2002:timestamp"] = SafeConstructor.construct_yaml_str
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = 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
yaml.constructor.yaml_constructors[YAML_TIMESTAMP_TYPE] = tmp

if not isinstance(cffobj, dict):
raise ValueError("Provided CITATION.cff does not seem valid YAML.")
Expand Down
1 change: 1 addition & 0 deletions src/cffconvert/lib/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YAML_TIMESTAMP_TYPE = "tag:yaml.org,2002:timestamp"

0 comments on commit 0d89c42

Please sign in to comment.