Skip to content

sok.file_operations.mixins.parsing

Parsing mixin for file operations.

Provides utilities to extract information from filenames:

  • Year extraction
  • Quality metadata extraction (codec, resolution, source...)
  • Title and filename cleaning

FileParsingMixin

class FileParsingMixin

Mixin providing filename parsing utilities.

Used by all specific operations classes to avoid parsing code duplication.

Source

clean_filenamestaticmethod

def clean_filename(filename: str, replace_char: str = '_') -> str:

Clean a filename by removing invalid characters.

Parameters

NameTypeDefaultDescription
filenamestrFilename to clean.
replace_charstr'_'Replacement character.

Returns

  • strCleaned filename.
Source

clean_titlestaticmethod

def clean_title(
title: str,
replace_dots: bool = True,
replace_underscores: bool = True,
) -> str:

Clean a title by removing common formatting characters.

Parameters

NameTypeDefaultDescription
titlestrTitle to clean.
replace_dotsboolTrueReplace dots with spaces.
replace_underscoresboolTrueReplace underscores with spaces.

Returns

  • strCleaned title.
Source

extract_number_from_textstaticmethod

def extract_number_from_text(text: str, pattern: str = '\\d+') -> Optional[int]:

Extract the first number from text.

Parameters

NameTypeDefaultDescription
textstrText to analyze.
patternstr'\\d+'Regex pattern to find the number.

Returns

  • Optional[int]Found number or None.
Source

extract_quality_metadatastaticmethod

def extract_quality_metadata(text: str) -> Dict[str, Optional[str]]:

Extract quality metadata from text (filename).

Parameters

NameTypeDescription
textstrText to analyze.

Returns

  • Dict[str, Optional[str]]Extracted metadata (quality, codec, audio, source, language).
Source

extract_yearstaticmethod

def extract_year(text: str) -> Optional[int]:

Extract a year from text.

A year in parentheses wins; otherwise the last one, since a title can hold a number that looks like a year: "Blade.Runner.2049.2017" is 2017, "1917.2019" is 2019. A year after next year is not one.

Parameters

NameTypeDescription
textstrText potentially containing a year.

Returns

  • Optional[int]Extracted year or None.
Source

normalize_path_for_comparisonstaticmethod

def normalize_path_for_comparison(path: str) -> str:

Normalize a path for comparison (lowercase, unified separators).

Parameters

NameTypeDescription
pathstrPath to normalize.

Returns

  • strNormalized path.
Source

parse_track_disc_numberstaticmethod

def parse_track_disc_number(value: str) -> int:

Parse a track/disc number that may be in '1/12' format.

Parameters

NameTypeDescription
valuestrValue to parse (e.g., '5' or '5/12').

Returns

  • intExtracted number.
Source

Attributes

NameTypeValue
AUDIO_CODECSList[str]['AAC', 'AC3', 'DTS', 'TrueHD', 'Atmos', 'DD5.1', 'DD+', 'EAC3', 'FLAC', 'MP3']
CONTENT_SOURCESList[str]['BluRay', 'BRRip', 'WEB-DL', 'WEBRip', 'HDTV', 'DVDRip', 'DVD', 'REMUX']
INVALID_FILENAME_CHARSstr'<>:"/\\|?*'
LANGUAGE_PATTERNSDict[str, str]{'FRENCH': 'fr', 'VOSTFR': 'fr-sub', 'MULTI': 'multi', 'TRUEFRENCH': 'fr', 'VFF': 'fr', 'VFQ': 'fr-qc', 'ENGLISH': 'en', 'GERMAN': 'de', 'SPANISH': 'es'}
VIDEO_CODECSList[str]['x264', 'x265', 'H264', 'H265', 'HEVC', 'XviD', 'DivX', 'VP9', 'AV1']
VIDEO_QUALITY_PATTERNSDict[str, str]{'2160p': '4K', '1080p': '1080p', '720p': '720p', '480p': '480p', '4K': '4K', 'UHD': '4K', 'HD': 'HD'}
Built with Kiln