Giving names and populating the ods current_eval file
This commit is contained in:
+61
-37
@@ -1,23 +1,20 @@
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import shutil
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python rename_copies.py <directory_path>")
|
||||
print("Usage: python giving_names.py <directory_path>")
|
||||
sys.exit(1)
|
||||
|
||||
work_dir = sys.argv[1]
|
||||
target_subdir = os.path.join(work_dir, "Copies annotées")
|
||||
work_dir = os.path.abspath(sys.argv[1])
|
||||
target_subdir = os.path.join(work_dir, "A Rendre")
|
||||
|
||||
os.makedirs(target_subdir, exist_ok=True)
|
||||
|
||||
pattern = re.compile(r"^Copie(\d+)\.json$")
|
||||
|
||||
# Store data: name -> list of (copie_id, source_folder)
|
||||
copies_map = defaultdict(list)
|
||||
|
||||
# 1. Collect all data
|
||||
@@ -26,40 +23,67 @@ def main():
|
||||
if match:
|
||||
copie_id = match.group(1)
|
||||
json_path = os.path.join(work_dir, filename)
|
||||
source_folder = os.path.join(work_dir, f"Anot_Copie{copie_id}")
|
||||
|
||||
if os.path.isdir(source_folder):
|
||||
try:
|
||||
with open(json_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
name = data.get("name", "Unknown").strip()
|
||||
copies_map[name].append((copie_id, source_folder))
|
||||
except Exception as e:
|
||||
print(f"Error processing {filename}: {e}")
|
||||
|
||||
# 2. Check constraints and move files
|
||||
for name, entries in copies_map.items():
|
||||
# Alert if name is Unknown
|
||||
if name == "Unknown":
|
||||
ids = [e[0] for e in entries]
|
||||
print(f"ALERT: 'Unknown' name found for IDs: {', '.join(ids)}")
|
||||
|
||||
# Alert if duplicates (same name, multiple IDs)
|
||||
elif len(entries) > 1:
|
||||
ids = [e[0] for e in entries]
|
||||
print(f"ALERT: Name '{name}' assigned to multiple IDs: {', '.join(ids)}")
|
||||
|
||||
# Perform move
|
||||
safe_name = re.sub(r'[<>:"/\\|?*]', '', name)
|
||||
for copie_id, source_folder in entries:
|
||||
new_folder_name = f"{safe_name} ({copie_id})"
|
||||
dest_path = os.path.join(target_subdir, new_folder_name)
|
||||
|
||||
try:
|
||||
print(f"Moving '{source_folder}' -> '{dest_path}'")
|
||||
shutil.move(source_folder, dest_path)
|
||||
with open(json_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
name = data.get("name", "Unknown").strip()
|
||||
copies_map[name].append(copie_id)
|
||||
except Exception as e:
|
||||
print(f"Error moving {source_folder}: {e}")
|
||||
print(f"Error processing {filename}: {e}")
|
||||
|
||||
# 2. Check constraints and symlink files
|
||||
for name, ids in copies_map.items():
|
||||
if name == "Unknown":
|
||||
print(f"ALERT: 'Unknown' name found for IDs: {', '.join(ids)}")
|
||||
elif len(ids) > 1:
|
||||
print(f"ALERT: Name '{name}' assigned to multiple IDs: {', '.join(ids)}")
|
||||
|
||||
safe_name = re.sub(r'[<>:"/\\|?*]', '', name).strip()
|
||||
|
||||
for copie_id in ids:
|
||||
path_b = os.path.join(work_dir, f"Bnot/Copie{copie_id}")
|
||||
path_a = os.path.join(work_dir, f"Anot/Copie{copie_id}")
|
||||
|
||||
# Determine source: must contain both files
|
||||
source_folder = None
|
||||
|
||||
# Check Bnot first
|
||||
if (os.path.exists(os.path.join(path_b, "Concat.jpg")) and
|
||||
os.path.exists(os.path.join(path_b, "score.json"))):
|
||||
source_folder = path_b
|
||||
# Fallback to Anot
|
||||
elif (os.path.exists(os.path.join(path_a, "Concat.jpg")) and
|
||||
os.path.exists(os.path.join(path_a, "score.json"))):
|
||||
source_folder = path_a
|
||||
|
||||
if not source_folder:
|
||||
print(f"Skipping ID {copie_id}: Files missing in both Anot and Bnot.")
|
||||
continue
|
||||
|
||||
# Create destination directory
|
||||
dest_folder_name = safe_name if len(ids) == 1 else f"{safe_name} ({copie_id})"
|
||||
dest_path = os.path.join(target_subdir, dest_folder_name)
|
||||
os.makedirs(dest_path, exist_ok=True)
|
||||
|
||||
print(f"Linking '{source_folder}' -> '{dest_path}'")
|
||||
|
||||
# Link configuration: (source_filename, dest_filename)
|
||||
links = [
|
||||
("Concat.jpg", f"{safe_name}.jpg"),
|
||||
("score.json", "score.json")
|
||||
]
|
||||
|
||||
for src_name, dst_name in links:
|
||||
src_file = os.path.join(source_folder, src_name)
|
||||
dst_link = os.path.join(dest_path, dst_name)
|
||||
|
||||
try:
|
||||
if os.path.lexists(dst_link):
|
||||
os.remove(dst_link)
|
||||
os.symlink(src_file, dst_link)
|
||||
except Exception as e:
|
||||
print(f"Error linking {src_name} for {dest_folder_name}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user