Skip to content

Commit

Permalink
Configure ruff formatting, format project
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Jun 8, 2024
1 parent 7353c32 commit ae24075
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 86 deletions.
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ ruff = "^0.4.8"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
target-version = "py311"
line-length = 100
indent-width = 4

[tool.ruff.format]
preview = true
indent-style = "space"
quote-style = "double"
4 changes: 1 addition & 3 deletions src/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def create_app() -> Flask:
if sys_vars.get_bool("ENABLE_DISCORD_LOGGING"):
logger.LINKROT.addHandler(logger.DiscordHandler())
else:
logger.LINKROT.addHandler(
logger.file_handler("error-linkrot.log", linkrot=True)
)
logger.LINKROT.addHandler(logger.file_handler("error-linkrot.log", linkrot=True))

# Register the API endpoints
api = Api(app)
Expand Down
77 changes: 31 additions & 46 deletions src/core/database/linkrot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from typing import Literal, TypedDict

import requests
Expand Down Expand Up @@ -55,9 +54,7 @@ def __create(data: WebLink) -> Literal[True]:


def __get(uuid: str) -> RottedLinks:
return (
db.session.execute(db.select(RottedLinks).filter_by(id=uuid)).scalars().first()
)
return db.session.execute(db.select(RottedLinks).filter_by(id=uuid)).scalars().first()


def __update(data: RottedLinks) -> Literal[True]:
Expand Down Expand Up @@ -94,20 +91,16 @@ def check_one(uuid: str) -> RotResult:

# A rotten link has been revived
if delete(uuid):
LINKROT.info(
{
"id": link.id,
"url": link.url,
"message": f"Link has been marked to not be dead or a Web Archive reference.",
}
)
weblink.update(
{
"id": link.id,
"is_dead": 0,
"is_web_archive": 0,
}
)
LINKROT.info({
"id": link.id,
"url": link.url,
"message": "Link has been marked to not be dead or a Web Archive reference.",
})
weblink.update({
"id": link.id,
"is_dead": 0,
"is_web_archive": 0,
})

# We could not ping the site, decide the next step
else:
Expand All @@ -123,26 +116,22 @@ def __record_failure(data: WebLink) -> Check:
existing = __get(data.id)
if existing is None:
__create(data)
LINKROT.error(
{
"id": data.id,
"url": data.url,
"message": "Linkrot check failure #1.",
}
)
LINKROT.error({
"id": data.id,
"url": data.url,
"message": "Linkrot check failure #1.",
})
result["times_failed"] = 1
return result

# We have an existing failure record, update the failure count
if (existing.times_failed + 1) < TIMES_FAILED_THRESHOLD:
__update(existing)
LINKROT.error(
{
"id": data.id,
"url": data.url,
"message": f"Linkrot check failure #{existing.times_failed}.",
}
)
LINKROT.error({
"id": data.id,
"url": data.url,
"message": f"Linkrot check failure #{existing.times_failed}.",
})
result["times_failed"] = existing.times_failed
return result

Expand All @@ -153,25 +142,21 @@ def __record_failure(data: WebLink) -> Check:
revised_info["url"] = wb_url
revised_info["is_web_archive"] = 1
result["is_web_archive"] = True
LINKROT.critical(
{
"id": data.id,
"url": data.url,
"message": "Link has been updated to indicate a Web Archive reference.",
}
)
LINKROT.critical({
"id": data.id,
"url": data.url,
"message": "Link has been updated to indicate a Web Archive reference.",
})

# An archive url doesn't exist, mark as a dead link
else:
revised_info["is_dead"] = 1
result["is_dead"] = True
LINKROT.critical(
{
"id": data.id,
"url": data.url,
"message": "Link has been marked as a dead link.",
}
)
LINKROT.critical({
"id": data.id,
"url": data.url,
"message": "Link has been marked as a dead link.",
})

# Update the dead link
weblink.update(revised_info)
Expand Down
36 changes: 15 additions & 21 deletions src/core/database/weblink.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ def create(data: OrderedDict) -> dict:
db.session.add(weblink)
db.session.commit()
db.session.refresh(weblink)
LINKROT.info(
{
"id": entry_id,
"url": weblink.url,
"message": "Link has been added to the webring.",
}
)
LINKROT.info({
"id": entry_id,
"url": weblink.url,
"message": "Link has been added to the webring.",
})
return {"id": entry_id}


Expand All @@ -41,13 +39,11 @@ def delete(uuid: str) -> bool:

db.session.delete(get(uuid))
db.session.commit()
LINKROT.info(
{
"id": uuid,
"url": "N/A",
"message": "Link has been deleted from the webring.",
}
)
LINKROT.info({
"id": uuid,
"url": "N/A",
"message": "Link has been deleted from the webring.",
})
return True


Expand Down Expand Up @@ -93,11 +89,9 @@ def update(data: OrderedDict) -> bool:
synchronize_session="fetch",
)
db.session.commit()
LINKROT.info(
{
"id": data["id"],
"url": "N/A",
"message": f"Link has been updated with the following info: `{data}`",
}
)
LINKROT.info({
"id": data["id"],
"url": "N/A",
"message": f"Link has been updated with the following info: `{data}`",
})
return True
10 changes: 4 additions & 6 deletions src/core/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@


def _linkrot_formatter(record: logging.LogRecord) -> str:
msg_date = datetime.fromtimestamp(record.created).strftime(
"%B %d, %Y @ %I:%M:%S %p"
)
msg_date = datetime.fromtimestamp(record.created).strftime("%B %d, %Y @ %I:%M:%S %p")
msg = f""":warning: Webring Alert :warning:
Alert level: **{record.levelname.capitalize()}**
Date: {msg_date}
Webring URL: {request.root_url}
Link ID: `{record.msg['id']}`
Link URL: {record.msg['url']}
Message: {record.msg['message']}"""
Link ID: `{record.msg["id"]}`
Link URL: {record.msg["url"]}
Message: {record.msg["message"]}"""
return msg


Expand Down
4 changes: 1 addition & 3 deletions src/views/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ def embed() -> Response:

# Render the JavaScript module, taking care to indicate it's a JS file
# so browsers correctly load it
resp = make_response(
render_template("webring-embed.js", **{"all_links": all_links})
)
resp = make_response(render_template("webring-embed.js", **{"all_links": all_links}))
resp.mimetype = "text/javascript"
return resp
12 changes: 5 additions & 7 deletions tests/test_linkrot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ def test_single_link_single_fail(client):
),
json={"url": helpers.item_dead_url()["url"]},
)
response = client.post(
helpers.authed_request("/", "linkrot", item_id, auth=helpers.VALID_AUTH)
)
response = client.post(helpers.authed_request("/", "linkrot", item_id, auth=helpers.VALID_AUTH))
response_data = helpers.from_json(response.get_data(as_text=True))
assert response.status_code == 200
assert response_data["id"] == item_id
assert response_data["url"] == helpers.item_dead_url()["url"]
assert response_data["result"]["times_failed"] == 1
assert response_data["result"]["is_dead"] == False
assert response_data["result"]["is_web_archive"] == False
assert response_data["result"]["is_dead"] is False
assert response_data["result"]["is_web_archive"] is False


def test_single_link_is_dead(client):
Expand All @@ -46,5 +44,5 @@ def test_single_link_is_dead(client):
assert response_data["id"] == item_id
assert response_data["url"] == helpers.item_dead_url()["url"]
assert response_data["result"]["times_failed"] == 0
assert response_data["result"]["is_dead"] == True
assert response_data["result"]["is_web_archive"] == False
assert response_data["result"]["is_dead"] is True
assert response_data["result"]["is_web_archive"] is False

0 comments on commit ae24075

Please sign in to comment.