Skip to content

Commit f8fac8e

Browse files
author
Patrick Bareiss
committed
testing
1 parent 6432baa commit f8fac8e

File tree

1 file changed

+95
-5
lines changed

1 file changed

+95
-5
lines changed

bin/rename_data.py

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
- Another yml file with a different name
88
99
The script will:
10-
1. Remove the other yml file first
11-
2. Rename data.yml to match the folder name (folder_name.yml)
10+
1. Copy metadata (author, id, date, description) from the other yml file to data.yml
11+
2. Remove the other yml file
12+
3. Rename data.yml to match the folder name (folder_name.yml)
1213
"""
1314

1415
import argparse
1516
import logging
17+
import yaml
1618
from pathlib import Path
17-
from typing import List, Tuple, Optional
19+
from typing import List, Tuple, Optional, Dict, Any
1820

1921

2022
def setup_logging(verbose: bool = False) -> None:
@@ -36,6 +38,69 @@ def find_yml_files(directory: Path) -> List[Path]:
3638
return yml_files
3739

3840

41+
def load_yaml_file(file_path: Path) -> Optional[Dict[str, Any]]:
42+
"""
43+
Load and parse a YAML file.
44+
45+
Returns:
46+
Dictionary containing the YAML data, or None if there was an error
47+
"""
48+
logger = logging.getLogger(__name__)
49+
try:
50+
with open(file_path, 'r', encoding='utf-8') as f:
51+
return yaml.safe_load(f)
52+
except Exception as e:
53+
logger.error(f"Error loading YAML file {file_path}: {e}")
54+
return None
55+
56+
57+
def save_yaml_file(file_path: Path, data: Dict[str, Any]) -> bool:
58+
"""
59+
Save data to a YAML file.
60+
61+
Returns:
62+
True if successful, False otherwise
63+
"""
64+
logger = logging.getLogger(__name__)
65+
try:
66+
with open(file_path, 'w', encoding='utf-8') as f:
67+
yaml.dump(data, f, default_flow_style=False, sort_keys=False,
68+
allow_unicode=True)
69+
return True
70+
except Exception as e:
71+
logger.error(f"Error saving YAML file {file_path}: {e}")
72+
return False
73+
74+
75+
def copy_metadata_fields(source_data: Dict[str, Any], target_data: Dict[str, Any]) \
76+
-> Dict[str, Any]:
77+
"""
78+
Copy metadata fields (author, id, date, description) from source to target.
79+
80+
Args:
81+
source_data: YAML data from the other yml file
82+
target_data: YAML data from data.yml file
83+
84+
Returns:
85+
Updated target data with copied metadata
86+
"""
87+
logger = logging.getLogger(__name__)
88+
metadata_fields = ['author', 'id', 'date', 'description']
89+
90+
updated_data = target_data.copy()
91+
92+
for field in metadata_fields:
93+
if field in source_data:
94+
old_value = updated_data.get(field, 'N/A')
95+
new_value = source_data[field]
96+
logger.info(f"Copying {field}: '{old_value}' -> '{new_value}'")
97+
updated_data[field] = new_value
98+
else:
99+
logger.warning(f"Field '{field}' not found in source file")
100+
101+
return updated_data
102+
103+
39104
def analyze_directory(directory: Path) -> Optional[Tuple[Path, List[Path]]]:
40105
"""
41106
Analyze a directory for data.yml and other yml files.
@@ -91,15 +156,40 @@ def process_directory(directory: Path, dry_run: bool = False) -> bool:
91156
logger.info(f"Found other yml files: {[str(f) for f in other_yml_files]}")
92157

93158
try:
94-
# Step 1: Remove other yml files
159+
# Step 1: Copy metadata from other yml files to data.yml
160+
data_yml_content = load_yaml_file(data_yml)
161+
if not data_yml_content:
162+
logger.error(f"Failed to load data.yml: {data_yml}")
163+
return False
164+
165+
# Process each other yml file and copy metadata
166+
for other_yml in other_yml_files:
167+
other_yml_content = load_yaml_file(other_yml)
168+
if other_yml_content:
169+
logger.info(f"Copying metadata from {other_yml.name} to data.yml")
170+
data_yml_content = copy_metadata_fields(
171+
other_yml_content, data_yml_content)
172+
else:
173+
logger.warning(f"Failed to load other yml file: {other_yml}")
174+
175+
# Save the updated data.yml
176+
if dry_run:
177+
logger.info("[DRY RUN] Would update data.yml with copied metadata")
178+
else:
179+
logger.info("Updating data.yml with copied metadata")
180+
if not save_yaml_file(data_yml, data_yml_content):
181+
logger.error(f"Failed to save updated data.yml: {data_yml}")
182+
return False
183+
184+
# Step 2: Remove other yml files
95185
for other_yml in other_yml_files:
96186
if dry_run:
97187
logger.info(f"[DRY RUN] Would remove: {other_yml}")
98188
else:
99189
logger.info(f"Removing: {other_yml}")
100190
other_yml.unlink()
101191

102-
# Step 2: Rename data.yml to folder name
192+
# Step 3: Rename data.yml to folder name
103193
if dry_run:
104194
logger.info(f"[DRY RUN] Would rename {data_yml} to {new_yml_path}")
105195
else:

0 commit comments

Comments
 (0)