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: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Run black - uses: chartboost/ruff-action@v1
run: | with:
pip3 install black-with-tabs args: format --check
black . --check
transcoder: transcoder:
name: "Lint transcoder" name: "Lint transcoder"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -82,7 +82,7 @@ class Scanner:
raw = guessit(path, {"episode_prefer_number": True, "excludes": "language"}) 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 return
# Remove seasons in "One Piece (1999) 152.mkv" for example # Remove seasons in "One Piece (1999) 152.mkv" for example
if raw.get("season") == raw.get("year") and "season" in raw: if raw.get("season") == raw.get("year") and "season" in raw:

View File

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