def _main_process_mp3_dir(base: str, path: str, image_height: int, target_volume: float, **kwargs) -> None: """Main MP3 directory processing function. Args: base: Base path holding directories of MP3s (Artist Directory) path: Sub-directory holding MP3s (Album Directory) image_height: The height that cover.jpg should be resized to. target_volume: Volume, in decibels, mp3gain should adjust a track to. Keyword Args: debug (bool): Enable debug logging when True ignore_folder (bool): Ignore the presence of folder.jpg tags_only (bool): Only correct tags on MP3 files """ full_path = os.path.join(base, path) LOGGER.debug("Processing: %s", full_path) mp3_list = fops.list_files_by_extension(full_path, 'mp3') nfo_data = mp3.album_nfo_from_file(os.path.join(full_path, mp3_list[0])) LOGGER.info(" * %s", nfo_data.get('album').get('title')) if kwargs.get('tags_only'): with fops.pushd(full_path): _main_process_mp3_dir_mp3s_tags_only(mp3_list) return folder_image = image.image_find(full_path, 'folder') if folder_image and not kwargs.get('ignore_folder', False): LOGGER.warning(" ** %s found: Skipping because already processed", folder_image) return cover_image = image.image_find(full_path, 'cover') discart_image = image.image_find(full_path, 'discart') if not cover_image: LOGGER.error(" ** No cover image found: Skipping.") return with fops.pushd(full_path): _main_process_mp3_dir_images(cover_image, folder_image, discart_image, image_height) _main_process_mp3_dir_mp3s(mp3_list, cover_image, target_volume, kwargs.get('debug')) _main_process_mp3_dir_finish(nfo_data)
def test_pushd(self): data = 'pushd test data' path = os.path.join(tempfile.gettempdir(), 'testFile.tmp') with fops.pushd(tempfile.gettempdir()): fops.file_write('testFile.tmp', data) got = fops.file_read(path, True) _cleanup(path) self.assertEqual(data, got)
def compress(comic_book: str, extracted_path: str, file_list: list) -> None: """Compresses images into a new comic book file. Args: comic_book: Full path to the comic book file to create. extracted_path: Path to folder containing files to compress. file_list: List of files to add to the comic book. """ with fops.pushd(extracted_path): with zipfile.ZipFile(comic_book, 'w', compression=zipfile.ZIP_STORED) as zip_file: for file in sorted(file_list): zip_file.write(file)
def image_find(path: str, image_name: str) -> str: """Checks if a image.ext exists in `path` Args: path: Directory to check for image.ext in. image_name: Name of image to look for without extension. Returns: Name of image.ext file if one is found, blank otherwise. """ LOGGER.debug("Searching for %s.ext in: %s", image_name, path) with fops.pushd(path): for image_ext in ('jpg', 'png', 'webp', 'gif'): image_file = f'{image_name}.{image_ext}' if os.path.isfile(image_file): return image_file return ''
def extract(comic: str, path: str, debug: bool = False) -> None: """Extracts a comic file to the specified directory. Args: comic: Full path to comic file. path: Full path to directory to extract comic file to. debug: Enable output of decompression program information. """ with fops.pushd(path): try: subprocess.run(["unar", "-D", comic], capture_output=not debug, check=True) except subprocess.CalledProcessError as error: if error.stderr: LOGGER.error(error.stderr) elif error.stdout: LOGGER.error(error.stdout) raise
def load_projects_folder(path: str, projects_folder: str) -> dict: """Loads the configuration files in the projects_folder Args: path: full path to config file projects_folder: projects folder from configuration file """ if not projects_folder: return dict() full_path = os.path.join(os.path.realpath(os.path.dirname(path)), projects_folder) if projects_folder[:1] == '/': full_path = projects_folder if not os.path.exists(full_path): raise FileNotFoundError('projects_folder defined but does not exist') if not os.path.isdir(full_path): raise IOError('projects_folder defined but not a folder') with fops.pushd(full_path): return load_projects_folder_read()
def child(): with fops.pushd('something_that_should_not_exist'): print('')