"""Whether a contributor can send a file the collection lacks.""" from __future__ import annotations import re from resolve.gamedata import is_game_data from resolve.validation import installed_everywhere # A name the code builds from the content (.txt, *.ini) is a # pattern, not a file anyone can upload. PATTERN = re.compile(r"[<>*?]") # A file not collected yet stays wanted, however hard it is to find ("no # public dump located", "retail data the collection does not hold"). It # leaves the list only when its reason (the profile's `unsourceable:`) # says no copy can be shared, read by the rules below. NOT_SHAREABLE = re.compile("|".join([ # Any file of the name serves: there is no one file to collect. r"\bno canonical\b", # The user writes, picks, records or places it (WHDLoad's key aside, # emucorev's config.txt: "plugin list the user writes"). r"\bthe user\b|\buser[- ](?:supplied|authored|created|settings|overlay|placeholder)\b" r"|\bother users\b", # The emulator or the guest writes it: a save, a state, a generated key. r"\bwritten by (?:the (?:emulator|guest|firmware editor)|bam)\b|\bgenerated by the emulator\b" r"|\bwith the debug key\b|\bmachine state\b|\bcreat\w* (?:this directory )?empty\b", # It belongs to one person, one account or one copy (puae's WHDLoad.key). r"\bper[- ]user\b|\btied to the (?:account|tag)\b|\bnever distributed generically\b" r"|\bissued for one copy\b", # Nothing was ever released under the name. r"\bno (?:distributed form|released artefact|hardware dump equivalent)\b" r"|\bno file of this name is distributed\b", ]), re.I) def shareable(reason: str) -> bool: """False when a reason says no copy of the file can be shared.""" return not NOT_SHAREABLE.search(reason) def can_send(name: str, reason: str = "") -> bool: """Whether a declared name and the reason it is not collected leave a file someone can upload: not a pattern, not a file no copy of which can be shared.""" return not PATTERN.search(name) and shareable(reason) def sendable(entry: dict, reason: str = "") -> bool: """Whether a profile entry the collection lacks is a file to ask for: can_send() holds, it is not game data (no contributor's to send), and the emulator does not install it on every setup.""" return (can_send(str(entry.get("name", "")), reason) and not is_game_data(entry) and not installed_everywhere(entry))