Skip to content

Commit 8c98cf7

Browse files
Remove usage of deprecated pkg_resources interface (#18910)
1 parent ec64c3e commit 8c98cf7

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

changelog.d/18910.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace usages of the deprecated `pkg_resources` interface in preparation of setuptools dropping it soon.

synapse/config/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import argparse
2424
import errno
25+
import importlib.resources as importlib_resources
2526
import logging
2627
import os
2728
import re
@@ -46,7 +47,6 @@
4647

4748
import attr
4849
import jinja2
49-
import pkg_resources
5050
import yaml
5151

5252
from synapse.types import StrSequence
@@ -174,8 +174,8 @@ def __init__(self, root_config: "RootConfig"):
174174
self.root = root_config
175175

176176
# Get the path to the default Synapse template directory
177-
self.default_template_dir = pkg_resources.resource_filename(
178-
"synapse", "res/templates"
177+
self.default_template_dir = str(
178+
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
179179
)
180180

181181
@staticmethod

synapse/config/oembed.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
# [This file includes modifications made by New Vector Limited]
1919
#
2020
#
21+
import importlib.resources as importlib_resources
2122
import json
2223
import re
2324
from typing import Any, Dict, Iterable, List, Optional, Pattern
2425
from urllib import parse as urlparse
2526

2627
import attr
27-
import pkg_resources
2828

2929
from synapse.types import JsonDict, StrSequence
3030

@@ -64,7 +64,12 @@ def _parse_and_validate_providers(
6464
"""
6565
# Whether to use the packaged providers.json file.
6666
if not oembed_config.get("disable_default_providers") or False:
67-
with pkg_resources.resource_stream("synapse", "res/providers.json") as s:
67+
path = (
68+
importlib_resources.files("synapse")
69+
.joinpath("res")
70+
.joinpath("providers.json")
71+
)
72+
with path.open("r", encoding="utf-8") as s:
6873
providers = json.load(s)
6974

7075
yield from self._parse_and_validate_provider(

synapse/metrics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
)
4444

4545
import attr
46-
from pkg_resources import parse_version
46+
from packaging.version import parse as parse_version
4747
from prometheus_client import (
4848
CollectorRegistry,
4949
Counter,

tests/push/test_email.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
#
1919
#
2020
import email.message
21+
import importlib.resources as importlib_resources
2122
import os
2223
from http import HTTPStatus
2324
from typing import Any, Dict, List, Sequence, Tuple
2425

2526
import attr
26-
import pkg_resources
2727
from parameterized import parameterized
2828

2929
from twisted.internet.defer import Deferred
@@ -59,11 +59,12 @@ class EmailPusherTests(HomeserverTestCase):
5959

6060
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
6161
config = self.default_config()
62+
templates = (
63+
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
64+
)
6265
config["email"] = {
6366
"enable_notifs": True,
64-
"template_dir": os.path.abspath(
65-
pkg_resources.resource_filename("synapse", "res/templates")
66-
),
67+
"template_dir": os.path.abspath(str(templates)),
6768
"expiry_template_html": "notice_expiry.html",
6869
"expiry_template_text": "notice_expiry.txt",
6970
"notif_template_html": "notif_mail.html",

tests/rest/client/test_account.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
# [This file includes modifications made by New Vector Limited]
1919
#
2020
#
21+
import importlib.resources as importlib_resources
2122
import os
2223
import re
2324
from email.parser import Parser
2425
from http import HTTPStatus
2526
from typing import Any, Dict, List, Optional, Union
2627
from unittest.mock import Mock
2728

28-
import pkg_resources
29-
3029
from twisted.internet.interfaces import IReactorTCP
3130
from twisted.internet.testing import MemoryReactor
3231

@@ -59,11 +58,12 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
5958
config = self.default_config()
6059

6160
# Email config.
61+
templates = (
62+
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
63+
)
6264
config["email"] = {
6365
"enable_notifs": False,
64-
"template_dir": os.path.abspath(
65-
pkg_resources.resource_filename("synapse", "res/templates")
66-
),
66+
"template_dir": os.path.abspath(str(templates)),
6767
"smtp_host": "127.0.0.1",
6868
"smtp_port": 20,
6969
"require_transport_security": False,
@@ -798,11 +798,12 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
798798
config = self.default_config()
799799

800800
# Email config.
801+
templates = (
802+
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
803+
)
801804
config["email"] = {
802805
"enable_notifs": False,
803-
"template_dir": os.path.abspath(
804-
pkg_resources.resource_filename("synapse", "res/templates")
805-
),
806+
"template_dir": os.path.abspath(str(templates)),
806807
"smtp_host": "127.0.0.1",
807808
"smtp_port": 20,
808809
"require_transport_security": False,

tests/rest/client/test_register.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
#
2121
#
2222
import datetime
23+
import importlib.resources as importlib_resources
2324
import os
2425
from typing import Any, Dict, List, Tuple
2526
from unittest.mock import AsyncMock
2627

27-
import pkg_resources
28-
2928
from twisted.internet.testing import MemoryReactor
3029

3130
import synapse.rest.admin
@@ -981,11 +980,12 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
981980

982981
# Email config.
983982

983+
templates = (
984+
importlib_resources.files("synapse").joinpath("res").joinpath("templates")
985+
)
984986
config["email"] = {
985987
"enable_notifs": True,
986-
"template_dir": os.path.abspath(
987-
pkg_resources.resource_filename("synapse", "res/templates")
988-
),
988+
"template_dir": os.path.abspath(str(templates)),
989989
"expiry_template_html": "notice_expiry.html",
990990
"expiry_template_text": "notice_expiry.txt",
991991
"notif_template_html": "notif_mail.html",

0 commit comments

Comments
 (0)