Migrate to ruff

This commit is contained in:
Zoe Roux 2024-01-31 02:32:48 +01:00
parent 898e7b272e
commit 2db6255fae
8 changed files with 21 additions and 24 deletions

View File

@ -47,10 +47,9 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run black
run: |
pip3 install black-with-tabs
black . --check
- uses: chartboost/ruff-action@v1
with:
args: format --check
transcoder:
name: "Lint transcoder"

View File

@ -18,7 +18,6 @@ from ..types.studio import Studio
from ..types.genre import Genre
from ..types.metadataid import MetadataID
from ..types.show import Show, ShowTranslation, Status as ShowStatus
from ..types.season import Season
from ..types.collection import Collection, CollectionTranslation

View File

@ -1,4 +1,2 @@
[tool.black]
extend-exclude = '''
^/scanner/cache.py
'''
[tool.ruff.format]
indent-style = "tab"

View File

@ -1,5 +1,4 @@
guessit
aiohttp
jsons
black-with-tabs
watchfiles

View File

@ -4,12 +4,17 @@ from functools import wraps
from typing import Any, Optional, Tuple, Final, Literal
from enum import Enum
# Waiting for https://github.com/python/typing/issues/689 for better sentinels
class Sentinel(Enum):
NoneSentinel = 0
none: Final = Sentinel.NoneSentinel
type Cache = dict[Any, Tuple[asyncio.Event | Literal[none], datetime | Literal[none], Any]]
type Cache = dict[
Any, Tuple[asyncio.Event | Literal[none], datetime | Literal[none], Any]
]
def cache(ttl: timedelta, cache: Optional[Cache] = None, typed=False):
"""
@ -18,8 +23,8 @@ def cache(ttl: timedelta, cache: Optional[Cache] = None, typed=False):
result will be cached.
Args:
typed: same as functools.lru_cache
ttl: how many time should the cached value be considered valid?
typed: same as functools.lru_cache
ttl: how many time should the cached value be considered valid?
"""
@ -41,11 +46,7 @@ def cache(ttl: timedelta, cache: Optional[Cache] = None, typed=False):
return await wrapper(*args, **kwargs)
return ret[2]
# Return the cached result if it exits and is not expired
if (
ret[2] != none
and ret[1] != none
and datetime.now() - ret[1] < ttl
):
if ret[2] != none and ret[1] != none and datetime.now() - ret[1] < ttl:
return ret[2]
return await exec_as_cache(cache, key, lambda: f(*args, **kwargs))
@ -54,6 +55,7 @@ def cache(ttl: timedelta, cache: Optional[Cache] = None, typed=False):
return wrap
async def exec_as_cache(cache: Cache, key, f):
event = asyncio.Event()
cache[key] = (event, none, none)

View File

@ -6,7 +6,7 @@ from .scanner import Scanner
async def monitor(path: str, scanner: Scanner):
async for changes in awatch(path):
for (event, file) in changes:
for event, file in changes:
try:
if event == Change.added:
await scanner.identify(file)

View File

@ -82,7 +82,7 @@ class Scanner:
raw = guessit(path, {"episode_prefer_number": True, "excludes": "language"})
if not "mimetype" in raw or not raw["mimetype"].startswith("video"):
if "mimetype" not in raw or not raw["mimetype"].startswith("video"):
return
# Remove seasons in "One Piece (1999) 152.mkv" for example
if raw.get("season") == raw.get("year") and "season" in raw:

View File

@ -1,11 +1,10 @@
{pkgs ? import <nixpkgs> {}}: let
python = pkgs.python312.withPackages (ps:
python = pkgs.python311.withPackages (ps:
with ps; [
guessit
aiohttp
jsons
watchfiles
black
]);
dotnet = with pkgs.dotnetCorePackages;
combinePackages [
@ -20,8 +19,9 @@ in
nodePackages.eas-cli
nodePackages.expo-cli
dotnet
# csharpier
# python
csharpier
python
ruff
go
wgo
mediainfo