Skip to content

Commit

Permalink
Avoid overwrite existing YAML files if no change has been made (#987)
Browse files Browse the repository at this point in the history
Do not overwrite existing yaml files by default.
  • Loading branch information
jesper-friis authored Oct 29, 2024
1 parent 63f3829 commit bbd331b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
10 changes: 10 additions & 0 deletions bindings/python/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,13 @@ test_success(
dlite-validate-empty
dlite-validate Empty.json
)
# Test valid yaml
test_success(
dlite-validate-valid1-yaml
dlite-validate ../tests/entities/ValidYaml1.yaml
)
# Test invalid yaml
test_failure(
dlite-validate-invalid1-yaml
dlite-validate ../tests/entities/InvalidYaml1.yaml
)
14 changes: 14 additions & 0 deletions bindings/python/tests/entities/InvalidYaml1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# An invalid version of datamodel Valid1.yaml
uri: http://onto-ns.com/meta/0.1/InvalidYaml1
description: An datamodel for an item with float type and invalid shape name.
dimensions:
nf: Number of eigen-frequencies.
properties:
name:
type: str
description: Name of the item.
f:
type: float64
shape: [nf_MISPELLED]
unit: Hz
description: The magic eigen-frequencies of the item.
14 changes: 14 additions & 0 deletions bindings/python/tests/entities/ValidYaml1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# An valid version of datamodel Invalid1.yaml
uri: http://onto-ns.com/meta/0.1/ValidYaml1
description: An datamodel for an item with float type and invalid shape name.
dimensions:
nf: Number of eigen-frequencies.
properties:
name:
type: str
description: Name of the item.
f:
type: float64
shape: [nf]
unit: Hz
description: The magic eigen-frequencies of the item.
6 changes: 3 additions & 3 deletions bindings/python/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@
location: Path to YAML file.
options: Supported options:
- `mode`: Mode for opening. Valid values are:
- `a`: Append to existing file or create new file (default).
- `r`: Open existing file for read-only.
- `w`: Truncate existing file or create new file.
- `a`: Open for writing, add to existing `location` (default).
- `r`: Open existing `location` for reading.
- `w`: Open for writing. If `location` exists, it is truncated.
- `soft7`: Whether to save using SOFT7 format.
- `single`: Whether the input is assumed to be in single-entity form.
If "auto" (default) the form will be inferred automatically.
Expand Down
14 changes: 7 additions & 7 deletions storages/python/python-storage-plugins/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def open(self, location: str, options=None):
location: Path to YAML file.
options: Supported options:
- `mode`: Mode for opening. Valid values are:
- `a`: Append to existing file or create new file (default).
- `r`: Open existing file for read-only.
- `w`: Truncate existing file or create new file.
- `a`: Open for writing, add to existing `location` (default).
- `r`: Open existing `location` for reading.
- `w`: Open for writing. If `location` exists, it is truncated.
- `soft7`: Whether to save using SOFT7 format.
- `single`: Whether the input is assumed to be in single-entity form.
If "auto" (default) the form will be inferred automatically.
Expand All @@ -35,13 +35,13 @@ def open(self, location: str, options=None):
self.options = Options(
options, defaults="mode=a;soft7=true;single=auto;with_uuid=false"
)
self.readable = "r" in self.options.mode
self.writable = "r" != self.options.mode
mode = self.options.mode
self.writable = "w" in mode or "a" in mode
self.generic = True
self.location = location
self.flushed = False # whether buffered data has been written to file
self.flushed = True # whether buffered data has been written to file
self._data = {} # data buffer
if self.options.mode in ("r", "a", "append"):
if "r" in mode or "a" in mode:
with open(location, "r") as f:
data = pyyaml.safe_load(f)
if data:
Expand Down

0 comments on commit bbd331b

Please sign in to comment.