Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fffb962

Browse files
committed
Honor 'Date': None in metadata
Allow removing the 'Date' metadata from output. Fix #17968
1 parent 79c26b4 commit fffb962

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ def _write_metadata(self, metadata):
360360
'Expected str, date, datetime, or iterable '
361361
'of the same, not {!r}.'.format(type(date)))
362362
metadata['Date'] = '/'.join(dates)
363-
else:
363+
elif 'Date' not in metadata:
364+
# Do not add `Date` if the user explicitly set `Date` to `None`
364365
# Get source date from SOURCE_DATE_EPOCH, if set.
365366
# See https://reproducible-builds.org/specs/source-date-epoch/
366367
date = os.getenv("SOURCE_DATE_EPOCH")

lib/matplotlib/tests/test_backend_svg.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,70 @@ def test_svg_default_metadata(monkeypatch):
371371
# Type
372372
assert 'StillImage' in buf
373373

374+
# Now make sure all the default metadata can be cleared.
375+
with BytesIO() as fd:
376+
fig.savefig(fd, format='svg', metadata={'Date': None, 'Creator': None,
377+
'Format': None, 'Type': None})
378+
buf = fd.getvalue().decode()
379+
380+
# Creator
381+
assert mpl.__version__ not in buf
382+
# Date
383+
assert '1970-08-16' not in buf
384+
# Format
385+
assert 'image/svg+xml' not in buf
386+
# Type
387+
assert 'StillImage' not in buf
388+
389+
390+
def test_svg_clear_default_metadata(monkeypatch):
391+
# Makes sure that setting a default metadata to `None`
392+
# removes the corresponding tag from the metadata.
393+
monkeypatch.setenv('SOURCE_DATE_EPOCH', '19680801')
394+
395+
metadata_contains = {'creator': mpl.__version__, 'date': '1970-08-16',
396+
'format': 'image/svg+xml', 'type': 'StillImage'}
397+
398+
SVGNS = '{http://www.w3.org/2000/svg}'
399+
RDFNS = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}'
400+
CCNS = '{http://creativecommons.org/ns#}'
401+
DCNS = '{http://purl.org/dc/elements/1.1/}'
402+
403+
fig, ax = plt.subplots()
404+
for name in metadata_contains:
405+
with BytesIO() as fd:
406+
fig.savefig(fd, format='svg', metadata={name.title(): None})
407+
buf = fd.getvalue().decode()
408+
409+
root = xml.etree.ElementTree.fromstring(buf)
410+
work, = root.findall(f'./{SVGNS}metadata/{RDFNS}RDF/{CCNS}Work')
411+
for key in metadata_contains:
412+
data = work.findall(f'./{DCNS}{key}')
413+
if key == name:
414+
# The one we cleared is not there
415+
assert not data
416+
continue
417+
# Everything else should be there
418+
data, = data
419+
xmlstr = xml.etree.ElementTree.tostring(data, encoding="unicode")
420+
assert metadata_contains[key] in xmlstr
421+
422+
423+
def test_svg_clear_all_metadata():
424+
# Makes sure that setting all default metadata to `None`
425+
# removes the metadata tag from the output.
426+
427+
fig, ax = plt.subplots()
428+
with BytesIO() as fd:
429+
fig.savefig(fd, format='svg', metadata={'Date': None, 'Creator': None,
430+
'Format': None, 'Type': None})
431+
buf = fd.getvalue().decode()
432+
433+
SVGNS = '{http://www.w3.org/2000/svg}'
434+
435+
root = xml.etree.ElementTree.fromstring(buf)
436+
assert not root.findall(f'./{SVGNS}metadata')
437+
374438

375439
def test_svg_metadata():
376440
single_value = ['Coverage', 'Identifier', 'Language', 'Relation', 'Source',

0 commit comments

Comments
 (0)