Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
main-latest-git:
uses: asottile/workflows/.github/workflows/[email protected]
with:
env: '["py39"]'
env: '["py310"]'
main:
uses: asottile/workflows/.github/workflows/[email protected]
with:
env: '["py39", "py310", "py311"]'
env: '["py310", "py311", "3.12"]'
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
rev: v3.15.0
hooks:
- id: reorder-python-imports
args: [--py39-plus, --add-import, 'from __future__ import annotations']
args: [--py310-plus, --add-import, 'from __future__ import annotations']
- repo: https://github.com/asottile/add-trailing-comma
rev: v3.2.0
hooks:
Expand All @@ -26,7 +26,7 @@ repos:
rev: v3.20.0
hooks:
- id: pyupgrade
args: [--py39-plus]
args: [--py310-plus]
- repo: https://github.com/hhatto/autopep8
rev: v2.3.2
hooks:
Expand Down
14 changes: 5 additions & 9 deletions git_code_debt/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
import argparse
import collections
import contextlib
import importlib.resources
import itertools
import multiprocessing.pool
import os.path
import sqlite3
from collections import Counter
from collections.abc import Callable
from collections.abc import Generator
from collections.abc import Iterable
from collections.abc import Sequence
from re import Pattern
from typing import Callable
from typing import TypeVar

import pkg_resources

from git_code_debt import options
from git_code_debt.discovery import get_metric_parsers_from_args
from git_code_debt.file_diff_stat import FileDiffStat
Expand Down Expand Up @@ -163,13 +162,10 @@ def load_data(

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

for sql_file in schema_files:
resource_filename = os.path.join(schema_dir, sql_file)
with open(resource_filename) as resource:
db.executescript(resource.read())
for sql_file in schema_dir.iterdir():
db.executescript(sql_file.read_text())


def get_metrics_info(
Expand Down
13 changes: 6 additions & 7 deletions git_code_debt/server/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from __future__ import annotations

import argparse
import importlib.resources
import os.path
import shutil
import sqlite3
from collections.abc import Sequence
from typing import NoReturn

import flask
import pkg_resources

from git_code_debt.server.metric_config import Config
from git_code_debt.server.servlets.changes import changes
Expand Down Expand Up @@ -53,12 +52,12 @@ def create_metric_config_if_not_exists() -> None:
return

print('WARNING: no metric_config.yaml detected. Creating sample config!')
shutil.copyfile(
pkg_resources.resource_filename(
'git_code_debt.server', 'metric_config.sample.yaml',
),
'metric_config.yaml',
bts = importlib.resources.read_binary(
'git_code_debt.server',
'metric_config.sample.yaml',
)
with open('metric_config.yaml', 'wb') as f:
f.write(bts)


def main(argv: Sequence[str] | None = None) -> NoReturn:
Expand Down
37 changes: 29 additions & 8 deletions git_code_debt/server/render_mako.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
from __future__ import annotations

import importlib.resources
from typing import Any

import mako.lookup
import pkg_resources
from mako.template import Template


template_lookup = mako.lookup.TemplateLookup(
directories=[
pkg_resources.resource_filename('git_code_debt.server', 'templates'),
],
default_filters=['html_escape'],
imports=['from mako.filters import html_escape'],
)
class ImportlibResourcesLookup(mako.lookup.TemplateCollection):
def __init__(self, mod: str) -> None:
self.mod = mod
self._cache: dict[str, Template] = {}

def get_template(
self,
uri: str,
relativeto: str | None = None,
) -> Template:
if relativeto is not None:
raise NotImplementedError(f'{relativeto=}')

try:
return self._cache[uri]
except KeyError:
pth = importlib.resources.files(self.mod).joinpath(uri)
with importlib.resources.as_file(pth) as pth:
return Template(
filename=str(pth),
lookup=self,
default_filters=['html_escape'],
imports=['from mako.filters import html_escape'],
)


template_lookup = ImportlibResourcesLookup('git_code_debt.server.templates')


def render_template(template_name: str, **env: Any) -> str:
Expand Down
2 changes: 1 addition & 1 deletion git_code_debt/util/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import inspect
import pkgutil
from collections.abc import Callable
from types import ModuleType
from typing import Any
from typing import Callable


def discover(
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ install_requires =
mako
markdown-code-blocks
pyyaml
python_requires = >=3.9
python_requires = >=3.10

[options.packages.find]
exclude =
Expand Down