Skip to content

Commit 2cfee97

Browse files
authored
Merge pull request #287 from asottile/no-pkg-resources
pkg_resources -> importlib.resources
2 parents 1a4c61d + 4ac2d06 commit 2cfee97

File tree

7 files changed

+46
-30
lines changed

7 files changed

+46
-30
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ jobs:
1010
main-latest-git:
1111
uses: asottile/workflows/.github/workflows/[email protected]
1212
with:
13-
env: '["py39"]'
13+
env: '["py310"]'
1414
main:
1515
uses: asottile/workflows/.github/workflows/[email protected]
1616
with:
17-
env: '["py39", "py310", "py311"]'
17+
env: '["py310", "py311", "3.12"]'

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
rev: v3.15.0
1818
hooks:
1919
- id: reorder-python-imports
20-
args: [--py39-plus, --add-import, 'from __future__ import annotations']
20+
args: [--py310-plus, --add-import, 'from __future__ import annotations']
2121
- repo: https://github.com/asottile/add-trailing-comma
2222
rev: v3.2.0
2323
hooks:
@@ -26,7 +26,7 @@ repos:
2626
rev: v3.20.0
2727
hooks:
2828
- id: pyupgrade
29-
args: [--py39-plus]
29+
args: [--py310-plus]
3030
- repo: https://github.com/hhatto/autopep8
3131
rev: v2.3.2
3232
hooks:

git_code_debt/generate.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
import argparse
44
import collections
55
import contextlib
6+
import importlib.resources
67
import itertools
78
import multiprocessing.pool
89
import os.path
910
import sqlite3
1011
from collections import Counter
12+
from collections.abc import Callable
1113
from collections.abc import Generator
1214
from collections.abc import Iterable
1315
from collections.abc import Sequence
1416
from re import Pattern
15-
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:

git_code_debt/util/discovery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import inspect
44
import pkgutil
5+
from collections.abc import Callable
56
from types import ModuleType
67
from typing import Any
7-
from typing import Callable
88

99

1010
def discover(

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ install_requires =
2424
mako
2525
markdown-code-blocks
2626
pyyaml
27-
python_requires = >=3.9
27+
python_requires = >=3.10
2828

2929
[options.packages.find]
3030
exclude =

0 commit comments

Comments
 (0)