Skip to content

Commit

Permalink
evaluate all strings for newlines rather than pre-labeling attributes…
Browse files Browse the repository at this point in the history
… likely to have them. natcap#49
  • Loading branch information
davemfish committed Nov 4, 2024
1 parent 46e4e2b commit 7717679
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/geometamaker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def write(self, target_path):
"""
with open(target_path, 'w') as file:
file.write(utils._yaml_dump(dataclasses.asdict(self)))
file.write(utils.yaml_dump(dataclasses.asdict(self)))


@dataclass()
Expand Down Expand Up @@ -443,7 +443,7 @@ def set_lineage(self, statement):
provenance of the dataset
"""
self.lineage = utils.FoldedStr(f'{statement}')
self.lineage = statement

def get_lineage(self):
"""Get the lineage statement of the dataset.
Expand Down Expand Up @@ -509,7 +509,7 @@ def write(self, workspace=None):
workspace, os.path.basename(self.metadata_path))

with open(target_path, 'w') as file:
file.write(utils._yaml_dump(dataclasses.asdict(self)))
file.write(utils.yaml_dump(dataclasses.asdict(self)))

def to_string(self):
pass
Expand Down
23 changes: 9 additions & 14 deletions src/geometamaker/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import yaml


class FoldedStr(str):
pass
def represent_str(dumper, data):
scalar = yaml.representer.SafeRepresenter.represent_str(dumper, data)
if len(data.splitlines()) > 1:
scalar.style = '>' # fold strings with newlines
return scalar


def _change_style(style, representer):
def new_representer(dumper, data):
scalar = representer(dumper, data)
scalar.style = style
return scalar
return new_representer


represent_folded_str = _change_style(
'>', yaml.representer.SafeRepresenter.represent_str)
yaml.SafeDumper.add_representer(FoldedStr, represent_folded_str)
# Patch the default string representer so that it uses
# a folded style when the data contains newline characters
yaml.SafeDumper.add_representer(str, represent_str)


# https://stackoverflow.com/questions/13518819/avoid-references-in-pyyaml
Expand All @@ -26,7 +21,7 @@ def ignore_aliases(self, data):
return True


def _yaml_dump(data):
def yaml_dump(data):
return yaml.dump(
data,
allow_unicode=True,
Expand Down
19 changes: 18 additions & 1 deletion tests/test_geometamaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,28 @@ def test_set_and_get_lineage(self):
import geometamaker

resource = geometamaker.models.Resource()
statement = 'a lineage statment'
statement = 'a lineage statment\n is long and has\n many lines.'

resource.set_lineage(statement)
self.assertEqual(resource.get_lineage(), statement)

def test_lineage_roundtrip(self):
"""Test writing and loading yaml with custom style."""
import geometamaker

datasource_path = os.path.join(self.workspace_dir, 'raster.tif')
numpy_type = numpy.int16
create_raster(numpy_type, datasource_path)

resource = geometamaker.describe(datasource_path)

statement = 'a lineage statment\n is long and has\n many lines.'
resource.set_lineage(statement)
resource.write()

new_resource = geometamaker.describe(datasource_path)
self.assertEqual(new_resource.get_lineage(), statement)

def test_set_and_get_purpose(self):
"""Test set and get purpose of resource."""
import geometamaker
Expand Down

0 comments on commit 7717679

Please sign in to comment.