32 lines
858 B
Python
32 lines
858 B
Python
import os
|
|
from typing import IO, Any
|
|
from urllib.parse import parse_qs, urlparse
|
|
|
|
if os.name == "nt":
|
|
import msvcrt
|
|
elif os.name == "posix":
|
|
import fcntl
|
|
|
|
|
|
class FileLock:
|
|
@staticmethod
|
|
def lock(file: IO[Any]) -> None:
|
|
if os.name == "nt":
|
|
msvcrt.locking(file.fileno(), msvcrt.LK_LOCK, 1)
|
|
elif os.name == "posix":
|
|
fcntl.flock(file.fileno(), fcntl.LOCK_EX)
|
|
|
|
@staticmethod
|
|
def unlock(file: IO[Any]) -> None:
|
|
if os.name == "nt":
|
|
msvcrt.locking(file.fileno(), msvcrt.LK_UNLCK, 1)
|
|
elif os.name == "posix":
|
|
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
|
|
|
|
|
|
def page_num_from_url(url: str) -> int | None:
|
|
parsed_url = urlparse(url)
|
|
params = parse_qs(parsed_url.query)
|
|
page = params.get("page", [None])[0]
|
|
return int(page) if page is not None else None
|