Browse Source

refactor: lint code with Ruff

Zubarev Grigoriy 7 months ago
parent
commit
1eeb5427a7
2 changed files with 47 additions and 12 deletions
  1. 36 0
      pyproject.toml
  2. 11 12
      src/rural_dict/__main__.py

+ 36 - 0
pyproject.toml

@@ -42,3 +42,39 @@ packages = ["src/rural_dict"]
 
 [tool.hatch.build.targets.sdist]
 exclude = ["*.xcf"]
+
+[tool.ruff]
+target-version = "py312"
+line-length = 99
+exclude = [
+    ".git",
+    ".venv",
+    ".idea",
+    ".tests",
+    "build",
+    "dist",
+]
+
+[tool.ruff.lint]
+select = [
+    "E", # pycodestyle errors
+    "W", # pycodestyle warnings
+    "F", # pyflakes
+    "I", # isort
+    "N", # pep8-naming
+    "S", # flake8-bandit
+    "B", # flake8-bugbear
+    "G", # flake8-logging-format
+    "C4", # flake8-comprehensions
+    "UP", # pyupgrade
+    "PLC", # pylint conventions
+    "PLE", # pylint errors
+    "SIM", # flake8-simplify
+    "RET", # flake8-return
+    "YTT", # flake8-2020
+    "DTZ", # flake8-datetimez
+    "RUF", # ruff-specific rules
+    "TCH", # flake8-type-checking
+    "PTH", # flake8-use-pathlib
+    "ASYNC", # flake8-async
+]

+ 11 - 12
src/rural_dict/__main__.py

@@ -1,15 +1,13 @@
-#!/usr/bin/env python
+import re
+from urllib.parse import quote, unquote
 
-from flask import Flask, render_template, request, redirect
 import requests
-import html
-import re
 from bs4 import BeautifulSoup
-from urllib.parse import quote, unquote
+from flask import Flask, redirect, render_template, request
 
 
 def scrape(url):
-    data = requests.get(url)
+    data = requests.get(url, timeout=10)
 
     our_path = re.sub(r".*://.*/", "/", request.url)
     path = re.sub(r".*://.*/", "/", data.url)
@@ -27,10 +25,11 @@ def scrape(url):
             str(entry["defid"]): entry
             for entry in requests.get(
                 "https://api.urbandictionary.com/v0/uncacheable?ids="
-                + ",".join(defid for (_, defid) in defs)
+                + ",".join(defid for (_, defid) in defs),
+                timeout=10,
             ).json()["thumbs"]
         }
-    except:
+    except (KeyError, TypeError):
         thumbs_data = {}
 
     for definition, defid in defs:
@@ -44,9 +43,9 @@ def scrape(url):
         thumbs_down = thumbs_data.get(defid, {}).get("down")
         ret.append([defid, word, meaning, example, contributor, thumbs_up, thumbs_down])
     pages = soup.find(attrs={"class": ["pagination text-xl text-center"]})
-    if pages == None:
+    if pages is None:
         pages = ""
-    return (ret, pages)
+    return ret, pages
 
 
 app = Flask(__name__, template_folder="templates", static_folder="static")
@@ -56,7 +55,7 @@ app = Flask(__name__, template_folder="templates", static_folder="static")
 @app.route("/<path:path>")
 def catch_all(path):
     scraped = scrape(f"https://urbandictionary.com/{re.sub(r'.*://.*/', '/', request.url)}")
-    if type(scraped) == str and scraped.startswith("REDIRECT"):
+    if isinstance(scraped, str) and scraped.startswith("REDIRECT"):
         return redirect(scraped.replace("REDIRECT ", ""), 302)
     return render_template("index.html", data=scraped, term=request.args.get("term"))
 
@@ -64,4 +63,4 @@ def catch_all(path):
 if __name__ == "__main__":
     from waitress import serve
 
-    serve(app, host="0.0.0.0", port=8080)
+    serve(app, host="0.0.0.0", port=8080)  # noqa: S104