sok.file_operations.detection
Detection of the media type of files dropped in the app.
The user drops files or folders without saying what they are. This module sorts them into groups that one page of the app can handle at a time. The batch pages take all their files at once: one group of movies, one of tracks, one of books, one of games. The other pages take one item at a time: one group per series and per album. Files whose type is a guess are kept apart for the user to check.
DetectedFiledataclass
class DetectedFile( path: Path, kind: ContentType, title: Optional[str], certain: bool, reason: str,)One file and the type found for it.
Parameters
| Name | Type | Description |
|---|---|---|
path | Path | The file. |
kind | ContentType | MOVIE, EPISODE, ALBUM (a track of an album), TRACK (a track on its own), BOOK or GAME. |
title | Optional[str] | Series or album title, book or game title, or None. |
certain | bool | False when the type is a guess the user should check. |
reason | str | Why the type was chosen, for the log and the interface. |
Attributes
| Name | Type | Description |
|---|---|---|
certain | bool | False when the type is a guess the user should check. |
kind | ContentType | MOVIE, EPISODE, ALBUM (a track of an album), TRACK (a track on its own), BOOK or GAME. |
path | Path | The file. |
reason | str | Why the type was chosen, for the log and the interface. |
title | Optional[str] | Series or album title, book or game title, or None. |
DetectionReportdataclass
class DetectionReport( groups: List[MediaGroup] = list(), to_check: List[DetectedFile] = list(), ignored: List[Path] = list(),)Result of detect_sources().
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
groups | List[MediaGroup] | list() | Sure files, movies first, then series, albums, books, games. |
to_check | List[DetectedFile] | list() | Files whose type is a guess, with the suggested type. |
ignored | List[Path] | list() | Files that are not media, hidden files and samples. |
Attributes
| Name | Type | Description |
|---|---|---|
groups | List[MediaGroup] | Sure files, movies first, then series, albums, books, games. |
ignored | List[Path] | Files that are not media, hidden files and samples. |
to_check | List[DetectedFile] | Files whose type is a guess, with the suggested type. |
total_filesproperty
int
Number of media files found, checked or not.
add
def add(item: DetectedFile) -> MediaGroup:Put a file in the group of its series, album, book or game.
The group is created if needed, and the groups stay in page order.
Parameters
| Name | Type | Description |
|---|---|---|
item | DetectedFile | The file, whose type is taken as sure. |
Returns
MediaGroupThe group the file went into.
MediaGroupdataclass
class MediaGroup( kind: ContentType, title: Optional[str], files: List[Path] = list(), key: str = '',)Files that one page of the app organizes together.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
kind | ContentType | MOVIE, EPISODE, ALBUM, TRACK, BOOK or GAME. | |
title | Optional[str] | Series or album title; None for the kinds of BATCH_KINDS, whose files are all in one group. | |
files | List[Path] | list() | The files, sorted naturally. |
key | str | '' |
Attributes
| Name | Type | Description |
|---|---|---|
files | List[Path] | The files, sorted naturally. |
key | str | |
kind | ContentType | MOVIE, EPISODE, ALBUM, TRACK, BOOK or GAME. |
title | Optional[str] | Series or album title; None for the kinds of BATCH_KINDS, whose files are all in one group. |
MediaTypeDetector
class MediaTypeDetector()Find the type of media files from their name, extension and folder.
Sourcesupported_extensionsproperty
List[str]
Extensions of the files the app can organize, sorted.
confirm
def confirm( report: DetectionReport, item: DetectedFile, kind: ContentType,) -> MediaGroup:Move a file to check into a group, with the type the user chose.
Parameters
| Name | Type | Description |
|---|---|---|
report | DetectionReport | The report that holds the file in to_check. |
item | DetectedFile | The file to check. |
kind | ContentType | MOVIE, EPISODE, ALBUM, TRACK, BOOK or GAME. |
Returns
MediaGroupThe group the file went into.
detect_file
def detect_file(path: Path) -> Optional[DetectedFile]:Find the type of one file, without looking at the files around it.
Parameters
| Name | Type | Description |
|---|---|---|
path | Path | The file. |
Returns
Optional[DetectedFile]The detected file, or None if it is ignored (see is_ignored).
detect_sources
def detect_sources(paths: Iterable[Path]) -> DetectionReport:Sort dropped files and folders into groups.
Folders are scanned recursively. A movie found in a folder that holds mostly episodes of one series is kept apart: without a year in its name, it is often a bonus of the series. Next to the episodes of several series (a downloads folder), it stays a movie. Audio files are an album when their folder holds one album (see _is_album), tracks on their own otherwise.
Parameters
| Name | Type | Description |
|---|---|---|
paths | Iterable[Path] | Files and folders, in the order they were dropped. |
Returns
DetectionReportThe groups, the files to check and the ignored files.
is_ignored
def is_ignored(path: Path) -> bool:Return True for files that are not media or must be skipped.
Hidden files (including the "._" files macOS writes on other disks), video samples and generic files (GENERIC_EXTENSIONS) are skipped.
Parameters
| Name | Type | Description |
|---|---|---|
path | Path |
Functions
detect_sources
def detect_sources(paths: Iterable[Path]) -> DetectionReport:Sort dropped files and folders into groups (see MediaTypeDetector).
Parameters
| Name | Type | Description |
|---|---|---|
paths | Iterable[Path] |
Attributes
| Name | Value |
|---|---|
ALBUM_MIN_TRACKS | 3 |
AMBIGUOUS_EXTENSIONS | {'.iso', '.img'} |
BATCH_KINDS | {ContentType.MOVIE, ContentType.TRACK, ContentType.BOOK, ContentType.GAME} |
DISC_FOLDER_PATTERN | re.compile('^(?:cd|disc|disk|disque)[\\s._\\-]?\\d{1,2}$', re.IGNORECASE) |
EPISODE_PATTERN | re.compile('(?<![a-z])S\\d{1,2}\\]?[.\\s_\\-]?\\[?E\\d{1,3}|(?:^|[.\\s_\\-])\\d{1,2}x\\d{1,3}(?:$|[.\\s_\\-])|(?:season|saison)[.\\s_\\-]?\\d{1,2}[.\\s_\\-]+(?:episode|ep|e)[.\\s_\\-]?\\d{1,3}', re.IGNORECASE) |
GENERIC_EXTENSIONS | {'.txt', '.rtf', '.doc', '.docx', '.exe', '.zip', '.rar', '.7z'} |
KIND_ORDER | [ContentType.MOVIE, ContentType.EPISODE, ContentType.ALBUM, ContentType.TRACK, ContentType.BOOK, ContentType.GAME] |
SAMPLE_PATTERN | re.compile('(?:^|[\\s._\\-\\[(])sample(?:$|[\\s._\\-\\])])', re.IGNORECASE) |
SEASON_FOLDER_PATTERN | re.compile('(?:^|[\\s._\\-])(?:s|season|saison|staffel|temporada|stagione|sezon)[\\s._\\-]?\\d{1,2}$', re.IGNORECASE) |