From ceee620f0b7791e6f39badde174d7ec6d1ee999f Mon Sep 17 00:00:00 2001 From: flofriday Date: Mon, 12 Feb 2024 21:36:55 +0100 Subject: [PATCH] Add ruff as a formatter and linter --- .gitignore | 2 ++ app/__init__.py | 13 +++++++------ app/format.py | 5 +++-- app/monitoring.py | 2 +- app/resources/generate_rooms.py | 3 ++- pyproject.toml | 3 +++ requirements.txt | 1 + tests/conftest.py | 1 + tests/test_request.py | 3 ++- 9 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 42eed97..8cbaedd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ .venv/ __pycache__/ +.pytest_cache/ +.ruff_cache/ node_modules/ app/static/style.css bettercal.db* diff --git a/app/__init__.py b/app/__init__.py index 1556f69..bb00c36 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,11 +1,12 @@ import json -from flask import Flask, render_template, send_from_directory, request, g -import requests import sqlite3 +import requests +from flask import Flask, g, render_template, request, send_from_directory + import app.tiss as tiss from app.format import improve_calendar -from app.monitoring import get_statistics, add_usage, get_chart_data +from app.monitoring import add_usage, get_chart_data, get_statistics app = Flask(__name__) @@ -86,7 +87,7 @@ def verify(): return "Could not contact TISS. Maybe TISS is down?", 400 except ValueError: return "TISS didn't return an ical file, did you paste the correct url?", 400 - except: + except Exception: return "Somthing unexpected went wrong, maybe create an GitHub issue?", 500 return "Ok" @@ -102,8 +103,8 @@ def icalendar(): if locale is None: return "No locale provided", 400 - is_google = "google" in request.args.keys() - use_shorthand = "noshorthand" not in request.args.keys() + is_google = "google" in request.args + use_shorthand = "noshorthand" not in request.args url = f"https://tiss.tuwien.ac.at/events/rest/calendar/personal?token={token}&locale={locale}" cal = tiss.get_calendar(url) diff --git a/app/format.py b/app/format.py index d8b4304..1b1d86e 100644 --- a/app/format.py +++ b/app/format.py @@ -1,9 +1,10 @@ import csv import html -from icalendar import Calendar +import re from dataclasses import dataclass from functools import cache -import re + +from icalendar import Calendar summary_regex = re.compile("([0-9A-Z]{3}\\.[0-9A-Z]{3}) ([A-Z]{2}) (.*)") diff --git a/app/monitoring.py b/app/monitoring.py index 965a34c..4a07519 100644 --- a/app/monitoring.py +++ b/app/monitoring.py @@ -1,6 +1,6 @@ +import hashlib from dataclasses import dataclass from sqlite3 import Connection -import hashlib from typing import Tuple diff --git a/app/resources/generate_rooms.py b/app/resources/generate_rooms.py index 909141d..d585e47 100644 --- a/app/resources/generate_rooms.py +++ b/app/resources/generate_rooms.py @@ -2,10 +2,11 @@ # It does this automatically by the room UI from tiss and extracting the information from there. # To run this you first need to: pip install selenium import csv + from selenium import webdriver from selenium.webdriver.common.by import By -from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.ui import WebDriverWait def main(): diff --git a/pyproject.toml b/pyproject.toml index 0c1df29..bdbe2ba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,3 +4,6 @@ testpaths = ["tests"] [tool.coverage.run] branch = true source = ["app"] + +[tool.ruff.lint] +select = ["F", "UP", "B", "SIM", "I"] diff --git a/requirements.txt b/requirements.txt index 344553d..7696b06 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,6 +21,7 @@ pytest-mock==3.12.0 python-dateutil==2.8.2 pytz==2024.1 requests==2.31.0 +ruff==0.2.1 six==1.16.0 urllib3==2.2.0 Werkzeug==3.0.1 diff --git a/tests/conftest.py b/tests/conftest.py index 8e6e669..e5acbec 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import pytest + from app import app as backendapp diff --git a/tests/test_request.py b/tests/test_request.py index 76ff8f9..e36f79a 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -1,6 +1,7 @@ +import re + from flask.testing import FlaskClient from icalendar import Calendar -import re def get_test_calendar(lang: str = "de"):