Skip to content

Commit 742fd71

Browse files
committed
pkg_resources -> importlib.resources
1 parent 1a4c61d commit 742fd71

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

git_code_debt/generate.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import collections
55
import contextlib
6+
import importlib.resources
67
import itertools
78
import multiprocessing.pool
89
import os.path
@@ -15,8 +16,6 @@
1516
from typing import Callable
1617
from typing import TypeVar
1718

18-
import pkg_resources
19-
2019
from git_code_debt import options
2120
from git_code_debt.discovery import get_metric_parsers_from_args
2221
from git_code_debt.file_diff_stat import FileDiffStat
@@ -163,13 +162,10 @@ def load_data(
163162

164163
def create_schema(db: sqlite3.Connection) -> None:
165164
"""Creates the database schema."""
166-
schema_dir = pkg_resources.resource_filename('git_code_debt', 'schema')
167-
schema_files = os.listdir(schema_dir)
165+
schema_dir = importlib.resources.files('git_code_debt.schema')
168166

169-
for sql_file in schema_files:
170-
resource_filename = os.path.join(schema_dir, sql_file)
171-
with open(resource_filename) as resource:
172-
db.executescript(resource.read())
167+
for sql_file in schema_dir.iterdir():
168+
db.executescript(sql_file.read_text())
173169

174170

175171
def get_metrics_info(

git_code_debt/server/app.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from __future__ import annotations
22

33
import argparse
4+
import importlib.resources
45
import os.path
5-
import shutil
66
import sqlite3
77
from collections.abc import Sequence
88
from typing import NoReturn
99

1010
import flask
11-
import pkg_resources
1211

1312
from git_code_debt.server.metric_config import Config
1413
from git_code_debt.server.servlets.changes import changes
@@ -53,12 +52,12 @@ def create_metric_config_if_not_exists() -> None:
5352
return
5453

5554
print('WARNING: no metric_config.yaml detected. Creating sample config!')
56-
shutil.copyfile(
57-
pkg_resources.resource_filename(
58-
'git_code_debt.server', 'metric_config.sample.yaml',
59-
),
60-
'metric_config.yaml',
55+
bts = importlib.resources.read_binary(
56+
'git_code_debt.server',
57+
'metric_config.sample.yaml',
6158
)
59+
with open('metric_config.yaml', 'wb') as f:
60+
f.write(bts)
6261

6362

6463
def main(argv: Sequence[str] | None = None) -> NoReturn:

git_code_debt/server/render_mako.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
from __future__ import annotations
22

3+
import importlib.resources
34
from typing import Any
45

56
import mako.lookup
6-
import pkg_resources
7+
from mako.template import Template
78

89

9-
template_lookup = mako.lookup.TemplateLookup(
10-
directories=[
11-
pkg_resources.resource_filename('git_code_debt.server', 'templates'),
12-
],
13-
default_filters=['html_escape'],
14-
imports=['from mako.filters import html_escape'],
15-
)
10+
class ImportlibResourcesLookup(mako.lookup.TemplateCollection):
11+
def __init__(self, mod: str) -> None:
12+
self.mod = mod
13+
self._cache: dict[str, Template] = {}
14+
15+
def get_template(
16+
self,
17+
uri: str,
18+
relativeto: str | None = None,
19+
) -> Template:
20+
if relativeto is not None:
21+
raise NotImplementedError(f'{relativeto=}')
22+
23+
try:
24+
return self._cache[uri]
25+
except KeyError:
26+
pth = importlib.resources.files(self.mod).joinpath(uri)
27+
with importlib.resources.as_file(pth) as pth:
28+
return Template(
29+
filename=str(pth),
30+
lookup=self,
31+
default_filters=['html_escape'],
32+
imports=['from mako.filters import html_escape'],
33+
)
34+
35+
36+
template_lookup = ImportlibResourcesLookup('git_code_debt.server.templates')
1637

1738

1839
def render_template(template_name: str, **env: Any) -> str:

0 commit comments

Comments
 (0)