From 595640efdf8c4d366d1d817011b580c95d176e38 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 25 Aug 2024 20:50:03 +0200 Subject: [PATCH] Ruff --- personal_data/html_util.py | 6 +++++- personal_data/main.py | 12 +++--------- personal_data/parse_util.py | 3 ++- personal_data/util.py | 28 ++++++++++++++++++++-------- personal_data_calendar/__main__.py | 22 ++++++++++++++-------- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/personal_data/html_util.py b/personal_data/html_util.py index 91d2b5d..fc02319 100644 --- a/personal_data/html_util.py +++ b/personal_data/html_util.py @@ -71,7 +71,11 @@ def has_data_attribute(e) -> bool: def normalize_soup_slightly( - soup, classes=True, scripts=True, comments=True, data_attributes=True, + soup, + classes=True, + scripts=True, + comments=True, + data_attributes=True, ): """Perform soup normalization.""" # Little if any content diff --git a/personal_data/main.py b/personal_data/main.py index c21bcec..795cfa7 100644 --- a/personal_data/main.py +++ b/personal_data/main.py @@ -1,18 +1,12 @@ -import csv -import datetime -import decimal import inspect -import io -from pathlib import Path import logging -from collections.abc import Iterable, Mapping, Sequence -from decimal import Decimal +from collections.abc import Sequence +from pathlib import Path import requests import requests_cache -from frozendict import frozendict -from . import notification, data +from . import data, notification from .util import * logger = logging.getLogger(__name__) diff --git a/personal_data/parse_util.py b/personal_data/parse_util.py index 868f55f..878b271 100644 --- a/personal_data/parse_util.py +++ b/personal_data/parse_util.py @@ -31,7 +31,8 @@ def parse_duration(text: str) -> datetime.timedelta: def parse_response_datetime(response) -> datetime.datetime: return datetime.datetime.strptime( - response.headers['Date'], FORMAT_DATE_HEADER, + response.headers['Date'], + FORMAT_DATE_HEADER, ).replace(tzinfo=datetime.UTC) diff --git a/personal_data/util.py b/personal_data/util.py index 4d64b50..b11025a 100644 --- a/personal_data/util.py +++ b/personal_data/util.py @@ -2,13 +2,13 @@ import csv import datetime import decimal import io -import urllib.parse -from pathlib import Path import logging -from collections.abc import Iterable, Mapping, Sequence, Callable -from decimal import Decimal - import typing +import urllib.parse +from collections.abc import Callable, Iterable, Mapping, Sequence +from decimal import Decimal +from pathlib import Path + from frozendict import frozendict from . import data @@ -20,14 +20,25 @@ csv.register_dialect(CSV_DIALECT, lineterminator='\n', skipinitialspace=True) T = typing.TypeVar('T') -def try_value(fn: Callable[[str],T], s: str) -> T | None: + +def try_value(fn: Callable[[str], T], s: str) -> T | None: try: return fn(s) except (ValueError, decimal.InvalidOperation): return None -def to_value(s: str) -> str | Decimal | datetime.date | datetime.datetime | urllib.parse.ParseResult | bool | None: +def to_value( + s: str, +) -> ( + str + | Decimal + | datetime.date + | datetime.datetime + | urllib.parse.ParseResult + | bool + | None +): s = s.strip() if len(s) == 0: return None @@ -139,6 +150,7 @@ def load_csv_file(csv_file: Path) -> list[frozendict]: del csvfile return dicts + def extend_csv_file( csv_file: Path, new_dicts: list[dict], @@ -175,7 +187,7 @@ def extend_csv_file( output_csv = csvfile_in_memory.getvalue() del writer, csvfile_in_memory - csv_file.parent.mkdir(parents=True,exist_ok=True) + csv_file.parent.mkdir(parents=True, exist_ok=True) with open(csv_file, 'w') as csvfile: csvfile.write(output_csv) del csvfile diff --git a/personal_data_calendar/__main__.py b/personal_data_calendar/__main__.py index 373b62f..56171c1 100644 --- a/personal_data_calendar/__main__.py +++ b/personal_data_calendar/__main__.py @@ -1,13 +1,13 @@ import argparse -import logging -import icalendar -import urllib.parse import datetime -import csv +import urllib.parse + +import icalendar from personal_data.util import load_csv_file -NOW = datetime.datetime.now(tz = datetime.UTC) +NOW = datetime.datetime.now(tz=datetime.UTC) + def parse_arguments(): parser = argparse.ArgumentParser() @@ -15,6 +15,7 @@ def parse_arguments(): parser.add_argument('output_file') return parser.parse_args() + def generate_calendar(rows: list[dict]) -> icalendar.Calendar: cal = icalendar.Calendar() cal.add('prodid', '-//personal_data_calendar//example.org//') @@ -24,9 +25,13 @@ def generate_calendar(rows: list[dict]) -> icalendar.Calendar: print(event_data) # Select data - possible_time_keys = [k for k,v in event_data.items() if isinstance(v, datetime.date)] - possible_name_keys = [k for k,v in event_data.items() if isinstance(v, str)] - possible_image_keys = [k for k,v in event_data.items() if isinstance(v, urllib.parse.ParseResult)] + possible_time_keys = [ + k for k, v in event_data.items() if isinstance(v, datetime.date) + ] + possible_name_keys = [k for k, v in event_data.items() if isinstance(v, str)] + possible_image_keys = [ + k for k, v in event_data.items() if isinstance(v, urllib.parse.ParseResult) + ] date = event_data[possible_time_keys[0]] if possible_time_keys else None title = event_data[possible_name_keys[0]] @@ -64,5 +69,6 @@ def main(): with open(args.output_file, 'wb') as f: f.write(calendar.to_ical()) + if __name__ == '__main__': main()