Skip to content
Open
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
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ repos:
args: ["--py310-plus"]

- repo: "https://github.com/sirosen/slyp"
rev: "0.8.2"
rev: "0.9.0"
hooks:
- id: "slyp"

- repo: "https://github.com/psf/black-pre-commit-mirror"
rev: "25.12.0"
rev: "26.5.1"
hooks:
- id: "black"

- repo: "https://github.com/pycqa/isort"
rev: "7.0.0"
rev: "9.0.0a3"
hooks:
- id: "isort"

Expand All @@ -55,18 +55,18 @@ repos:
- "flake8-bugbear==25.11.29"

- repo: "https://github.com/editorconfig-checker/editorconfig-checker"
rev: "v3.6.0"
rev: "v3.8.0"
hooks:
- id: "editorconfig-checker"

- repo: "https://github.com/python-jsonschema/check-jsonschema"
rev: "0.36.0"
rev: "0.37.4"
hooks:
- id: "check-dependabot"
- id: "check-readthedocs"

- repo: "https://github.com/rhysd/actionlint"
rev: "v1.7.9"
rev: "v1.7.12"
hooks:
- id: "actionlint"
additional_dependencies:
Expand All @@ -86,6 +86,6 @@ repos:
]

- repo: "https://github.com/zizmorcore/zizmor-pre-commit"
rev: "v1.18.0"
rev: "v1.26.1"
hooks:
- id: "zizmor"
1 change: 0 additions & 1 deletion docs/ruff/assets/F401/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import boto3
import requests


# Hidden from rendering in the docs.
#
# This list does not include 'sqliteimport', above,
Expand Down
30 changes: 10 additions & 20 deletions src/sqliteimport/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def get_tables(self) -> list[str]:
def initialize_database(self) -> None:
"""Create database tables and insert basic information about the database."""

self.connection.executescript(
"""
self.connection.executescript("""
CREATE TABLE sqliteimport (
field TEXT,
value TEXT
Expand All @@ -67,8 +66,7 @@ def initialize_database(self) -> None:
magic_number INTEGER,
python_identifier TEXT
);
"""
)
""")

@staticmethod
def get_database_path(database: sqlite3.Connection) -> str:
Expand All @@ -84,16 +82,14 @@ def get_database_path(database: sqlite3.Connection) -> str:
def get_magic_numbers(self) -> dict[int, str]:
"""Get the magic numbers of the already-compiled bytecodes in the database."""

magic_numbers = self.connection.execute(
"""
magic_numbers = self.connection.execute("""
SELECT
magic_number,
python_identifier
FROM
magic_numbers
;
"""
).fetchall()
""").fetchall()
return {row[0]: row[1] for row in magic_numbers}

def add_directory(self, directory: pathlib.Path) -> None:
Expand Down Expand Up @@ -154,8 +150,7 @@ def create_bytecode_table(self, magic_number: int) -> None:
"""Create a compiled bytecode table."""

table_name = self.get_bytecode_table_name(magic_number)
self.connection.executescript(
f"""
self.connection.executescript(f"""
CREATE TABLE {table_name}
(
fullname TEXT,
Expand All @@ -165,8 +160,7 @@ def create_bytecode_table(self, magic_number: int) -> None:
);

CREATE INDEX {table_name}_fullname_index ON {table_name} (fullname);
"""
)
""")

def add_bytecode(
self, magic_number: int, fullname: str, path: str, is_package: bool, code: bytes
Expand Down Expand Up @@ -361,8 +355,7 @@ def list_directory(self, path_like: str) -> list[str]:

def iter_source_code(self) -> typing.Generator[tuple[str, str, bool, bytes]]:
cursor = self.connection.cursor()
iterable = cursor.execute(
"""
iterable = cursor.execute("""
SELECT
fullname,
path,
Expand All @@ -371,8 +364,7 @@ def iter_source_code(self) -> typing.Generator[tuple[str, str, bool, bytes]]:
FROM code
WHERE path LIKE '%.py'
;
"""
)
""")
row: tuple[str, str, bool, bytes]
for row in iterable:
fullname, path, is_package, contents = row
Expand All @@ -382,15 +374,13 @@ def iter_package_metadata(self) -> typing.Generator[bytes]:
"""Find and return all METADATA files in `.dist-info/` directories."""

cursor = self.connection.cursor()
iterable = cursor.execute(
"""
iterable = cursor.execute("""
SELECT
contents
FROM code
WHERE path LIKE '%.dist-info/METADATA'
;
"""
)
""")
row: tuple[bytes]
for row in iterable:
contents = row[0]
Expand Down
24 changes: 8 additions & 16 deletions src/sqliteimport/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,18 @@ def describe(database: pathlib.Path) -> None:
type=click.Path(
exists=True, dir_okay=False, file_okay=True, path_type=pathlib.Path
),
help=(
"""
help=("""
The Python code file to inject sqliteimport
and the `--database` of packages into.

The target file WILL NOT be overwritten by default;
it can only be overwritten if it is specified again as the `--output-file`.
"""
),
"""),
)
@click.option(
"--marker",
default=DEFAULT_MARKER,
help=(
f"""
help=(f"""
The marker to search for in the `--target-file`.

The marker must exist in the `--target-file`,
Expand All @@ -200,29 +197,24 @@ def describe(database: pathlib.Path) -> None:
\b
# {DEFAULT_MARKER}
\b
"""
),
"""),
)
@click.option(
"--output-file",
type=click.Path(dir_okay=False, file_okay=True, path_type=pathlib.Path),
help=(
"""
help=("""
The output file to write, containing the code of the `--target-file`
combined with the sqliteimport source code and database of dependencies.
"""
),
"""),
)
@click.option(
"--overwrite",
is_flag=True,
help=(
"""
help=("""
If set, the `--output-file` will be overwritten if it exists.

By default, the `--output-file` will never be overwritten.
"""
),
"""),
)
def inject(
database: pathlib.Path,
Expand Down
12 changes: 4 additions & 8 deletions src/sqliteimport/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@


def generate_prologue(database_path: pathlib.Path) -> str:
header = textwrap.dedent(
"""\
header = textwrap.dedent("""\
# The code in this block was generated by sqliteimport.
# https://github.com/kurtmckee/sqliteimport
# Copyright Kurt McKee <contactme@kurtmckee.org>
# SPDX-License-Identifier: MIT
"""
).rstrip()
""").rstrip()
lines: list[str] = [header]

# Add code variables.
Expand All @@ -39,8 +37,7 @@ def generate_prologue(database_path: pathlib.Path) -> str:
continue
lines.append(line)

wrapper = textwrap.dedent(
"""
wrapper = textwrap.dedent("""
# BEGIN GENERATED CODE BLOCK. DO NOT EDIT!
def __sqliteimport_setup(database: bytes) -> None:
{function_block}
Expand All @@ -49,8 +46,7 @@ def __sqliteimport_setup(database: bytes) -> None:
del __sqliteimport_database
del __sqliteimport_setup
# END GENERATED CODE BLOCK.
"""
)
""")
function_block = textwrap.indent("\n".join(lines), " ")
database = database_path.read_bytes()
return wrapper.format(function_block=function_block, database=database)
Expand Down
Loading