Skip to content

Commit d04ced1

Browse files
authored
Modernise based off of new version of flit_core (#12)
1 parent 9782c4e commit d04ced1

2 files changed

Lines changed: 95 additions & 33 deletions

File tree

pyproject.toml

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ build-backend = "flit_core.buildapi"
88
name = "socketmap-sql"
99
authors = [{ name = "Keith Gaughan", email = "k@stereochro.me" }]
1010
readme = "README.rst"
11-
requires-python = ">=3.8"
11+
requires-python = ">=3.11"
12+
license = "MIT"
13+
license-files = ["LICENSE"]
1214
classifiers = [
1315
"Development Status :: 4 - Beta",
1416
"Intended Audience :: System Administrators",
15-
"License :: OSI Approved :: MIT License",
1617
"Operating System :: POSIX",
1718
"Programming Language :: Python :: 3",
1819
"Programming Language :: Python :: 3 :: Only",
@@ -30,8 +31,74 @@ socketmap-sql = "socketmapsql:main"
3031
[tool.flit.module]
3132
name = "socketmapsql"
3233

33-
[tool.uv]
34-
dev-dependencies = [
35-
"mypy>=1.10.1",
34+
[dependency-groups]
35+
dev = [
36+
"mypy>=1.10.1",
3637
]
37-
managed = true
38+
39+
[tool.mypy]
40+
ignore_missing_imports = true
41+
42+
[tool.black]
43+
line-length = 120
44+
45+
[tool.ruff]
46+
target-version = "py311"
47+
line-length = 120
48+
49+
[tool.ruff.lint]
50+
select = [
51+
"A",
52+
# "ANN",
53+
"ARG",
54+
"B",
55+
"C",
56+
"DTZ",
57+
"E",
58+
"EM",
59+
"EXE",
60+
"F",
61+
"FBT",
62+
"FLY",
63+
"FURB",
64+
"G",
65+
"I",
66+
"ICN",
67+
"ISC002",
68+
"LOG",
69+
"N",
70+
"PERF",
71+
"PIE",
72+
"PLC",
73+
"PLE",
74+
"PLR",
75+
"PLW",
76+
# "PT",
77+
"PYI",
78+
"Q",
79+
"RET",
80+
"RSE",
81+
"RUF",
82+
"S",
83+
"SIM",
84+
"SLOT",
85+
"T",
86+
"TC",
87+
"TID",
88+
"TRY",
89+
"UP",
90+
"W",
91+
"YTT",
92+
]
93+
ignore = [
94+
"EM102", # Exception must not use an f-string literal, assign to variable first
95+
"PLR0913", # Too many arguments in function definition
96+
"T201", # `print` found
97+
"TRY003", # Avoid specifying long messages outside the exception class
98+
]
99+
100+
[tool.ruff.lint.isort]
101+
force-sort-within-sections = true
102+
103+
[tool.ruff.lint.flake8-tidy-imports]
104+
ban-relative-imports = "all"

socketmapsql.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import sys
1717
import typing as t
1818

19+
__version__: str = "0.2.0"
1920

20-
__version__ = "0.2.0"
2121

22-
23-
Transform = t.Callable[[str, t.Mapping[str, str]], t.List[str]]
22+
Transform = t.Callable[[str, t.Mapping[str, str]], list[str]]
2423

2524

2625
@dataclasses.dataclass
@@ -30,9 +29,9 @@ class TableCfg:
3029

3130

3231
class Cfg(t.TypedDict):
33-
database: t.Dict[str, str]
34-
misc: t.Dict[str, str]
35-
tables: t.Dict[str, TableCfg]
32+
database: dict[str, str]
33+
misc: dict[str, str]
34+
tables: dict[str, TableCfg]
3635

3736

3837
FUNC_REF_PATTERN = re.compile(
@@ -43,11 +42,11 @@ class Cfg(t.TypedDict):
4342
(?P<object>[a-z_][a-z0-9_]*(?:\.[a-z_][a-z0-9_]*)*)
4443
$
4544
""",
46-
re.I | re.X,
45+
re.IGNORECASE | re.VERBOSE,
4746
)
4847

4948

50-
def match(name: str) -> t.Tuple[str, str]:
49+
def match(name: str) -> tuple[str, str]:
5150
matches = FUNC_REF_PATTERN.match(name)
5251
if not matches:
5352
raise ValueError(f"Malformed callable '{name}'")
@@ -65,18 +64,16 @@ class MalformedNetstringError(Exception):
6564
pass
6665

6766

68-
def read_netstring(fp: t.IO[str]) -> t.Optional[str]:
69-
"""
70-
Reads a single netstring.
71-
"""
67+
def read_netstring(fp: t.IO[str]) -> str | None:
68+
"""Reads a single netstring."""
7269
ns = ""
7370
while True:
7471
c = fp.read(1)
7572
if c == "":
7673
return None
7774
if c == ":":
7875
break
79-
if len(ns) > 10:
76+
if len(ns) > 10: # noqa: PLR2004
8077
raise MalformedNetstringError
8178
if c == "0" and ns == "":
8279
# We can't allow leading zeros.
@@ -98,7 +95,7 @@ def read_netstring(fp: t.IO[str]) -> t.Optional[str]:
9895
return result
9996

10097

101-
def write_netstring(fp: t.IO[str], response: str):
98+
def write_netstring(fp: t.IO[str], response: str) -> None:
10299
fp.write(f"{len(response)}:{response},")
103100
fp.flush()
104101

@@ -110,7 +107,7 @@ def process_local(local_part: str, cfg: t.Mapping[str, str]) -> str:
110107
return local_part.lower()
111108

112109

113-
def split(arg: str, cfg: t.Mapping[str, str]) -> t.List[str]:
110+
def split(arg: str, cfg: t.Mapping[str, str]) -> list[str]:
114111
parts = arg.split("@", 1)
115112
parts[0] = process_local(parts[0], cfg)
116113
parts[1] = parts[1].lower()
@@ -119,10 +116,10 @@ def split(arg: str, cfg: t.Mapping[str, str]) -> t.List[str]:
119116

120117
def parse_config(fp: t.IO[str]) -> Cfg:
121118
transforms = {
122-
"all": lambda arg, cfg: [arg],
123-
"lowercase": lambda arg, cfg: [arg.lower()],
119+
"all": lambda arg, _: [arg],
120+
"lowercase": lambda arg, _: [arg.lower()],
124121
"local": lambda arg, cfg: [process_local(arg.split("@", 1)[0], cfg)],
125-
"domain": lambda arg, cfg: [arg.split("@", 1)[1].lower()],
122+
"domain": lambda arg, _: [arg.split("@", 1)[1].lower()],
126123
"split": split,
127124
}
128125

@@ -156,7 +153,7 @@ def parse_config(fp: t.IO[str]) -> Cfg:
156153
)
157154

158155

159-
def get_int(cfg: t.Mapping[str, str], key: str) -> t.Optional[int]:
156+
def get_int(cfg: t.Mapping[str, str], key: str) -> int | None:
160157
return int(cfg[key]) if key in cfg else None
161158

162159

@@ -167,7 +164,7 @@ def serve_client(
167164
timeout: int,
168165
tables: t.Mapping[str, TableCfg],
169166
cfg: t.Mapping[str, str],
170-
):
167+
) -> None:
171168
max_requests = get_int(cfg, "max_requests")
172169
try:
173170
while True:
@@ -202,17 +199,15 @@ def serve_client(
202199
if result is None:
203200
write_netstring(fh_out, "NOTFOUND ")
204201
else:
205-
write_netstring(fh_out, f"OK {str(result[0])}")
202+
write_netstring(fh_out, f"OK {result[0]!s}")
206203
except MalformedNetstringError:
207204
write_netstring(fh_out, "PERM malformed netstring")
208205
except Exception as exc:
209-
write_netstring(fh_out, f"PERM {str(exc)}")
206+
write_netstring(fh_out, f"PERM {exc!s}")
210207

211208

212-
def connect(settings: t.Dict[str, str]):
213-
"""
214-
Connect to a database.
215-
"""
209+
def connect(settings: dict[str, str]):
210+
"""Connect to a database."""
216211
driver = importlib.import_module(settings.pop("driver", "sqlite3"))
217212
return driver.connect(**settings)
218213

@@ -239,7 +234,7 @@ def make_parser() -> argparse.ArgumentParser:
239234
return parser
240235

241236

242-
def main():
237+
def main() -> int:
243238
args = make_parser().parse_args()
244239

245240
with contextlib.closing(args.config):

0 commit comments

Comments
 (0)