Skip to content

Commit

Permalink
Added an option to write to skip the date metadata (#442)
Browse files Browse the repository at this point in the history
For auto-generated SVG under VCS, the <dc:date> metadata generates spurious diffs which would be best avoided.

* adds a `--dont-set-date` option to the `write` command
* adds a `set_date` parameter (default: `True`) to the `vpype.write_svg()` API

Fixes #438
  • Loading branch information
abey79 authored Mar 31, 2022
1 parent 9d300d0 commit 22e2dab
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Release date: UNRELEASED
### New features and improvements

* Added HPGL configuration for the Calcomp Artisan plotter (thanks to Andee Collard and @ithinkido) (#418)
* Added the `--dont-set-date` option to the `write` command (#442)


### Bug fixes
Expand All @@ -18,6 +19,7 @@ Release date: UNRELEASED

* Added `vpype_cli.FloatType()`, `vpype_cli.IntRangeType()`, and `vpype_cli.ChoiceType()` (#430)
* Changed `vpype.Document.add_to_sources()` to also modify the `vp_source` property (#431)
* Added a `set_date:bool = True` argument to `vpype.write_svg()` (#442)


### Other changes
Expand Down
12 changes: 12 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,15 @@ def test_read_by_attr_stdin_sets_source_properties(monkeypatch):
doc = vpype_cli.execute(f"read -a stroke -a fill -")
assert vp.METADATA_FIELD_SOURCE not in doc.metadata
assert doc.sources == set()


def test_write_set_date(capsys):
vpype_cli.execute("line 0 0 10 10 write -f svg -")
output = capsys.readouterr().out
assert "<dc:date>" in output


def test_write_dont_set_date(capsys):
vpype_cli.execute("line 0 0 10 10 write -f svg --dont-set-date -")
output = capsys.readouterr().out
assert "<dc:date>" not in output
8 changes: 6 additions & 2 deletions vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ def write_svg(
show_pen_up: bool = False,
color_mode: str = "default",
use_svg_metadata: bool = False,
set_date: bool = True,
) -> None:
"""Create a SVG from a :py:class:`Document` instance.
Expand Down Expand Up @@ -665,6 +666,8 @@ def write_svg(
color_mode: "default" (system property), "none" (no formatting), "layer" (one color per
layer), "path" (one color per path)
use_svg_metadata: apply ``svg_``-prefixed properties as SVG attributes
set_date: controls whether the current date and time is set in the SVG's metadata
(disabling it is useful for auto-generated SVG under VCS)
"""

# compute bounds
Expand Down Expand Up @@ -713,8 +716,9 @@ def write_svg(
fmt.text = "image/svg+xml"
source = ElementTree.SubElement(work, "dc:source")
source.text = source_string
date = ElementTree.SubElement(work, "dc:date")
date.text = datetime.datetime.now().isoformat()
if set_date:
date = ElementTree.SubElement(work, "dc:date")
date.text = datetime.datetime.now().isoformat()
dwg.set_metadata(metadata)

color_idx = 0
Expand Down
9 changes: 8 additions & 1 deletion vpype_cli/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@
"--restore-attribs",
is_flag=True,
default=False,
help="[SVG only] attempt to restore SVG attributes from properties",
help="[SVG only] attempt to restore SVG attributes from properties.",
)
@click.option(
"--dont-set-date",
is_flag=True,
help="[SVG only] do not add date metadata (useful for auto-generated SVG under VCS).",
)
@click.option(
"-d", "--device", type=TextType(), help="[HPGL only] Type of the plotter device."
Expand Down Expand Up @@ -203,6 +208,7 @@ def write(
center: bool,
layer_label: str | None,
restore_attribs: bool,
dont_set_date: bool,
pen_up: bool,
color_mode: str,
device: str | None,
Expand Down Expand Up @@ -238,6 +244,7 @@ def write(
show_pen_up=pen_up,
color_mode=color_mode,
use_svg_metadata=restore_attribs,
set_date=not dont_set_date,
)
elif file_format == "hpgl":
if not page_size:
Expand Down

0 comments on commit 22e2dab

Please sign in to comment.