Skip to content

Commit cc52826

Browse files
committed
chores: docstring update
1 parent b5174cf commit cc52826

3 files changed

Lines changed: 43 additions & 35 deletions

File tree

synapse/media/media_repository.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -925,11 +925,11 @@ async def copy_media(
925925
# Let existing methods handle creating the new file for us. By not passing a
926926
# media id, one will be created.
927927
new_mxc_uri = await self.create_or_update_content(
928-
old_media_info.media_type,
929-
old_media_info.upload_name,
930-
io_object,
931-
old_media_info.media_length,
932-
auth_user,
928+
media_type=old_media_info.media_type,
929+
upload_name=old_media_info.upload_name,
930+
content=io_object,
931+
content_length=old_media_info.media_length,
932+
auth_user=auth_user,
933933
restricted=True,
934934
)
935935

@@ -1340,7 +1340,6 @@ async def _get_remote_media_impl(
13401340
await self.is_media_visible(requester.user, media_info)
13411341

13421342
file_id = media_info.filesystem_id
1343-
13441343
if not media_info.media_type:
13451344
media_info = attr.evolve(media_info, media_type="application/octet-stream")
13461345

tests/media/test_media_repository.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def create_resource_dict(self) -> dict[str, Resource]:
5959
return resources
6060

6161
def _store_media_with_path(self, file_info: FileInfo, expected_path: str) -> None:
62+
"""Store media with given path."""
6263
assert isinstance(self.repo, MediaRepository)
6364
ctx = self.repo.media_storage.store_into_file(file_info)
6465
(f, fname) = self.get_success(ctx.__aenter__())
@@ -70,8 +71,8 @@ def _store_media_with_path(self, file_info: FileInfo, expected_path: str) -> Non
7071

7172
def _create_local_media_with_media_id_path(self, user: str) -> MXCUri:
7273
"""
73-
Force creation of a media object at the specific place based on the media id
74-
path. This does assert that the file exists where it is expected to be
74+
Force creation of a local media object at the specific place based on the media
75+
id path. This does assert that the file exists where it is expected to be
7576
"""
7677
assert isinstance(self.repo, MediaRepository)
7778
media_id = random_string(24)
@@ -121,6 +122,7 @@ def _create_local_media_with_media_id_path(self, user: str) -> MXCUri:
121122
return MXCUri(server_name=self.hs.config.server.server_name, media_id=media_id)
122123

123124
def _create_local_media_with_sha256_path(self, user: str) -> MXCUri:
125+
"""Creates local media object with sha256 path."""
124126
assert isinstance(self.repo, MediaRepository)
125127
media_id = random_string(24)
126128
# Curate a specialized FileInfo that includes sha256 data, then file will be
@@ -162,18 +164,17 @@ def _create_local_media_with_sha256_path(self, user: str) -> MXCUri:
162164
media_id=media_id,
163165
file_id=media_id,
164166
media_type="image/png",
165-
# We purposely do not use the sha256 here, as it directly causes the sha256
166-
# path for thumbnails to be populated, and that is not what we are looking
167-
# for.
168167
sha256=SMALL_PNG_SHA256,
169168
)
170169
)
171-
172170
return MXCUri(server_name=self.hs.config.server.server_name, media_id=media_id)
173171

174172
def _create_remote_media_with_media_id_path(self, server_name: str) -> str:
173+
"""Creates remote media object with media id path."""
175174
media_id = random_string(24)
176175
upload_name = "other_server_media"
176+
# Curate a specialized FileInfo that is lacking sha256 data, then file will be
177+
# forced to the old media_id path
177178
file_info = FileInfo(
178179
server_name=server_name,
179180
file_id=media_id,
@@ -205,6 +206,7 @@ def _create_remote_media_with_media_id_path(self, server_name: str) -> str:
205206
return media_id
206207

207208
def _create_remote_media_with_sha256_path(self, server_name: str) -> str:
209+
"""Creates remote media object with sha256 path."""
208210
media_id = random_string(24)
209211
upload_name = "other_server_media"
210212
file_info = FileInfo(
@@ -265,11 +267,12 @@ def test_create_or_update_content_creates_new_content_with_sha256_path(
265267
assert not os.path.exists(old_path)
266268

267269
def test_create_or_update_content_updates_content_with_media_id_path(self) -> None:
268-
"""Test that `create_or_update_content` function can update existing media with media_id path"""
269-
# Strictly speaking, this is not an operation that is supposed to be supported,
270-
# but is currently possible. In the case it becomes necessary, the behavior
271-
# should not be unexpected.
272-
270+
"""
271+
Test that `create_or_update_content` function can update existing media with
272+
media_id path. Strictly speaking, this is not an operation that is supposed to
273+
be supported, but is currently possible. In the case it becomes necessary, the
274+
behavior should not be unexpected.
275+
"""
273276
# Create media with media_id path
274277
mxc_uri = self._create_local_media_with_media_id_path(self.creator)
275278
media_id = mxc_uri.media_id
@@ -291,11 +294,12 @@ def test_create_or_update_content_updates_content_with_media_id_path(self) -> No
291294
assert os.path.exists(self.repo.filepaths.local_media_filepath(media_id))
292295

293296
def test_create_or_update_content_updates_content_with_sha256_path(self) -> None:
294-
"""Test that `create_or_update_content` function can update existing media with sha256 path"""
295-
# Strictly speaking, this is not an operation that is supposed to be supported,
296-
# but is currently possible. In the case it becomes necessary, the behavior
297-
# should not be unexpected.
298-
297+
"""
298+
Test that `create_or_update_content` function can update existing media with
299+
sha256 path. Strictly speaking, this is not an operation that is supposed to be
300+
supported, but is currently possible. In the case it becomes necessary, the
301+
behavior should not be unexpected.
302+
"""
299303
# Create media with sha256 path
300304
mxc_uri = self._create_local_media_with_sha256_path(self.creator)
301305
media_id = mxc_uri.media_id
@@ -392,6 +396,10 @@ def test_get_local_media_with_sha256_path(self) -> None:
392396
self.assertEqual(channel.result["body"], SMALL_PNG)
393397

394398
def test_get_remote_media_impl_with_sha256_path_cache_hit(self) -> None:
399+
"""
400+
Test `_get_remote_media_impl` can fetch the media with sha256 path successfully
401+
in case of cache hit.
402+
"""
395403
server_name = "other_server.com"
396404
# Generate remote media with sha256 path
397405
media_id = self._create_remote_media_with_sha256_path(server_name)
@@ -423,7 +431,10 @@ def test_get_remote_media_impl_with_sha256_path_cache_hit(self) -> None:
423431
def test_get_remote_media_impl_with_sha256_path_cache_miss_no_federation(
424432
self,
425433
) -> None:
426-
"""Test that `_get_remote_media_impl` can fetch the media with sha256 path successfully"""
434+
"""
435+
Test that `_get_remote_media_impl` can fetch the media with sha256 path
436+
successfully in case of cache miss, without using federation endpoint.
437+
"""
427438

428439
# Mock the download media function of the client.
429440
async def _mock_download_media(
@@ -492,7 +503,7 @@ async def _mock_download_media(
492503
assert thumbnail is not None
493504

494505
def test_remove_local_media_from_disk_with_media_id_path(self) -> None:
495-
"""Test that `_remove_local_media_from_disk` can remove media with media_id path"""
506+
"""Test that `_remove_local_media_from_disk` can remove media with media_id path."""
496507
# Generate 2 media with media_id path
497508
media1_mxc = self._create_local_media_with_media_id_path(self.creator)
498509
media1_id = media1_mxc.media_id
@@ -509,7 +520,7 @@ def test_remove_local_media_from_disk_with_media_id_path(self) -> None:
509520
assert not os.path.exists(self.repo.filepaths.local_media_filepath(media2_id))
510521

511522
def test_remove_local_media_from_disk_with_sha256_path(self) -> None:
512-
"""Test that `_remove_local_media_from_disk` can remove media with sha256 path"""
523+
"""Test that `_remove_local_media_from_disk` can remove media with sha256 path."""
513524
# Generate 2 media with sha256 path with the same image.
514525
# There should be 2 rows in the table and only 1 media stored in filesystem.
515526
media1_mxc = self._create_local_media_with_sha256_path(self.creator)

tests/rest/client/test_media.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5098,6 +5098,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
50985098
self.tok = self.login("user", "pass")
50995099

51005100
def _create_local_media_with_sha256_path(self) -> str:
5101+
"""Create local media with sha256 path."""
51015102
assert isinstance(self.repo, MediaRepository)
51025103
media_id = random_string(24)
51035104
# Curate a specialized FileInfo that includes sha256 data, then file will be
@@ -5139,16 +5140,13 @@ def _create_local_media_with_sha256_path(self) -> str:
51395140
media_id=media_id,
51405141
file_id=media_id,
51415142
media_type="image/png",
5142-
# We purposely do not use the sha256 here, as it directly causes the sha256
5143-
# path for thumbnails to be populated, and that is not what we are looking
5144-
# for.
51455143
sha256=SMALL_PNG_SHA256,
51465144
)
51475145
)
5148-
51495146
return media_id
51505147

51515148
def _create_local_media_with_media_id_path(self) -> str:
5149+
"""Create local media with media id path."""
51525150
assert isinstance(self.repo, MediaRepository)
51535151
media_id = random_string(24)
51545152
# Curate a specialized FileInfo that is lacking sha256 data, then file will be
@@ -5217,7 +5215,7 @@ async def _mock_federation_download_media(
52175215
return 67, headers
52185216

52195217
def test_download_remote_media_saves_file_in_sha256_path(self) -> None:
5220-
"""Test that downloading remote media using sha256 path."""
5218+
"""Test downloading remote media using sha256 path via download endpoint."""
52215219
# Mock the federation download media
52225220
self.repo.client.federation_download_media = ( # type: ignore
52235221
self._mock_federation_download_media
@@ -5251,10 +5249,10 @@ def test_download_remote_media_saves_file_in_sha256_path(self) -> None:
52515249
def test_download_local_media_with_media_id_path(
52525250
self,
52535251
) -> None:
5254-
"""Test that downloading local media with media_id path serves correct file."""
5255-
# Specifically, that a preexisting file on the legacy media paths can still be
5256-
# served from its present location.
5257-
5252+
"""Test that downloading local media with media_id path serves correct file.
5253+
Specifically, that a preexisting file on the media id path(legacy) can still be
5254+
served from its present location.
5255+
"""
52585256
media_id = self._create_local_media_with_media_id_path()
52595257
channel = self.make_request(
52605258
"GET",
@@ -5325,7 +5323,7 @@ def test_copy_remote_media_with_sha256_path(self) -> None:
53255323
)
53265324

53275325
def test_copy_local_media_with_media_id(self) -> None:
5328-
"""Test that copying media with media_id path works correctly, when sha256 path is enabled"""
5326+
"""Test that copying media with media_id path works correctly, when sha256 path is enabled."""
53295327
# Create local media with media_id path.
53305328
media_id = self._create_local_media_with_media_id_path()
53315329

0 commit comments

Comments
 (0)