toolkit / utils /nocap.py
k4d3's picture
refactor stuff
5db1908
raw
history blame
3.12 kB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# 画像ファイルと.captionファイルの両方を含むサブフォルダを確認する
# Check for subfolders containing both image files and .caption files
import os
import sys
from pathlib import Path
import gettext
import locale
import logging
from utils.file_processor import FileProcessor, ProcessorOptions
from collections import Counter
from dataclasses import dataclass
from typing import Dict, List, Tuple
# Set up i18n
def setup_i18n():
"""Set up internationalization"""
try:
locale.setlocale(locale.LC_ALL, '')
current_locale = locale.getlocale()[0]
locale_path = Path(__file__).parent / 'locales'
trans = gettext.translation('nocap', locale_path, languages=[current_locale])
trans.install()
return trans.gettext
except:
return gettext.gettext
# Initialize translation
_ = setup_i18n()
# ANSI color codes
RED = "\033[91m"
GREEN = "\033[92m"
ORANGE = "\033[93m"
RESET = "\033[0m"
IMAGE_EXTENSIONS = {"jxl", "png", "jpg", "jpeg", "webp"}
CAPTION_EXTENSION = "caption"
def setup_logging(debug=False):
if debug:
logging.basicConfig(level=logging.DEBUG, filename='nocap_debug.log', filemode='w',
format='%(asctime)s - %(levelname)s - %(message)s')
else:
logging.basicConfig(level=logging.INFO)
@dataclass
class DirectoryStats:
jxl: int = 0
png: int = 0
webp: int = 0
jpg: int = 0
caption: int = 0
class DirectoryProcessor(FileProcessor):
def __init__(self, options: ProcessorOptions):
super().__init__(options)
self.stats = DirectoryStats()
def process_content(self, content: str) -> str:
# This processor doesn't modify content, it just counts files
ext = Path(self.current_file).suffix[1:].lower()
if ext == "jxl":
self.stats.jxl += 1
elif ext == "png":
self.stats.png += 1
elif ext == "webp":
self.stats.webp += 1
elif ext in ("jpg", "jpeg"):
self.stats.jpg += 1
elif ext == "caption":
self.stats.caption += 1
return content
def main():
debug = "--debug" in sys.argv
options = ProcessorOptions(
recursive=True,
dry_run=True, # We're just counting, not modifying
debug=debug,
file_extensions={'.jxl', '.png', '.webp', '.jpg', '.jpeg', '.caption'}
)
current_directory = Path.cwd()
matching_dirs = []
for directory in (d for d in current_directory.iterdir() if d.is_dir()):
processor = DirectoryProcessor(options)
processor.process_directory(directory)
if processor.stats.caption > 0:
matching_dirs.append((
directory.name,
processor.stats.jxl,
processor.stats.png,
processor.stats.webp,
processor.stats.jpg,
processor.stats.caption
))
# Process results as needed...
if __name__ == "__main__":
main()