diff --git a/backend/app/importer/compiler.py b/backend/app/importer/compiler.py index 9a65f57..f64e21e 100644 --- a/backend/app/importer/compiler.py +++ b/backend/app/importer/compiler.py @@ -12,7 +12,8 @@ logger = logging.getLogger(__name__) def compile_format_to_api_structure( format_yaml: Dict[str, Any], - arr_type: str + arr_type: str, + patterns: Dict[str, str] = None ) -> Dict[str, Any]: """ Compile a format from YAML to Arr API structure. @@ -20,12 +21,15 @@ def compile_format_to_api_structure( Args: format_yaml: Format data from YAML file arr_type: 'radarr' or 'sonarr' + patterns: Pre-loaded regex patterns (if None, will load from disk) Returns: Compiled format ready for API """ target_app = TargetApp.RADARR if arr_type.lower() == 'radarr' else TargetApp.SONARR - patterns = load_regex_patterns() + # Only load patterns if not provided + if patterns is None: + patterns = load_regex_patterns() compiled = { 'name': format_yaml.get('name', 'Unknown') diff --git a/backend/app/importer/strategies/format.py b/backend/app/importer/strategies/format.py index 76ad96a..aedcc40 100644 --- a/backend/app/importer/strategies/format.py +++ b/backend/app/importer/strategies/format.py @@ -22,6 +22,11 @@ class FormatStrategy(ImportStrategy): Returns: Dictionary with 'formats' key containing compiled formats """ + from ..utils import load_regex_patterns + + # Load all regex patterns once at the start + patterns = load_regex_patterns() + formats = [] failed = [] import_logger = get_import_logger() @@ -35,7 +40,7 @@ class FormatStrategy(ImportStrategy): format_yaml = load_yaml(f"custom_format/{filename}.yml") # Compile to API structure - compiled = compile_format_to_api_structure(format_yaml, self.arr_type) + compiled = compile_format_to_api_structure(format_yaml, self.arr_type, patterns) # Add unique suffix if needed if self.import_as_unique: diff --git a/backend/app/importer/strategies/profile.py b/backend/app/importer/strategies/profile.py index 6a2dbc8..198bcda 100644 --- a/backend/app/importer/strategies/profile.py +++ b/backend/app/importer/strategies/profile.py @@ -22,6 +22,11 @@ class ProfileStrategy(ImportStrategy): Returns: Dictionary with 'profiles' and 'formats' keys """ + from ..utils import load_regex_patterns + + # Load all regex patterns once at the start + patterns = load_regex_patterns() + profiles = [] all_formats = [] processed_formats: Set[str] = set() @@ -49,7 +54,7 @@ class ProfileStrategy(ImportStrategy): try: format_yaml = load_yaml(f"custom_format/{format_name}.yml") - compiled_format = compile_format_to_api_structure(format_yaml, self.arr_type) + compiled_format = compile_format_to_api_structure(format_yaml, self.arr_type, patterns) if self.import_as_unique: compiled_format['name'] = self.add_unique_suffix(compiled_format['name']) @@ -72,7 +77,7 @@ class ProfileStrategy(ImportStrategy): for lang_format in language_formats: lang_name = lang_format.get('name', 'Language format') - compiled_lang = compile_format_to_api_structure(lang_format, self.arr_type) + compiled_lang = compile_format_to_api_structure(lang_format, self.arr_type, patterns) if self.import_as_unique: compiled_lang['name'] = self.add_unique_suffix(compiled_lang['name'])