Skip to content

Commit 82613de

Browse files
authored
Merge pull request #26 from tekktrik/dev/ruff
Switch to ruff
2 parents e754120 + 8596b3f commit 82613de

8 files changed

+54
-43
lines changed

.pre-commit-config.yaml

+7-17
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@
22
# SPDX-License-Identifier: MIT
33

44
repos:
5-
- repo: https://github.com/python/black
6-
rev: 23.12.1
7-
hooks:
8-
- name: Run black
9-
id: black
10-
- repo: https://github.com/pycqa/isort
11-
rev: 5.13.2
12-
hooks:
13-
- id: isort
14-
name: Sort Python imports
15-
args: ["--profile", "black", "--filter-files"]
165
- repo: https://github.com/fsfe/reuse-tool
176
rev: v2.1.0
187
hooks:
@@ -27,10 +16,11 @@ repos:
2716
id: end-of-file-fixer
2817
- name: Trim trailing whitespace
2918
id: trailing-whitespace
30-
- repo: local
19+
- repo: https://github.com/astral-sh/ruff-pre-commit
20+
rev: v0.1.14
3121
hooks:
32-
- name: Run pylint
33-
id: pylint
34-
entry: pylint
35-
language: system
36-
types: [python]
22+
- id: ruff
23+
name: Lint via ruff
24+
args: [--fix]
25+
- id: ruff-format
26+
name: Format via ruff

Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: 2024 Alec Delaney
2+
# SPDX-License-Identifier: MIT
3+
4+
.PHONY: localhost
5+
localhost:
6+
flask --app flask_app run
7+
8+
.PHONY: lint
9+
lint:
10+
pre-commit run ruff --all-files
11+
12+
.PHONY: format
13+
format:
14+
pre-commit run ruff-format --all-files
15+
16+
.PHONY: check
17+
check:
18+
pre-commit run --all-files

flask_app/__init__.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# SPDX-FileCopyrightText: 2023 Alec Delaney
22
# SPDX-License-Identifier: MIT
33

4-
"""
5-
Main entry point for the flask application
4+
"""Main entry point for the flask application.
65
76
Author: Alec Delaney
87
"""
@@ -51,24 +50,24 @@
5150

5251
@app.route("/")
5352
def index() -> str:
54-
"""Route for index (landing page)"""
53+
"""Route for index (landing page)."""
5554
return render_template("index.html")
5655

5756

5857
@app.route("/set-menorah")
5958
def menorah_settings() -> Response:
60-
"""Route for shortcut to menorah settings page"""
59+
"""Route for shortcut to menorah settings page."""
6160
return redirect("/projects/menorah/settings")
6261

6362

6463
@app.route("/projects/menorah/settings", methods=["GET", "POST"])
6564
@limiter.limit("10/second", key_func=lambda: "menorah-settings")
6665
def project_menorah_settings() -> str:
67-
"""Route for creating menorah settings file"""
66+
"""Route for creating menorah settings file."""
6867
input_form = MenorahSetupForm()
6968
if input_form.validate_on_submit():
7069
zipcode = input_form.data["zipcode"]
71-
with open("assets/settings.json", mode="r", encoding="utf-8") as template_file:
70+
with open("assets/settings.json", encoding="utf-8") as template_file:
7271
template_text = template_file.read()
7372
template = jinja2.Template(template_text)
7473
rendered_temp = template.render(zipcode=zipcode)
@@ -83,7 +82,7 @@ def project_menorah_settings() -> str:
8382

8483
@app.route("/recent", methods=["GET"])
8584
def recent() -> str:
86-
"""Route for recent GitHub activity"""
85+
"""Route for recent GitHub activity."""
8786
with open("assets/contrib/recent.json", encoding="utf-8") as respfile:
8887
contents = json.load(respfile)
8988
contributions, repos = contents["contributionsCollection"], contents["repositories"]
@@ -104,11 +103,11 @@ def recent() -> str:
104103

105104
@app.route("/about", methods=["GET"])
106105
def about() -> str:
107-
"""Route for about me page"""
106+
"""Route for about me page."""
108107
jobs_path = pathlib.Path("assets/about/jobs")
109108
jobs = []
110109
for job_path in jobs_path.glob("*.json"):
111-
with open(job_path, mode="r", encoding="utf-8") as jobfile:
110+
with open(job_path, encoding="utf-8") as jobfile:
112111
job_obj = json.load(jobfile)
113112
if job_obj["endDate"] is None:
114113
job_obj["endDate"] = "current"
@@ -120,7 +119,7 @@ def about() -> str:
120119
education_paths = pathlib.Path("assets/about/education")
121120
educations = []
122121
for education_path in education_paths.glob("*.json"):
123-
with open(education_path, mode="r", encoding="utf-8") as edufile:
122+
with open(education_path, encoding="utf-8") as edufile:
124123
edu_obj = json.load(edufile)
125124
if edu_obj["endYear"] is None:
126125
edu_obj["endYear"] = "current"

flask_app/forms.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# SPDX-FileCopyrightText: 2023 Alec Delaney
22
# SPDX-License-Identifier: MIT
33

4-
"""
5-
WTForms used in the Flask application
4+
"""WTForms used in the Flask application.
65
76
Author: Alec Delaney
87
"""
@@ -13,7 +12,7 @@
1312

1413

1514
class MenorahSetupForm(FlaskForm):
16-
"""Form for menorah information"""
15+
"""Form for menorah information."""
1716

1817
zipcode = StringField("Zip Code", validators=[DataRequired()])
1918
submit = SubmitField("Generate file...")

flask_app/helpers.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# SPDX-FileCopyrightText: 2023 Alec Delaney
22
# SPDX-License-Identifier: MIT
33

4-
"""
5-
Helper functions used by the Flask application
4+
"""Helper functions used by the Flask application.
65
76
Author: Alec Delaney
87
"""
@@ -16,7 +15,7 @@
1615

1716

1817
class JobDict(TypedDict):
19-
"""TypedDict representing job JSON file contents"""
18+
"""TypedDict representing job JSON file contents."""
2019

2120
title: str
2221
employer: str
@@ -29,15 +28,15 @@ class JobDict(TypedDict):
2928

3029

3130
def generate_settings_json(zipcode: str) -> str:
32-
"""Generate a settings file for the CircuiyPythonukiah"""
31+
"""Generate a settings file for the CircuiyPythonukiah."""
3332
return json.dumps({"zipcode": zipcode})
3433

3534

3635
def get_repo_info(token: str) -> tuple[dict[str, Any], dict[str, Any]]:
37-
"""Get repository info from the GraphQL query"""
36+
"""Get repository info from the GraphQL query."""
3837
url = "https://api.github.com/graphql"
3938

40-
with open("assets/graphql_query.txt", mode="r", encoding="utf-8") as queryfile:
39+
with open("assets/graphql_query.txt", encoding="utf-8") as queryfile:
4140
query_param = {"query": queryfile.read()}
4241

4342
resp = requests.post(
@@ -55,13 +54,13 @@ def get_repo_info(token: str) -> tuple[dict[str, Any], dict[str, Any]]:
5554

5655

5756
def sort_jobs_start_date(job: JobDict) -> int:
58-
"""Sort the jobs by start date"""
57+
"""Sort the jobs by start date."""
5958
month_str, year_str = job["startDate"].split("/")
6059
return int(year_str) * 100 + int(month_str)
6160

6261

6362
def consolidate_sorted_jobs(jobs: list[JobDict]) -> list[list[JobDict]]:
64-
"""Consolidate jobs in instances like promotions"""
63+
"""Consolidate jobs in instances like promotions."""
6564
grouped_jobs_dict: dict[str, list[JobDict]] = {}
6665
grouped_jobs_list: list[list[JobDict]] = []
6766

@@ -85,7 +84,8 @@ def consolidate_sorted_jobs(jobs: list[JobDict]) -> list[list[JobDict]]:
8584

8685
# If job was not just newly added and gap is no more than 31 days apart
8786
# then add to existing list
88-
if not newly_added and (start_role - end_role).days <= 31:
87+
duration_days = 31
88+
if not newly_added and (start_role - end_role).days <= duration_days:
8989
grouped_jobs_dict[employer].append(job)
9090

9191
elif not newly_added:
@@ -99,5 +99,5 @@ def consolidate_sorted_jobs(jobs: list[JobDict]) -> list[list[JobDict]]:
9999

100100

101101
def sort_grouped_jobs(jobs_list: list[JobDict]) -> int:
102-
"""Sort the grouped lists of jobs"""
102+
"""Sort the grouped lists of jobs."""
103103
return sort_jobs_start_date(jobs_list[0])

requirements-dev.txt

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
# SPDX-License-Identifier: MIT
33

44
pre-commit~=3.6
5-
pylint~=3.0

ruff.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-FileCopyrightText: 2024 Alec Delaney
2+
# SPDX-License-Identifier: MIT
3+
4+
select = ["D", "PL", "UP", "I"]
5+
ignore = ["D213", "D203"]

scripts/graphql.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
with open("/etc/config.json", encoding="utf-8") as jsonfile:
1515
config = json.load(jsonfile)
1616

17-
with open("assets/graphql_query.txt", mode="r", encoding="utf-8") as queryfile:
17+
with open("assets/graphql_query.txt", encoding="utf-8") as queryfile:
1818
query_param = {"query": queryfile.read()}
1919

2020
resp = requests.post(
@@ -40,7 +40,8 @@
4040
for index, node in enumerate(json_resp["repositories"]["nodes"]):
4141
for _ in range(5):
4242
img_resp = requests.get(node["openGraphImageUrl"], timeout=5)
43-
if img_resp.status_code == 200:
43+
status_okay = 200
44+
if img_resp.status_code == status_okay:
4445
with open(str(card_dir / f"card{index}.png"), mode="wb") as imgfile:
4546
for data_chunk in img_resp:
4647
imgfile.write(data_chunk)

0 commit comments

Comments
 (0)