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
| Name | Type | Default | Description |
|---|---|---|---|
code | str | What is wrong: "braces", "unknown", "twice" or "required". | |
name | str | '' | The variable concerned, if any. |
Attributes
| Name | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
pattern | str | |
values | Dict[str, Any] |
Attributes
| Name | Type |
|---|---|
pattern | str |
values | Dict[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
| Name | Type | Description |
|---|---|---|
kind | str | |
pattern | str |
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
| Name | Type | Description |
|---|---|---|
kind | str | EPISODE, MOVIE, MUSIC, BOOK or GAME. |
pattern | str | The pattern, such as "{title} ({year})". |
Returns
re.Pattern[str]The compiled regex, case-insensitive, with one group per variable.
Raises
PatternErrorThe pattern is not valid for that kind.
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
| Name | Type | Description |
|---|---|---|
kind | str | EPISODE, MOVIE, MUSIC, BOOK or GAME. |
stem | str | The file name, without its extension. |
patterns | List[str] | The patterns to try, in order. Invalid ones are skipped. |
Returns
Optional[PatternMatch]The pattern and its values (numbers as int), or None.
match_user_pattern
def match_user_pattern(kind: str, filename: str) -> Optional[PatternMatch]:Read a file name with the patterns of the settings.
Parameters
| Name | Type | Description |
|---|---|---|
kind | str | EPISODE, MOVIE, MUSIC, BOOK or GAME. |
filename | str | The file name, with its extension. |
user_patterns
def user_patterns(kind: str) -> List[str]:Patterns of the settings for a kind of name.
Parameters
| Name | Type | Description |
|---|---|---|
kind | str |
Attributes
| Name | Type | Value |
|---|---|---|
BOOK | 'book' | |
BUILTIN | Dict[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'} | |
REQUIRED | Dict[str, str] | {EPISODE: 'episode', MOVIE: 'title', MUSIC: 'title', BOOK: 'title', GAME: 'title'} |
VARIABLES | Dict[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']} |