Coverage for src/romfarmer/organizers/kind.py: 0%
27 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-03 11:31 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-03 11:31 -0700
1"""
2Kind-based ROM organizer.
4Organizes ROMs by kind (Games, Demos, Betas, etc.) extracted from filenames.
5Supports No-Intro naming conventions for various ROM types.
6"""
8import logging
9import re
11from .base import BaseOrganizer, OrganizeMode
13logger = logging.getLogger(__name__)
16# Standard kind markers from No-Intro
17KIND_MARKERS: dict[str, list[str]] = {
18 "Demo": ["Demo", "Kiosk"],
19 "Beta": ["Beta", "Proto", "Prototype"],
20 "Sample": ["Sample"],
21 "Program": ["Program"],
22 "Application": ["Application"],
23 "Unlicensed": ["Unlicensed"],
24 "Pirate": ["Pirate"],
25 "Aftermarket": ["Aftermarket"],
26 "Homebrew": ["Homebrew"],
27 "Hack": ["Hack"],
28 "Translation": ["T+", "T-"], # Translation markers
29 "Enhancement": ["Enhancement"],
30}
33class KindOrganizer(BaseOrganizer):
34 """
35 Organize ROMs by kind (type).
37 Extracts kind information from ROM filenames and organizes them into
38 kind-specific directories. Useful for separating games from demos,
39 betas, prototypes, etc.
41 Examples:
42 >>> organizer = KindOrganizer(
43 ... mode=OrganizeMode.MOVE,
44 ... keep_in_place=['Demo', 'Beta']
45 ... )
46 >>> stats = organizer.organize(Path('/roms/nes'))
48 # Results in:
49 # /roms/nes/Super Mario Demo (USA) (Demo).nes <- kept in place
50 # /roms/nes/By Kind/Proto/Game (USA) (Proto).nes <- organized
51 # /roms/nes/By Kind/Homebrew/Game (Homebrew).nes <- organized
53 Args:
54 mode: How to organize files (move, copy, or symlink)
55 dry_run: Preview changes without actually making them
56 keep_in_place: Kinds to keep in root directory
57 exclude_kinds: Kinds to exclude from organization
58 custom_markers: Additional kind markers to recognize
59 """
61 def __init__(
62 self,
63 mode: OrganizeMode = OrganizeMode.MOVE,
64 dry_run: bool = False,
65 keep_in_place: list[str] | None = None,
66 exclude_kinds: list[str] | None = None,
67 custom_markers: dict[str, list[str]] | None = None,
68 ):
69 super().__init__(
70 mode=mode,
71 dry_run=dry_run,
72 keep_in_place=keep_in_place,
73 exclude_values=exclude_kinds,
74 )
76 # Merge custom markers with standard ones
77 self.kind_markers = KIND_MARKERS.copy()
78 if custom_markers:
79 self.kind_markers.update(custom_markers)
81 def get_organization_dir_name(self) -> str:
82 """Get the directory name for kind organization."""
83 return "By Kind"
85 def get_organization_value(self, filename: str) -> str | None:
86 """
87 Extract kind from filename.
89 Searches for kind markers in parentheses, brackets, or as standalone words.
90 Supports formats like:
91 - (Demo)
92 - (Proto)
93 - [Beta]
94 - (T+Eng) # Translation
96 Args:
97 filename: ROM filename
99 Returns:
100 Kind name, or None if not detected
101 """
102 # Pattern to match content in parentheses or brackets
103 pattern = r"[\(\[]([^\)\]]+)[\)\]]"
105 matches = re.findall(pattern, filename)
107 if not matches:
108 return None
110 # Check each match for kind markers
111 for match in matches:
112 # Split by comma for multi-value entries
113 parts = [p.strip() for p in match.split(",")]
115 for part in parts:
116 # Check each kind and its markers
117 for kind, markers in self.kind_markers.items():
118 for marker in markers:
119 # Case-insensitive check
120 if marker.lower() in part.lower():
121 logger.debug(f"Detected kind: {kind} (marker: {marker})")
122 return kind
124 return None