Skip to content

Commit 804b92a

Browse files
committed
better functionality to integrate media data
1 parent 4819df5 commit 804b92a

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
The `pycldf` package adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
44

55

6+
## Unreleased
7+
8+
Added support for reading media file data from locally downloaded zip archives. This helps with the
9+
use case of datasets linking to media in separate Zenodo deposits.
10+
11+
612
## [2.0.2] - 2026-05-14
713

814
- Fix bug whereby pipes in regular expression patterns would screw up table

src/pycldf/media.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,19 @@ def mimetype(self) -> 'Mimetype':
139139

140140
def local_path(self, d: pathlib.Path = None) -> Optional[pathlib.Path]:
141141
"""
142-
:return: The expected path of the file in the directory `d`.
142+
:return: The expected path of the file in the directory `d` in the case of files \
143+
downloaded using the `downloadmedia` command. If `d` is a file it is accepted as full \
144+
local path if it has the same name as the filename in the URL.
143145
"""
144146
if d is None:
145147
if self.scheme == 'file':
146148
return self._dsdir / urllib.parse.unquote(self.relpath)
147149
return None
150+
if d.is_file() and self.parsed_url:
151+
if d.name == pathlib.Path(self.parsed_url.path).name:
152+
# Support the use case of files in zip archives, where the archives are available
153+
# locally, e.g. as download of a separate media file deposit from Zenodo.
154+
return d
148155
zip_ext = '.zip' if self.path_in_zip else (self.mimetype.extension or '')
149156
return d.joinpath(f'{self.id}{zip_ext}')
150157

@@ -155,14 +162,14 @@ def read_json(self, d=None):
155162

156163
def read(self, d: Optional[pathlib.Path] = None) -> Optional[StrOrBytes]:
157164
"""
158-
:param d: A local directory where the file has been saved before. If `None`, the content \
159-
will be read from the file's URL.
165+
:param d: A local path where the file has been saved before - as expected by `local_path`. \
166+
If `None`, the content will be read from the file's URL.
160167
"""
161168
if self.path_in_zip:
162169
zipcontent = None
163170
if d:
164171
zipcontent = self.local_path(d).read_bytes()
165-
if self.url:
172+
if zipcontent is None and self.url:
166173
zipcontent = self.url_reader[self.scheme](
167174
self.parsed_url, Mimetype('application/zip'))
168175
if zipcontent:

tests/test_media.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ def test_Media(tmp_path, ds_factory):
194194
assert list(MediaTable(ds))[0].read() == 'äöü'
195195
assert list(MediaTable(ds))[0].read(d=tmp_path) == 'äöü'
196196

197+
ds = ds_factory(dict(
198+
ID='x',
199+
Download_URL="http://example.com/path/123.zip",
200+
Media_Type='text/plain',
201+
Path_In_Zip='arc/name',
202+
))
203+
assert list(MediaTable(ds))[0].read(d=tmp_path / '123.zip') == 'äöü'
204+
197205

198206
def test_save_read_zipped_media(dataset_with_trees, tmp_path):
199207
zipped = None

0 commit comments

Comments
 (0)