Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Convert all namedtuples to attrs. #11665

Merged
merged 17 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions synapse/config/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

import logging
import os
from collections import namedtuple
from typing import Dict, List, Tuple
from urllib.request import getproxies_environment # type: ignore

import attr

from synapse.config.server import DEFAULT_IP_RANGE_BLACKLIST, generate_ip_set
from synapse.python_dependencies import DependencyException, check_requirements
from synapse.types import JsonDict
Expand All @@ -44,18 +45,20 @@
HTTP_PROXY_SET_WARNING = """\
The Synapse config url_preview_ip_range_blacklist will be ignored as an HTTP(s) proxy is configured."""

ThumbnailRequirement = namedtuple(
"ThumbnailRequirement", ["width", "height", "method", "media_type"]
)

MediaStorageProviderConfig = namedtuple(
"MediaStorageProviderConfig",
(
"store_local", # Whether to store newly uploaded local files
"store_remote", # Whether to store newly downloaded remote files
"store_synchronous", # Whether to wait for successful storage for local uploads
),
)
@attr.s(frozen=True, slots=True, auto_attribs=True)
class ThumbnailRequirement:
width: int
height: int
method: str
media_type: str


@attr.s(frozen=True, slots=True, auto_attribs=True)
class MediaStorageProviderConfig:
store_local: bool # Whether to store newly uploaded local files
store_remote: bool # Whether to store newly downloaded remote files
store_synchronous: bool # Whether to wait for successful storage for local uploads


def parse_thumbnail_requirements(
Expand All @@ -66,11 +69,10 @@ def parse_thumbnail_requirements(
method, and thumbnail media type to precalculate

Args:
thumbnail_sizes(list): List of dicts with "width", "height", and
"method" keys
thumbnail_sizes: List of dicts with "width", "height", and "method" keys

Returns:
Dictionary mapping from media type string to list of
ThumbnailRequirement tuples.
Dictionary mapping from media type string to list of ThumbnailRequirement.
"""
requirements: Dict[str, List[ThumbnailRequirement]] = {}
for size in thumbnail_sizes:
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ async def query_room_alias_exists(

Args:
room_alias: The room alias to query.

Returns:
namedtuple: with keys "room_id" and "servers" or None if no
association can be found.
RoomAliasMapping or None if no association can be found.
"""
room_alias_str = room_alias.to_string()
services = self.store.get_app_services()
Expand Down
8 changes: 5 additions & 3 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,13 @@ async def get_association(self, room_alias: RoomAlias) -> JsonDict:

users = await self.store.get_users_in_room(room_id)
extra_servers = {get_domain_from_id(u) for u in users}
servers = set(extra_servers) | set(servers)
unique_servers = set(extra_servers) | set(servers)
clokep marked this conversation as resolved.
Show resolved Hide resolved

# If this server is in the list of servers, return it first.
if self.server_name in servers:
servers = [self.server_name] + [s for s in servers if s != self.server_name]
if self.server_name in unique_servers:
servers = [self.server_name] + [
s for s in unique_servers if s != self.server_name
]
else:
servers = list(servers)

Expand Down
19 changes: 13 additions & 6 deletions synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,21 @@ async def _generate_thumbnails(
# We deduplicate the thumbnail sizes by ignoring the cropped versions if
# they have the same dimensions of a scaled one.
thumbnails: Dict[Tuple[int, int, str], str] = {}
for r_width, r_height, r_method, r_type in requirements:
if r_method == "crop":
thumbnails.setdefault((r_width, r_height, r_type), r_method)
elif r_method == "scale":
t_width, t_height = thumbnailer.aspect(r_width, r_height)
for requirement in requirements:
if requirement.method == "crop":
thumbnails.setdefault(
(requirement.width, requirement.height, requirement.media_type),
requirement.method,
)
elif requirement.method == "scale":
t_width, t_height = thumbnailer.aspect(
requirement.width, requirement.height
)
t_width = min(m_width, t_width)
t_height = min(m_height, t_height)
thumbnails[(t_width, t_height, r_type)] = r_method
thumbnails[
(t_width, t_height, requirement.media_type)
] = requirement.method

# Now we generate the thumbnails for each dimension, store it
for (t_width, t_height, t_type), t_method in thumbnails.items():
Expand Down
10 changes: 8 additions & 2 deletions synapse/storage/databases/main/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from collections import namedtuple
from typing import Iterable, List, Optional, Tuple

import attr

from synapse.api.errors import SynapseError
from synapse.storage.database import LoggingTransaction
from synapse.storage.databases.main import CacheInvalidationWorkerStore
from synapse.types import RoomAlias
from synapse.util.caches.descriptors import cached

RoomAliasMapping = namedtuple("RoomAliasMapping", ("room_id", "room_alias", "servers"))

@attr.s(slots=True, frozen=True, auto_attribs=True)
class RoomAliasMapping:
room_id: str
room_alias: str
servers: List[str]


class DirectoryWorkerStore(CacheInvalidationWorkerStore):
Expand Down
16 changes: 14 additions & 2 deletions synapse/storage/databases/main/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
import logging
from abc import abstractmethod
from enum import Enum
from typing import TYPE_CHECKING, Any, Awaitable, Dict, List, Optional, Tuple, cast
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Dict,
List,
Optional,
Tuple,
Union,
cast,
)

from synapse.api.constants import EventContentFields, EventTypes, JoinRules
from synapse.api.errors import StoreError
Expand Down Expand Up @@ -207,6 +217,7 @@ def _count_public_rooms_txn(txn: LoggingTransaction) -> int:
WHERE appservice_id = ? AND network_id = ?
"""
query_args.append(network_tuple.appservice_id)
assert network_tuple.network_id is not None
query_args.append(network_tuple.network_id)
else:
published_sql = """
Expand Down Expand Up @@ -284,7 +295,7 @@ async def get_largest_public_rooms(
"""

where_clauses = []
query_args = []
query_args: List[Union[str, int]] = []

if network_tuple:
if network_tuple.appservice_id:
Expand All @@ -293,6 +304,7 @@ async def get_largest_public_rooms(
WHERE appservice_id = ? AND network_id = ?
"""
query_args.append(network_tuple.appservice_id)
assert network_tuple.network_id is not None
query_args.append(network_tuple.network_id)
else:
published_sql = """
Expand Down
17 changes: 7 additions & 10 deletions synapse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import abc
import re
import string
from collections import namedtuple
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -708,9 +707,11 @@ def to_room_stream_token(self) -> RoomStreamToken:
return RoomStreamToken(None, self.stream)


class ThirdPartyInstanceID(
namedtuple("ThirdPartyInstanceID", ("appservice_id", "network_id"))
):
@attr.s(slots=True, frozen=True, auto_attribs=True)
class ThirdPartyInstanceID:
appservice_id: Optional[str]
network_id: Optional[str]

# Deny iteration because it will bite you if you try to create a singleton
# set by:
# users = set(user)
Expand All @@ -725,22 +726,18 @@ def __deepcopy__(self, memo):
return self

@classmethod
def from_string(cls, s):
def from_string(cls, s: str) -> "ThirdPartyInstanceID":
bits = s.split("|", 2)
if len(bits) != 2:
raise SynapseError(400, "Invalid ID %r" % (s,))

return cls(appservice_id=bits[0], network_id=bits[1])

def to_string(self):
def to_string(self) -> str:
return "%s|%s" % (self.appservice_id, self.network_id)

__str__ = to_string

@classmethod
def create(cls, appservice_id, network_id):
return cls(appservice_id=appservice_id, network_id=network_id)


@attr.s(slots=True)
class ReadReceipt:
Expand Down