Ruff
This commit is contained in:
parent
aebf3c7df4
commit
595640efdf
|
@ -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
|
||||
|
|
|
@ -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__)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user