Skip to content

Commit b06d7ce

Browse files
committed
Add linting and formatting
1 parent e1a7592 commit b06d7ce

File tree

9 files changed

+126
-26
lines changed

9 files changed

+126
-26
lines changed

.github/workflows/lint.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Lint
2+
3+
on: [push, pull_request, workflow_dispatch]
4+
5+
env:
6+
FORCE_COLOR: 1
7+
8+
jobs:
9+
lint:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
persist-credentials: false
16+
- uses: actions/setup-python@v5
17+
with:
18+
python-version: "3.x"
19+
- uses: tox-dev/action-pre-commit-uv@v1
20+
21+
mypy:
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
with:
27+
persist-credentials: false
28+
- uses: actions/setup-python@v5
29+
with:
30+
python-version: "3.x"
31+
- name: Install uv
32+
uses: hynek/setup-cached-uv@v2
33+
- name: Mypy
34+
run: uvx --with tox-uv tox -e mypy

.github/workflows/update.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/setup-python@v5
1616
with:
1717
python-version: "3.x"
18-
- uses: astral-sh/setup-uv@v4
18+
- uses: astral-sh/setup-uv@v5
1919
- uses: actions/checkout@v4
2020
- run: sudo apt-get install -y gettext
2121
- run: uv run generate.py # generates "index.html"

.pre-commit-config.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.8.4
4+
hooks:
5+
- id: ruff
6+
args: [--exit-non-zero-on-fix]
7+
8+
- repo: https://github.com/psf/black-pre-commit-mirror
9+
rev: 24.10.0
10+
hooks:
11+
- id: black
12+
13+
- repo: https://github.com/pre-commit/pre-commit-hooks
14+
rev: v5.0.0
15+
hooks:
16+
- id: check-added-large-files
17+
- id: check-case-conflict
18+
- id: check-merge-conflict
19+
- id: check-yaml
20+
- id: debug-statements
21+
- id: end-of-file-fixer
22+
- id: forbid-submodules
23+
- id: trailing-whitespace
24+
25+
- repo: https://github.com/python-jsonschema/check-jsonschema
26+
rev: 0.30.0
27+
hooks:
28+
- id: check-github-workflows
29+
30+
- repo: https://github.com/rhysd/actionlint
31+
rev: v1.7.5
32+
hooks:
33+
- id: actionlint
34+
35+
- repo: meta
36+
hooks:
37+
- id: check-hooks-apply
38+
- id: check-useless-excludes
39+
40+
ci:
41+
autoupdate_schedule: quarterly

completion.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@cache
1313
def branches_from_devguide(devguide_dir: Path) -> list[str]:
14-
p = devguide_dir.joinpath('include/release-cycle.json')
14+
p = devguide_dir.joinpath("include/release-cycle.json")
1515
data = json.loads(p.read_text())
1616
return [
1717
branch for branch in data if data[branch]["status"] in ("bugfix", "security")
@@ -20,18 +20,24 @@ def branches_from_devguide(devguide_dir: Path) -> list[str]:
2020

2121
def get_completion(clones_dir: str, repo: str) -> tuple[float, int]:
2222
clone_path = Path(clones_dir, repo)
23-
for branch in branches_from_devguide(Path(clones_dir, 'devguide')) + ['master']:
23+
for branch in branches_from_devguide(Path(clones_dir, "devguide")) + ["master"]:
2424
try:
25-
git.Repo.clone_from(f'https://github.com/{repo}.git', clone_path, branch=branch)
25+
git.Repo.clone_from(
26+
f"https://github.com/{repo}.git", clone_path, branch=branch
27+
)
2628
except git.GitCommandError:
27-
print(f'failed to clone {repo} {branch}')
29+
print(f"failed to clone {repo} {branch}")
2830
translators_number = 0
2931
continue
3032
else:
3133
translators_number = translators.get_number(clone_path)
3234
break
3335
with TemporaryDirectory() as tmpdir:
3436
completion = potodo.merge_and_scan_path(
35-
clone_path, pot_path=Path(clones_dir, 'cpython/Doc/build/gettext'), merge_path=Path(tmpdir), hide_reserved=False, api_url=''
37+
clone_path,
38+
pot_path=Path(clones_dir, "cpython/Doc/build/gettext"),
39+
merge_path=Path(tmpdir),
40+
hide_reserved=False,
41+
api_url="",
3642
).completion
3743
return completion, translators_number

generate.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,22 @@
2525
generation_time = datetime.now(timezone.utc)
2626

2727
with TemporaryDirectory() as clones_dir:
28-
Repo.clone_from(f'https://github.com/python/devguide.git', devguide_dir := Path(clones_dir, 'devguide'), depth=1)
28+
Repo.clone_from(
29+
"https://github.com/python/devguide.git",
30+
devguide_dir := Path(clones_dir, "devguide"),
31+
depth=1,
32+
)
2933
latest_branch = branches_from_devguide(devguide_dir)[0]
3034
Repo.clone_from(
31-
f'https://github.com/python/cpython.git', Path(clones_dir, 'cpython'), depth=1, branch=latest_branch
35+
"https://github.com/python/cpython.git",
36+
Path(clones_dir, "cpython"),
37+
depth=1,
38+
branch=latest_branch,
39+
)
40+
subprocess.run(["make", "-C", Path(clones_dir, "cpython/Doc"), "venv"], check=True)
41+
subprocess.run(
42+
["make", "-C", Path(clones_dir, "cpython/Doc"), "gettext"], check=True
3243
)
33-
subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'venv'], check=True)
34-
subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'gettext'], check=True)
3544
switcher_languages = list(switcher.get_languages())
3645
for language, repo in repositories.get_languages_and_repos(devguide_dir):
3746
if repo:
@@ -78,7 +87,7 @@
7887
<a href="https://github.com/{{ repo }}" target="_blank">
7988
{{ language }}
8089
</a>
81-
</td>
90+
</td>
8291
<td data-label="build">
8392
{% if in_switcher %}
8493
<a href="https://docs.python.org/{{ language }}/">in switcher</a>
@@ -111,7 +120,9 @@
111120
"""
112121
)
113122

114-
output = template.render(completion_progress=completion_progress, generation_time=generation_time)
123+
output = template.render(
124+
completion_progress=completion_progress, generation_time=generation_time
125+
)
115126

116127
with open("index.html", "w") as file:
117128
file.write(output)

repositories.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import tempfile
21
import pathlib
32
import re
43
from typing import Generator, Optional
@@ -7,14 +6,18 @@
76
from docutils.nodes import table, row
87

98

10-
def get_languages_and_repos(devguide_dir: pathlib.Path) -> Generator[tuple[str, Optional[str]], None, None]:
11-
translating = devguide_dir.joinpath('documentation/translating.rst').read_text()
9+
def get_languages_and_repos(
10+
devguide_dir: pathlib.Path,
11+
) -> Generator[tuple[str, Optional[str]], None, None]:
12+
translating = devguide_dir.joinpath("documentation/translating.rst").read_text()
1213
doctree = core.publish_doctree(translating)
1314

1415
for node in doctree.traverse(table):
1516
for row_node in node.traverse(row)[1:]:
1617
language = row_node[0].astext()
1718
repo = row_node[2].astext()
18-
language_code = re.match(r'.* \((.*)\)', language).group(1).lower().replace('_', '-')
19-
repo_match = re.match(':github:`GitHub <(.*)>`', repo)
19+
language_code = (
20+
re.match(r".* \((.*)\)", language).group(1).lower().replace("_", "-")
21+
)
22+
repo_match = re.match(":github:`GitHub <(.*)>`", repo)
2023
yield language_code, repo_match and repo_match.group(1)

switcher.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
import tomllib
9-
from collections import defaultdict
109
from typing import Generator
1110

1211
import requests

translators.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@ def get_number(path: Path) -> int:
1010
from_git_history = get_number_from_git_history(path)
1111
return max(from_headers, from_git_history)
1212

13+
1314
def get_number_from_git_history(path: Path) -> int:
14-
return len(Repo(path).git.shortlog('-s', 'HEAD').splitlines())
15+
return len(Repo(path).git.shortlog("-s", "HEAD").splitlines())
16+
1517

1618
def yield_from_headers(path: Path) -> Generator[str, None, None]:
17-
for file in path.rglob('*.po'):
19+
for file in path.rglob("*.po"):
1820
try:
1921
header = pofile(file).header.splitlines()
2022
except IOError:
2123
continue
22-
if 'Translators:' not in header:
24+
if "Translators:" not in header:
2325
continue
24-
for translator_record in header[header.index('Translators:') + 1:]:
26+
for translator_record in header[header.index("Translators:") + 1 :]:
2527
try:
26-
translator, _year = translator_record.split(', ')
28+
translator, _year = translator_record.split(", ")
2729
except ValueError:
2830
yield translator_record
2931
else:

visitors.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77

88

99
def get_number_of_visitors(language: str) -> int:
10-
param = urllib.parse.urlencode({'filters': f'[["contains","event:page",["/{language}/"]]]'})
11-
r = requests.get(f'https://plausible.io/docs.python.org/export?{param}', timeout=10)
12-
with zipfile.ZipFile(io.BytesIO(r.content), 'r') as z, z.open('visitors.csv') as csv_file:
10+
param = urllib.parse.urlencode(
11+
{"filters": f'[["contains","event:page",["/{language}/"]]]'}
12+
)
13+
r = requests.get(f"https://plausible.io/docs.python.org/export?{param}", timeout=10)
14+
with zipfile.ZipFile(io.BytesIO(r.content), "r") as z, z.open(
15+
"visitors.csv"
16+
) as csv_file:
1317
csv_reader = csv.DictReader(io.TextIOWrapper(csv_file))
1418
return sum(int(row["visitors"]) for row in csv_reader)

0 commit comments

Comments
 (0)