Skip to content

sok.core.name_patterns

Input patterns: how the user's file names are built.

A pattern uses the variables of the name formats, such as "{artist} - {year} - {album} - {track} - {title}". The parsers of the file operations try the patterns of the settings before their own rules, so a naming scheme S.O.K does not know can still be read.

In a pattern, spaces, dots and underscores are the same separator, and the separators around a dash are optional. {year} takes four digits; {season}, {episode}, {track}, {disc} and {series_number} take a number; the other variables take text. {ignore} skips a part of the name, as many times as needed.

PatternError

class PatternError(code: str, name: str = '')

Bases: ValueError

A pattern that cannot be used.

Parameters

NameTypeDefaultDescription
codestrWhat is wrong: "braces", "unknown", "twice" or "required".
namestr''The variable concerned, if any.
Source

Attributes

NameDescription
code

What is wrong: "braces", "unknown", "twice" or "required".

name

The variable concerned, if any.

PatternMatchdataclass

class PatternMatch(pattern: str, values: Dict[str, Any])

A pattern that matched a file name, and the values it read.

Parameters

NameTypeDescription
patternstr
valuesDict[str, Any]
Source

Attributes

NameType
patternstr
valuesDict[str, Any]

Functions

check_pattern

def check_pattern(kind: str, pattern: str) -> Optional[PatternError]:

Return what is wrong with a pattern, or None if it can be used.

Parameters

NameTypeDescription
kindstr
patternstr
Source

compile_pattern

def compile_pattern(kind: str, pattern: str) -> re.Pattern[str]:

Turn a pattern into a regex that matches a whole file name.

Parameters

NameTypeDescription
kindstrEPISODE, MOVIE, MUSIC, BOOK or GAME.
patternstrThe pattern, such as "{title} ({year})".

Returns

  • re.Pattern[str]The compiled regex, case-insensitive, with one group per variable.

Raises

Source

match_name

def match_name(
kind: str,
stem: str,
patterns: List[str],
) -> Optional[PatternMatch]:

Read a file name with the first pattern that matches it.

Parameters

NameTypeDescription
kindstrEPISODE, MOVIE, MUSIC, BOOK or GAME.
stemstrThe file name, without its extension.
patternsList[str]The patterns to try, in order. Invalid ones are skipped.

Returns

  • Optional[PatternMatch]The pattern and its values (numbers as int), or None.
Source

match_user_pattern

def match_user_pattern(kind: str, filename: str) -> Optional[PatternMatch]:

Read a file name with the patterns of the settings.

Parameters

NameTypeDescription
kindstrEPISODE, MOVIE, MUSIC, BOOK or GAME.
filenamestrThe file name, with its extension.
Source

user_patterns

def user_patterns(kind: str) -> List[str]:

Patterns of the settings for a kind of name.

Parameters

NameTypeDescription
kindstr
Source

Attributes

NameTypeValue
BOOK'book'
BUILTINDict[str, List[str]]{EPISODE: ['{title} S{season}E{episode}', '{title} {season}x{episode}', '{title} Season {season} Episode {episode}', '{title} Saison {season} Episode {episode}', '{title} {season}.{episode}', '{title} Ep {episode}'], MOVIE: ['{title} ({year})', '{title} {year} {ignore}'], MUSIC: ['{artist} - {album} - {track} - {title}', 'CD{disc} {track} {title}', '{track} - {title}', '{track}. {title}', 'Track {track} {title}'], BOOK: ['{author} - [{series} {series_number}] - {title}', '{author} - {title} ({year})', '{author} - {title}', '{title} - {author}'], GAME: ['[{platform}] {title} ({region})', '{title} ({region}) [{ignore}]']}
EPISODE'episode'
GAME'game'
IGNORE'ignore'
MOVIE'movie'
MUSIC'music'
NUMBERS{'season', 'episode', 'disc', 'track', 'series_number'}
REQUIREDDict[str, str]{EPISODE: 'episode', MOVIE: 'title', MUSIC: 'title', BOOK: 'title', GAME: 'title'}
VARIABLESDict[str, List[str]]{EPISODE: ['title', 'season', 'episode', 'episode_title', 'year'], MOVIE: ['title', 'year'], MUSIC: ['artist', 'album', 'disc', 'track', 'title', 'year'], BOOK: ['author', 'title', 'series', 'series_number', 'year'], GAME: ['title', 'platform', 'region', 'year']}
Built with Kiln