Skip to content

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

NameTypeDescription
pathPathThe file.
kindContentTypeMOVIE, EPISODE, ALBUM (a track of an album), TRACK (a track on its own), BOOK or GAME.
titleOptional[str]Series or album title, book or game title, or None.
certainboolFalse when the type is a guess the user should check.
reasonstrWhy the type was chosen, for the log and the interface.
Source

Attributes

NameTypeDescription
certainbool

False when the type is a guess the user should check.

kindContentType

MOVIE, EPISODE, ALBUM (a track of an album), TRACK (a track on its own), BOOK or GAME.

pathPath

The file.

reasonstr

Why the type was chosen, for the log and the interface.

titleOptional[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

NameTypeDefaultDescription
groupsList[MediaGroup]list()Sure files, movies first, then series, albums, books, games.
to_checkList[DetectedFile]list()Files whose type is a guess, with the suggested type.
ignoredList[Path]list()Files that are not media, hidden files and samples.
Source

Attributes

NameTypeDescription
groupsList[MediaGroup]

Sure files, movies first, then series, albums, books, games.

ignoredList[Path]

Files that are not media, hidden files and samples.

to_checkList[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

NameTypeDescription
itemDetectedFileThe file, whose type is taken as sure.

Returns

Source

MediaGroupdataclass

class MediaGroup(
kind: ContentType,
title: Optional[str],
files: List[Path] = list(),
key: str = '',
)

Files that one page of the app organizes together.

Parameters

NameTypeDefaultDescription
kindContentTypeMOVIE, EPISODE, ALBUM, TRACK, BOOK or GAME.
titleOptional[str]Series or album title; None for the kinds of BATCH_KINDS, whose files are all in one group.
filesList[Path]list()The files, sorted naturally.
keystr''
Source

Attributes

NameTypeDescription
filesList[Path]

The files, sorted naturally.

keystr
kindContentType

MOVIE, EPISODE, ALBUM, TRACK, BOOK or GAME.

titleOptional[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.

Source

supported_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

NameTypeDescription
reportDetectionReportThe report that holds the file in to_check.
itemDetectedFileThe file to check.
kindContentTypeMOVIE, EPISODE, ALBUM, TRACK, BOOK or GAME.

Returns

Source

detect_file

def detect_file(path: Path) -> Optional[DetectedFile]:

Find the type of one file, without looking at the files around it.

Parameters

NameTypeDescription
pathPathThe file.

Returns

  • Optional[DetectedFile]The detected file, or None if it is ignored (see is_ignored).
Source

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

NameTypeDescription
pathsIterable[Path]Files and folders, in the order they were dropped.

Returns

Source

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

NameTypeDescription
pathPath
Source

Functions

detect_sources

def detect_sources(paths: Iterable[Path]) -> DetectionReport:

Sort dropped files and folders into groups (see MediaTypeDetector).

Parameters

NameTypeDescription
pathsIterable[Path]
Source

Attributes

NameValue
ALBUM_MIN_TRACKS3
AMBIGUOUS_EXTENSIONS{'.iso', '.img'}
BATCH_KINDS{ContentType.MOVIE, ContentType.TRACK, ContentType.BOOK, ContentType.GAME}
DISC_FOLDER_PATTERNre.compile('^(?:cd|disc|disk|disque)[\\s._\\-]?\\d{1,2}$', re.IGNORECASE)
EPISODE_PATTERNre.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_PATTERNre.compile('(?:^|[\\s._\\-\\[(])sample(?:$|[\\s._\\-\\])])', re.IGNORECASE)
SEASON_FOLDER_PATTERNre.compile('(?:^|[\\s._\\-])(?:s|season|saison|staffel|temporada|stagione|sezon)[\\s._\\-]?\\d{1,2}$', re.IGNORECASE)
Built with Kiln