Create a PDF infoDict based on user-supplied metadata. A default ``Creator``, ``Producer``, and ``CreationDate`` are added, though the user metadata may override it. The date may be the current time, or a time set by the ``SOURCE_DATE_EPOCH`` environment variable. Metadata is
(backend, metadata)
| 114 | |
| 115 | |
| 116 | def _create_pdf_info_dict(backend, metadata): |
| 117 | """ |
| 118 | Create a PDF infoDict based on user-supplied metadata. |
| 119 | |
| 120 | A default ``Creator``, ``Producer``, and ``CreationDate`` are added, though |
| 121 | the user metadata may override it. The date may be the current time, or a |
| 122 | time set by the ``SOURCE_DATE_EPOCH`` environment variable. |
| 123 | |
| 124 | Metadata is verified to have the correct keys and their expected types. Any |
| 125 | unknown keys/types will raise a warning. |
| 126 | |
| 127 | Parameters |
| 128 | ---------- |
| 129 | backend : str |
| 130 | The name of the backend to use in the Producer value. |
| 131 | |
| 132 | metadata : dict[str, Union[str, datetime, Name]] |
| 133 | A dictionary of metadata supplied by the user with information |
| 134 | following the PDF specification, also defined in |
| 135 | `~.backend_pdf.PdfPages` below. |
| 136 | |
| 137 | If any value is *None*, then the key will be removed. This can be used |
| 138 | to remove any pre-defined values. |
| 139 | |
| 140 | Returns |
| 141 | ------- |
| 142 | dict[str, Union[str, datetime, Name]] |
| 143 | A validated dictionary of metadata. |
| 144 | """ |
| 145 | |
| 146 | # get source date from SOURCE_DATE_EPOCH, if set |
| 147 | # See https://reproducible-builds.org/specs/source-date-epoch/ |
| 148 | source_date_epoch = os.getenv("SOURCE_DATE_EPOCH") |
| 149 | if source_date_epoch: |
| 150 | source_date = datetime.fromtimestamp(int(source_date_epoch), timezone.utc) |
| 151 | source_date = source_date.replace(tzinfo=UTC) |
| 152 | else: |
| 153 | source_date = datetime.today() |
| 154 | |
| 155 | info = { |
| 156 | 'Creator': f'Matplotlib v{mpl.__version__}, https://matplotlib.org', |
| 157 | 'Producer': f'Matplotlib {backend} backend v{mpl.__version__}', |
| 158 | 'CreationDate': source_date, |
| 159 | **metadata |
| 160 | } |
| 161 | info = {k: v for (k, v) in info.items() if v is not None} |
| 162 | |
| 163 | def is_string_like(x): |
| 164 | return isinstance(x, str) |
| 165 | is_string_like.text_for_warning = "an instance of str" |
| 166 | |
| 167 | def is_date(x): |
| 168 | return isinstance(x, datetime) |
| 169 | is_date.text_for_warning = "an instance of datetime.datetime" |
| 170 | |
| 171 | def check_trapped(x): |
| 172 | if isinstance(x, Name): |
| 173 | return x.name in (b'True', b'False', b'Unknown') |