1717from perdoo .comic .metadata .metron_info import Id , InformationSource
1818from perdoo .console import CONSOLE
1919from perdoo .services import BaseService , Comicvine , Metron
20- from perdoo .settings import Naming , Output , Service , Services , Settings
20+ from perdoo .settings import SETTINGS , Service
2121from perdoo .utils import (
2222 IssueSearch ,
2323 Search ,
@@ -36,19 +36,19 @@ class SyncOption(str, Enum):
3636 SKIP = "Skip"
3737
3838
39- def get_services (settings : Services ) -> dict [Service , BaseService ]:
39+ def get_services () -> dict [Service , BaseService ]:
4040 output = {}
41- if settings .comicvine .api_key :
42- output [Service .COMICVINE ] = Comicvine (api_key = settings .comicvine .api_key )
43- if settings . metron .username and settings .metron .password :
41+ if SETTINGS . services .comicvine .api_key :
42+ output [Service .COMICVINE ] = Comicvine (api_key = SETTINGS . services .comicvine .api_key )
43+ if SETTINGS . services . metron .username and SETTINGS . services .metron .password :
4444 output [Service .METRON ] = Metron (
45- username = settings . metron .username , password = settings .metron .password
45+ username = SETTINGS . services . metron .username , password = SETTINGS . services .metron .password
4646 )
4747 return output
4848
4949
5050def setup_environment (
51- clean_cache : bool , sync : SyncOption , settings : Settings , debug : bool = False
51+ clean_cache : bool , sync : SyncOption , debug : bool = False
5252) -> tuple [dict [Service , BaseService ], SyncOption ]:
5353 setup_logging (debug = debug )
5454 LOGGER .info ("Python v%s" , python_version ())
@@ -58,7 +58,7 @@ def setup_environment(
5858 LOGGER .info ("Cleaning Cache" )
5959 recursive_delete (path = get_cache_root ())
6060
61- services = get_services (settings = settings . services )
61+ services = get_services ()
6262 if not services and sync is not SyncOption .SKIP :
6363 LOGGER .warning ("No external services configured" )
6464 sync = SyncOption .SKIP
@@ -76,9 +76,9 @@ def load_comics(target: Path) -> list[Comic]:
7676 return comics
7777
7878
79- def prepare_comic (entry : Comic , settings : Settings , skip_convert : bool ) -> bool :
79+ def prepare_comic (entry : Comic , skip_convert : bool ) -> bool :
8080 if not skip_convert :
81- entry .convert_to (settings .output .format )
81+ entry .convert_to (SETTINGS .output .format )
8282 if not entry .archive .IS_WRITEABLE :
8383 LOGGER .warning ("Archive format %s is not writeable" , entry .archive .EXTENSION )
8484 return False
@@ -163,29 +163,23 @@ def sync_metadata(
163163
164164
165165def resolve_metadata (
166- entry : Comic ,
167- session : ArchiveSession ,
168- services : dict [Service , BaseService ],
169- settings : Services ,
170- sync : SyncOption ,
166+ entry : Comic , session : ArchiveSession , services : dict [Service , BaseService ], sync : SyncOption
171167) -> tuple [MetronInfo | None , ComicInfo | None ]:
172168 metron_info , comic_info = entry .read_metadata (session = session )
173169 if not should_sync_metadata (sync = sync , metron_info = metron_info ):
174170 return metron_info , comic_info
175171 search = build_search (
176172 metron_info = metron_info , comic_info = comic_info , filename = entry .filepath .stem
177173 )
178- return sync_metadata (search = search , services = services , service_order = settings .order )
174+ return sync_metadata (search = search , services = services , service_order = SETTINGS . services .order )
179175
180176
181- def generate_naming (
182- settings : Naming , metron_info : MetronInfo | None , comic_info : ComicInfo | None
183- ) -> str | None :
177+ def generate_naming (metron_info : MetronInfo | None , comic_info : ComicInfo | None ) -> str | None :
184178 filepath = None
185179 if metron_info :
186- filepath = metron_info .get_filename (settings = settings )
180+ filepath = metron_info .get_filename ()
187181 if not filepath and comic_info :
188- filepath = comic_info .get_filename (settings = settings )
182+ filepath = comic_info .get_filename ()
189183 return filepath .lstrip ("/" ) if filepath else None
190184
191185
@@ -196,7 +190,6 @@ def apply_changes(
196190 comic_info : ComicInfo | None ,
197191 skip_clean : bool ,
198192 skip_rename : bool ,
199- settings : Output ,
200193) -> str | None :
201194 local_metron_info , local_comic_info = entry .read_metadata (session = session )
202195 if local_metron_info != metron_info :
@@ -212,16 +205,14 @@ def apply_changes(
212205 session .delete (filename = ComicInfo .FILENAME )
213206
214207 if not skip_clean :
215- for extra in entry .list_extras ():
208+ for extra in entry .list_extras (image_extensions = SETTINGS . output . image_extensions ):
216209 session .delete (filename = extra .name )
217210
218211 naming = None
219212 if not skip_rename and (
220- naming := generate_naming (
221- settings = settings .naming , metron_info = metron_info , comic_info = comic_info
222- )
213+ naming := generate_naming (metron_info = metron_info , comic_info = comic_info )
223214 ):
224- images = entry .list_images ()
215+ images = entry .list_images (image_extensions = SETTINGS . output . image_extensions )
225216 stem = Path (naming ).stem
226217 pad = len (str (len (images )))
227218 for idx , img in enumerate (images ):
@@ -268,11 +259,7 @@ def process(
268259 bool , Option ("--debug" , help = "Enable debug mode to show extra information." )
269260 ] = False ,
270261) -> None :
271- settings = Settings .load ()
272- settings .save ()
273- services , sync = setup_environment (
274- clean_cache = clean_cache , sync = sync , settings = settings , debug = debug
275- )
262+ services , sync = setup_environment (clean_cache = clean_cache , sync = sync , debug = debug )
276263
277264 comics = load_comics (target = target )
278265 total = len (comics )
@@ -281,15 +268,11 @@ def process(
281268 f"[{ index } /{ total } ] Importing { entry .filepath .name } " , align = "left" , style = "subtitle"
282269 )
283270
284- if not prepare_comic (entry = entry , settings = settings , skip_convert = skip_convert ):
271+ if not prepare_comic (entry = entry , skip_convert = skip_convert ):
285272 continue
286273 with entry .open_session () as session :
287274 metron_info , comic_info = resolve_metadata (
288- entry = entry ,
289- session = session ,
290- services = services ,
291- settings = settings .services ,
292- sync = sync ,
275+ entry = entry , session = session , services = services , sync = sync
293276 )
294277 naming = apply_changes (
295278 entry = entry ,
@@ -298,10 +281,9 @@ def process(
298281 comic_info = comic_info ,
299282 skip_clean = skip_clean ,
300283 skip_rename = skip_rename ,
301- settings = settings .output ,
302284 )
303285 if naming :
304- entry .move_to (naming = naming , output_folder = settings .output .folder )
286+ entry .move_to (naming = naming , output_folder = SETTINGS .output .folder )
305287 with CONSOLE .status ("Cleaning up empty folders" ):
306288 delete_empty_folders (folder = target )
307289
0 commit comments