104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
COMPLETE MASS RENAME - VSE slike (root + podmape)
|
|
Preprosta angleška imena, brez podčrtajev
|
|
"""
|
|
|
|
import re
|
|
from pathlib import Path
|
|
from collections import defaultdict
|
|
|
|
REFERENCA_DIR = Path("/Users/davidkotnik/repos/novafarma/assets/slike/glavna_referenca")
|
|
|
|
def simplify_name(filepath):
|
|
"""Naredi preprosto ime iz dolgega imena"""
|
|
name = filepath.stem.lower()
|
|
|
|
# Odstrani prefixe
|
|
name = re.sub(r'^(src_|assets_|moje_slike_koncna_|library|sprites|references|phases)', '', name)
|
|
name = re.sub(r'(godot|library|references|sprites|phases|source|backup)', '', name)
|
|
name = re.sub(r'_+', ' ', name) # Replace _ with space
|
|
name = re.sub(r'\d{10,}', '', name) # Remove timestamps
|
|
name = re.sub(r'\s+', ' ', name) # Multiple spaces to one
|
|
|
|
# Vzami prvo smiselno besedo
|
|
words = name.strip().split()
|
|
if words:
|
|
# Izberi prvo pomembno besedo
|
|
for word in words:
|
|
if len(word) > 3 and word not in ['moje', 'slike', 'koncna', 'ostalo', 'style']:
|
|
return word
|
|
return words[0] if words else 'file'
|
|
|
|
return 'file'
|
|
|
|
def execute_complete_rename():
|
|
"""Izvede rename na VSEH slikah (root + podmape)"""
|
|
print("🔄 ZAČENJAM KOMPLETNI MASS RENAME...\n")
|
|
|
|
# Najdi VSE slike (rekurzivno)
|
|
all_images = []
|
|
for ext in ['*.png', '*.jpg', '*.jpeg']:
|
|
all_images.extend(REFERENCA_DIR.rglob(ext))
|
|
|
|
print(f"Najdenih: {len(all_images)} slik\n")
|
|
|
|
# Organiziraj po folder + kategorija
|
|
rename_map = {}
|
|
folder_counts = defaultdict(lambda: defaultdict(int))
|
|
|
|
for filepath in all_images:
|
|
folder = filepath.parent
|
|
category = simplify_name(filepath)
|
|
|
|
# Increment counter
|
|
folder_counts[folder][category] += 1
|
|
count = folder_counts[folder][category]
|
|
|
|
# Generate new name
|
|
if count == 1:
|
|
new_name = f"{category}{filepath.suffix}"
|
|
else:
|
|
new_name = f"{category}{count}{filepath.suffix}"
|
|
|
|
new_path = folder / new_name
|
|
rename_map[filepath] = new_path
|
|
|
|
# Execute rename
|
|
renamed = 0
|
|
errors = []
|
|
|
|
for old_path, new_path in rename_map.items():
|
|
if old_path == new_path:
|
|
continue # Already has good name
|
|
|
|
if new_path.exists():
|
|
errors.append(f"Already exists: {new_path.name}")
|
|
continue
|
|
|
|
try:
|
|
old_path.rename(new_path)
|
|
renamed += 1
|
|
|
|
if renamed % 100 == 0:
|
|
print(f" ✅ {renamed} preimenovanih...")
|
|
except Exception as e:
|
|
errors.append(f"Error: {old_path.name} → {e}")
|
|
|
|
# Report
|
|
print("\n" + "=" * 80)
|
|
print(" KONČNO POROČILO")
|
|
print("=" * 80)
|
|
print(f"✅ Uspešno preimenovanih: {renamed}")
|
|
print(f"⚠️ Že OK / preskočenih: {len(all_images) - renamed - len(errors)}")
|
|
print(f"❌ Napak: {len(errors)}")
|
|
print("=" * 80)
|
|
|
|
if errors and len(errors) <= 20:
|
|
print("\n⚠️ NAPAKE:")
|
|
for err in errors[:20]:
|
|
print(f" {err}")
|
|
|
|
if __name__ == "__main__":
|
|
execute_complete_rename()
|