apply Black v23.1.0 formatting changes

This commit is contained in:
ducklet 2023-02-04 01:12:50 +01:00
parent 8e1988eea2
commit 8a8bfce89d
7 changed files with 3 additions and 35 deletions

View file

@ -119,7 +119,6 @@ async def apply_db_patches(db: Database):
raise RuntimeError("No statement found.")
async with db.transaction():
for query in queries:
await db.execute(query)

View file

@ -35,12 +35,10 @@ log = logging.getLogger(__name__)
async def refresh_user_ratings_from_imdb(stop_on_dupe=True):
with session() as s:
s.headers["Accept-Language"] = "en-US, en;q=0.5"
for user in await db.get_all(User):
log.info("⚡️ Loading data for %s ...", user.name)
try:
@ -97,7 +95,6 @@ find_movie_id = re.compile(r"/title/(?P<id>tt\d+)/").search
def movie_and_rating_from_item(item) -> tuple[Movie, Rating]:
genres = (genre := item.find("span", "genre")) and genre.string or ""
movie = Movie(
title=item.h3.a.string.strip(),
@ -169,7 +166,6 @@ async def parse_page(url) -> tuple[list[Rating], str | None]:
items = soup.find_all("div", "lister-item-content")
for i, item in enumerate(items):
try:
movie, rating = movie_and_rating_from_item(item)
except Exception as err:
@ -199,7 +195,6 @@ async def load_ratings(user_id):
next_url = user_ratings_url(user_id)
while next_url:
ratings, next_url = await parse_page(next_url)
for i, rating in enumerate(ratings):

View file

@ -115,7 +115,6 @@ def count_lines(path: Path) -> int:
buf_size = 8 * one_mb # 8 MiB seems to give a good read/process performance.
with gzip.open(path, "rt") as f:
while buf := f.read(buf_size):
i += buf.count("\n")
@ -138,7 +137,6 @@ def read_imdb_tsv(
def read_imdb_tsv(path: Path, row_type, *, unpack=True):
with gzip.open(path, "rt", newline="") as f:
rows = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
# skip header line
@ -205,7 +203,6 @@ async def import_from_file(*, basics_path: Path, ratings_path: Path):
chunk = []
for i, m in enumerate(read_basics(basics_path)):
perc = 100 * i / total
if perc >= perc_next_report:
await db.set_import_progress(perc)

View file

@ -41,7 +41,6 @@ def fields(class_or_instance):
# XXX this might be a little slow (not sure), if so, memoize
for f in _fields(class_or_instance):
if f.name == "_is_lazy":
continue
@ -108,7 +107,6 @@ def asplain(
d: JSONObject = {}
for f in fields(o):
if filter_fields is not None and f.name not in filter_fields:
continue
@ -156,7 +154,6 @@ def fromplain(cls: Type[T], d: dict[str, Any], *, serialized: bool = False) -> T
dd: JSONObject = {}
for f in fields(cls):
target = f.type
otype = optional_type(f.type)
is_opt = otype is not None

View file

@ -77,7 +77,6 @@ def Session() -> requests.Session:
def throttle(
times: int, per_seconds: float, jitter: Callable[[], float] | None = None
) -> Callable[[Callable], Callable]:
calls: deque[float] = deque(maxlen=times)
if jitter is None:
@ -86,7 +85,6 @@ def throttle(
def decorator(func: Callable) -> Callable:
@wraps(func)
def inner(*args, **kwds):
# clean up
while calls:
if calls[0] + per_seconds > time():
@ -151,7 +149,6 @@ def cache_path(req) -> Path | None:
@throttle(1, 1, random)
def http_get(s: requests.Session, url: str, *args, **kwds) -> requests.Response:
req = s.prepare_request(requests.Request("GET", url, *args, **kwds))
cachefile = cache_path(req) if config.debug else None
@ -244,7 +241,6 @@ def download(
raise FileExistsError(23, "Would replace existing file", str(file_path))
with session() as s:
headers = {}
if file_exists and only_if_newer:
assert file_path

View file

@ -195,7 +195,6 @@ route.registered = _routes
@route("/groups/{group_id}/ratings")
async def get_ratings_for_group(request):
group_id = as_ulid(request.path_params["group_id"])
group = await db.get(Group, id=str(group_id))
@ -256,7 +255,6 @@ def not_implemented():
@route("/movies")
@requires(["authenticated"])
async def list_movies(request):
params = request.query_params
user = await auth_user(request)
@ -324,7 +322,6 @@ async def list_movies(request):
@route("/movies", methods=["POST"])
@requires(["authenticated", "admin"])
async def add_movie(request):
not_implemented()
@ -366,7 +363,6 @@ _import_lock = asyncio.Lock()
@route("/movies/_reload_imdb", methods=["POST"])
@requires(["authenticated", "admin"])
async def load_imdb_movies(request):
params = request.query_params
force = truthy(params.get("force"))
@ -389,7 +385,6 @@ async def load_imdb_movies(request):
@route("/users")
@requires(["authenticated", "admin"])
async def list_users(request):
users = await db.get_all(User)
return JSONResponse([asplain(u) for u in users])
@ -398,7 +393,6 @@ async def list_users(request):
@route("/users", methods=["POST"])
@requires(["authenticated", "admin"])
async def add_user(request):
name, imdb_id = await json_from_body(request, ["name", "imdb_id"])
# XXX restrict name
@ -420,7 +414,6 @@ async def add_user(request):
@route("/users/{user_id}")
@requires(["authenticated"])
async def show_user(request):
user_id = as_ulid(request.path_params["user_id"])
if is_admin(request):
@ -449,7 +442,6 @@ async def show_user(request):
@route("/users/{user_id}", methods=["DELETE"])
@requires(["authenticated", "admin"])
async def remove_user(request):
user_id = as_ulid(request.path_params["user_id"])
user = await db.get(User, id=str(user_id))
@ -467,7 +459,6 @@ async def remove_user(request):
@route("/users/{user_id}", methods=["PATCH"])
@requires(["authenticated"])
async def modify_user(request):
user_id = as_ulid(request.path_params["user_id"])
if is_admin(request):
@ -515,7 +506,6 @@ async def modify_user(request):
@route("/users/{user_id}/groups", methods=["POST"])
@requires(["authenticated", "admin"])
async def add_group_to_user(request):
user_id = as_ulid(request.path_params["user_id"])
user = await db.get(User, id=str(user_id))
@ -540,21 +530,18 @@ async def add_group_to_user(request):
@route("/users/{user_id}/ratings")
@requires(["private"])
async def ratings_for_user(request):
not_implemented()
@route("/users/{user_id}/ratings", methods=["PUT"])
@requires("authenticated")
async def set_rating_for_user(request):
not_implemented()
@route("/users/_reload_ratings", methods=["POST"])
@requires(["authenticated", "admin"])
async def load_imdb_user_ratings(request):
ratings = [rating async for rating in imdb.refresh_user_ratings_from_imdb()]
return JSONResponse({"new_ratings": [asplain(r) for r in ratings]})
@ -563,7 +550,6 @@ async def load_imdb_user_ratings(request):
@route("/groups")
@requires(["authenticated", "admin"])
async def list_groups(request):
groups = await db.get_all(Group)
return JSONResponse([asplain(g) for g in groups])
@ -572,7 +558,6 @@ async def list_groups(request):
@route("/groups", methods=["POST"])
@requires(["authenticated", "admin"])
async def add_group(request):
(name,) = await json_from_body(request, ["name"])
# XXX restrict name
@ -586,7 +571,6 @@ async def add_group(request):
@route("/groups/{group_id}/users", methods=["POST"])
@requires(["authenticated"])
async def add_user_to_group(request):
group_id = as_ulid(request.path_params["group_id"])
group = await db.get(Group, id=str(group_id))