66from binascii import crc32
77from pathlib import Path , PurePath
88from time import sleep
9- from tkinter .ttk import Progressbar
109from typing import Callable , Dict , List , Tuple
1110from uuid import UUID , uuid4
1211
1312import click
1413import magic
1514from click ._termui_impl import ProgressBar
1615
16+ from rpgmaker_mv_decoder .callback import Callback
1717from rpgmaker_mv_decoder .exceptions import (
1818 FileFormatError ,
1919 NoValidFilesFound ,
@@ -151,17 +151,16 @@ def __get_likely_key(keys: Dict[str, int], count) -> str:
151151 return main_key
152152
153153
154- def guess_at_key (src : Path , pb_cb : Callable [[ Progressbar ], bool ] = None ) -> str :
154+ def guess_at_key (src : Path , callbacks : Callback = Callback () ) -> str :
155155 """`guess_at_key` Check the path for PNG images and return the decoding key
156156
157157 Finds image files under the specified path and looks for a key to decode all the files.
158158 This can fail if only a small number (less than 3) of the .rpgmvp files are .png images.
159159
160160 Args:
161161 - `src` (`Path`): Path to search for .rpgmvp files
162- - `pb_cb` (`Callable[[click._termui_impl.Progressbar], bool]`, optional): Callback to\
163- display current progress. Call with `None` when bar is complete. Returns `True` if the\
164- user has canceled the operation. Defaults to `None`.
162+ - `callbacks` (`Callback`, optional): Callbacks to use for the UI. Defaults to an empty\
163+ set of callbacks
165164
166165 Raises:
167166 - `NoValidFilesFound`: If no valid PNG images are found
@@ -174,9 +173,8 @@ def guess_at_key(src: Path, pb_cb: Callable[[Progressbar], bool] = None) -> str:
174173 keys : Dict [str , int ] = {}
175174 count : int
176175 with click .progressbar (files , label = "Finding key" ) as all_files :
177- count = _handle_files (pb_cb , keys , all_files )
178- if pb_cb is not None :
179- pb_cb (None )
176+ count = _handle_files (callbacks .progressbar , keys , all_files )
177+ callbacks .progressbar (None )
180178
181179 if count == 0 :
182180 raise NoValidFilesFound (f"No png files found under: '{ Path } '" )
@@ -290,14 +288,12 @@ def get_file_ext(data: bytes) -> str:
290288 return "." + filetype .split ("/" )[- 1 ]
291289
292290
293- # pylint: disable=too-many-arguments
294291def decode_files (
295292 src : str ,
296293 dst : str ,
297294 key : str ,
298295 detect_type : bool ,
299- pb_cb : Callable [[Progressbar ], bool ] = None ,
300- overwrite_callback : Callable [[str ], bool ] = None ,
296+ callbacks : Callback = Callback (),
301297) -> None :
302298 """`decode_files` Decodes the files
303299
@@ -308,12 +304,8 @@ def decode_files(
308304 - `dst` (`str`): Destination directory for output
309305 - `key` (`str`): Key to use for decoding
310306 - `detect_type` (`bool`): If file extensions should be detected from the file contents
311- - `pb_cb` (`Callable[[click._termui_impl.Progressbar], bool]`, optional): Callback to\
312- display current progress. Call with `None` when bar is complete. Returns `True` if the\
313- user has canceled the operation. Defaults to `None`.
314- - `overwrite_callback` (`Callable[[str], bool`, optional): Callback to use if files are\
315- about to be overwritten. Call with the filename in question. `None` indicates user has\
316- canceled the operation. Returns if the file should be overwritten. Defaults to `None`.
307+ - `callbacks` (`Callback`, optional): Callbacks to use for the UI. Defaults to an empty\
308+ set of callbacks
317309 """
318310 # pylint: disable=too-many-locals
319311 source_dir = Path (src ).resolve ()
@@ -328,7 +320,7 @@ def decode_files(
328320 with click .progressbar (files , label = "Decoding files" ) as all_files :
329321 filename : Path
330322 for filename in all_files :
331- if pb_cb and pb_cb (all_files ):
323+ if callbacks . progressbar (all_files ):
332324 break
333325 output_file : PurePath = destination .joinpath (PurePath (filename ).relative_to (source ))
334326 result : bytes
@@ -352,18 +344,16 @@ def decode_files(
352344 continue
353345 else :
354346 output_file = _get_std_ext (output_file )
355- if not _save_file (output_file , result , overwrite_callback ):
347+ if not _save_file (output_file , result , callbacks . overwrite ):
356348 break
357- if pb_cb :
358- pb_cb (None )
349+ callbacks .progressbar (None )
359350
360351
361352def encode_files (
362353 src : str ,
363354 dst : str ,
364355 key : str ,
365- pb_cb : Callable [[Progressbar ], bool ] = None ,
366- overwrite_callback : Callable [[str ], bool ] = None ,
356+ callbacks : Callback = Callback (),
367357) -> None :
368358 """`encode_files` Encodes the files
369359
@@ -373,12 +363,8 @@ def encode_files(
373363 - `src` (`str`): Source directory to search
374364 - `dst` (`str`): Destination directory for output
375365 - `key` (`str`): Key to use for encoding
376- - `pb_cb` (`Callable[[click._termui_impl.Progressbar], bool]`, optional): Callback to\
377- display current progress. Call with `None` when bar is complete. Returns `True` if the\
378- user has canceled the operation. Defaults to `None`.
379- - `overwrite_callback` (`Callable[[str], bool`, optional): Callback to use if files are\
380- about to be overwritten. Call with the filename in question. `None` indicates user has\
381- canceled the operation. Returns if the file should be overwritten. Defaults to `None`.
366+ - `callbacks` (`Callback`, optional): Callbacks to use for the UI. Defaults to an empty\
367+ set of callbacks
382368 """
383369 # pylint: disable=too-many-locals
384370 source_dir = Path (src ).resolve ()
@@ -393,9 +379,8 @@ def encode_files(
393379 with click .progressbar (files , label = "Encoding files" ) as all_files :
394380 filename : Path
395381 for filename in all_files :
396- if pb_cb is not None :
397- if pb_cb (all_files ):
398- break
382+ if callbacks .progress (all_files ):
383+ break
399384 output_file : PurePath = destination .joinpath (PurePath (filename ).relative_to (source ))
400385 filetype : str
401386 with click .open_file (filename , "rb" ) as file :
@@ -408,10 +393,9 @@ def encode_files(
408393 output_file = output_file .with_suffix (".rpgmvp" )
409394 elif filetype .startswith ("audio" ):
410395 output_file = output_file .with_suffix (".rpgmvp" )
411- if not _save_file (output_file , data , overwrite_callback ):
396+ if not _save_file (output_file , data , callbacks . overwrite ):
412397 break
413- if pb_cb is not None :
414- pb_cb (None )
398+ callbacks .progressbar (None )
415399
416400
417401def _get_std_ext (output_file ):
0 commit comments