Compare commits

...

2 Commits

Author SHA1 Message Date
Andrew Morgan
3125e23a8e newsfile 2025-09-11 16:35:00 +01:00
anoa's Codex Agent
ed3218e164 Update method of locating package files
`pkg_resources` has been deprecated and will be removed from setuptools
on 2025-11-09 or later.
2025-09-11 16:33:04 +01:00
7 changed files with 31 additions and 23 deletions

1
changelog.d/18910.misc Normal file
View File

@@ -0,0 +1 @@
Replace usages of the deprecated `pkg_resources` interface in preparation of setuptools dropping it soon.

View File

@@ -22,6 +22,7 @@
import argparse import argparse
import errno import errno
import importlib.resources as importlib_resources
import logging import logging
import os import os
import re import re
@@ -46,7 +47,6 @@ from typing import (
import attr import attr
import jinja2 import jinja2
import pkg_resources
import yaml import yaml
from synapse.types import StrSequence from synapse.types import StrSequence
@@ -174,8 +174,8 @@ class Config:
self.root = root_config self.root = root_config
# Get the path to the default Synapse template directory # Get the path to the default Synapse template directory
self.default_template_dir = pkg_resources.resource_filename( self.default_template_dir = str(
"synapse", "res/templates" importlib_resources.files("synapse").joinpath("res").joinpath("templates")
) )
@staticmethod @staticmethod

View File

@@ -18,13 +18,13 @@
# [This file includes modifications made by New Vector Limited] # [This file includes modifications made by New Vector Limited]
# #
# #
import importlib.resources as importlib_resources
import json import json
import re import re
from typing import Any, Dict, Iterable, List, Optional, Pattern from typing import Any, Dict, Iterable, List, Optional, Pattern
from urllib import parse as urlparse from urllib import parse as urlparse
import attr import attr
import pkg_resources
from synapse.types import JsonDict, StrSequence from synapse.types import JsonDict, StrSequence
@@ -64,7 +64,12 @@ class OembedConfig(Config):
""" """
# Whether to use the packaged providers.json file. # Whether to use the packaged providers.json file.
if not oembed_config.get("disable_default_providers") or False: if not oembed_config.get("disable_default_providers") or False:
with pkg_resources.resource_stream("synapse", "res/providers.json") as s: path = (
importlib_resources.files("synapse")
.joinpath("res")
.joinpath("providers.json")
)
with path.open("r", encoding="utf-8") as s:
providers = json.load(s) providers = json.load(s)
yield from self._parse_and_validate_provider( yield from self._parse_and_validate_provider(

View File

@@ -43,7 +43,7 @@ from typing import (
) )
import attr import attr
from pkg_resources import parse_version from packaging.version import parse as parse_version
from prometheus_client import ( from prometheus_client import (
CollectorRegistry, CollectorRegistry,
Counter, Counter,

View File

@@ -18,12 +18,12 @@
# #
# #
import email.message import email.message
import importlib.resources as importlib_resources
import os import os
from http import HTTPStatus from http import HTTPStatus
from typing import Any, Dict, List, Sequence, Tuple from typing import Any, Dict, List, Sequence, Tuple
import attr import attr
import pkg_resources
from parameterized import parameterized from parameterized import parameterized
from twisted.internet.defer import Deferred from twisted.internet.defer import Deferred
@@ -59,11 +59,12 @@ class EmailPusherTests(HomeserverTestCase):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
config = self.default_config() config = self.default_config()
templates = (
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
)
config["email"] = { config["email"] = {
"enable_notifs": True, "enable_notifs": True,
"template_dir": os.path.abspath( "template_dir": os.path.abspath(str(templates)),
pkg_resources.resource_filename("synapse", "res/templates")
),
"expiry_template_html": "notice_expiry.html", "expiry_template_html": "notice_expiry.html",
"expiry_template_text": "notice_expiry.txt", "expiry_template_text": "notice_expiry.txt",
"notif_template_html": "notif_mail.html", "notif_template_html": "notif_mail.html",

View File

@@ -18,6 +18,7 @@
# [This file includes modifications made by New Vector Limited] # [This file includes modifications made by New Vector Limited]
# #
# #
import importlib.resources as importlib_resources
import os import os
import re import re
from email.parser import Parser from email.parser import Parser
@@ -25,8 +26,6 @@ from http import HTTPStatus
from typing import Any, Dict, List, Optional, Union from typing import Any, Dict, List, Optional, Union
from unittest.mock import Mock from unittest.mock import Mock
import pkg_resources
from twisted.internet.interfaces import IReactorTCP from twisted.internet.interfaces import IReactorTCP
from twisted.internet.testing import MemoryReactor from twisted.internet.testing import MemoryReactor
@@ -59,11 +58,12 @@ class PasswordResetTestCase(unittest.HomeserverTestCase):
config = self.default_config() config = self.default_config()
# Email config. # Email config.
templates = (
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
)
config["email"] = { config["email"] = {
"enable_notifs": False, "enable_notifs": False,
"template_dir": os.path.abspath( "template_dir": os.path.abspath(str(templates)),
pkg_resources.resource_filename("synapse", "res/templates")
),
"smtp_host": "127.0.0.1", "smtp_host": "127.0.0.1",
"smtp_port": 20, "smtp_port": 20,
"require_transport_security": False, "require_transport_security": False,
@@ -798,11 +798,12 @@ class ThreepidEmailRestTestCase(unittest.HomeserverTestCase):
config = self.default_config() config = self.default_config()
# Email config. # Email config.
templates = (
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
)
config["email"] = { config["email"] = {
"enable_notifs": False, "enable_notifs": False,
"template_dir": os.path.abspath( "template_dir": os.path.abspath(str(templates)),
pkg_resources.resource_filename("synapse", "res/templates")
),
"smtp_host": "127.0.0.1", "smtp_host": "127.0.0.1",
"smtp_port": 20, "smtp_port": 20,
"require_transport_security": False, "require_transport_security": False,

View File

@@ -20,12 +20,11 @@
# #
# #
import datetime import datetime
import importlib.resources as importlib_resources
import os import os
from typing import Any, Dict, List, Tuple from typing import Any, Dict, List, Tuple
from unittest.mock import AsyncMock from unittest.mock import AsyncMock
import pkg_resources
from twisted.internet.testing import MemoryReactor from twisted.internet.testing import MemoryReactor
import synapse.rest.admin import synapse.rest.admin
@@ -981,11 +980,12 @@ class AccountValidityRenewalByEmailTestCase(unittest.HomeserverTestCase):
# Email config. # Email config.
templates = (
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
)
config["email"] = { config["email"] = {
"enable_notifs": True, "enable_notifs": True,
"template_dir": os.path.abspath( "template_dir": os.path.abspath(str(templates)),
pkg_resources.resource_filename("synapse", "res/templates")
),
"expiry_template_html": "notice_expiry.html", "expiry_template_html": "notice_expiry.html",
"expiry_template_text": "notice_expiry.txt", "expiry_template_text": "notice_expiry.txt",
"notif_template_html": "notif_mail.html", "notif_template_html": "notif_mail.html",