glam/schemas/20251121/linkml/modules/migrate_was_slots.py
kempersc 37d923cae1 Refactor slot names and update imports for consistency
- Migrated `was_generated_by` to `is_or_was_generated_by` and `was_derived_from` to `is_or_was_derived_from` across multiple YAML schema files as per Rule 53.
- Updated relevant imports, slot lists, and slot usage keys to reflect the new naming conventions.
- Added migration comments for clarity and tracking.
- Introduced a migration script to automate the changes across all affected files.
2026-01-15 15:07:53 +01:00

133 lines
4.2 KiB
Python

#!/usr/bin/env python3
"""
Batch migration script for was_generated_by and was_derived_from slots.
Migrations per slot_fixes.yaml Rule 53:
- was_generated_by → is_or_was_generated_by
- was_derived_from → is_or_was_derived_from
This script:
1. Updates imports
2. Updates slots lists
3. Updates slot_usage keys
4. Adds migration comments
5. Preserves existing range specifications in slot_usage
"""
import os
import re
from datetime import datetime
TIMESTAMP = datetime.now().strftime("%Y-%m-%d")
MIGRATION_COMMENT = f"# MIGRATED {TIMESTAMP}: was_generated_by→is_or_was_generated_by, was_derived_from→is_or_was_derived_from per Rule 53"
def migrate_file(filepath):
"""Migrate a single class file."""
with open(filepath, 'r') as f:
content = f.read()
original = content
changes = []
# 1. Update imports: ../slots/was_generated_by → ../slots/is_or_was_generated_by
if '../slots/was_generated_by' in content:
content = content.replace(
'- ../slots/was_generated_by',
'- ../slots/is_or_was_generated_by # was: was_generated_by - migrated per Rule 53'
)
changes.append('import: was_generated_by → is_or_was_generated_by')
if '../slots/was_derived_from' in content:
content = content.replace(
'- ../slots/was_derived_from',
'- ../slots/is_or_was_derived_from # was: was_derived_from - migrated per Rule 53'
)
changes.append('import: was_derived_from → is_or_was_derived_from')
# 2. Update slots lists (under "slots:" in class definition)
# Match " - was_generated_by" at proper indentation in slots list
content = re.sub(
r'^(\s+)- was_generated_by\s*$',
r'\1- is_or_was_generated_by # was: was_generated_by - migrated per Rule 53',
content,
flags=re.MULTILINE
)
content = re.sub(
r'^(\s+)- was_derived_from\s*$',
r'\1- is_or_was_derived_from # was: was_derived_from - migrated per Rule 53',
content,
flags=re.MULTILINE
)
# 3. Update slot_usage keys
# Match " was_generated_by:" at slot_usage level
content = re.sub(
r'^(\s+)was_generated_by:',
r'\1is_or_was_generated_by: # was: was_generated_by - migrated per Rule 53',
content,
flags=re.MULTILINE
)
content = re.sub(
r'^(\s+)was_derived_from:',
r'\1is_or_was_derived_from: # was: was_derived_from - migrated per Rule 53',
content,
flags=re.MULTILINE
)
# 4. Update in examples and descriptions (but not the slot names themselves)
# Leave descriptions and examples mentioning the old name for now
if content != original:
changes.append('slots list and slot_usage updated')
with open(filepath, 'w') as f:
f.write(content)
return True, changes
return False, []
def main():
class_dir = "classes"
migrated = []
skipped = []
for filename in sorted(os.listdir(class_dir)):
if not filename.endswith('.yaml'):
continue
filepath = os.path.join(class_dir, filename)
with open(filepath) as f:
content = f.read()
# Check if needs migration (has old slots but not new ones already)
needs_migration = False
if 'was_generated_by' in content and 'is_or_was_generated_by' not in content:
needs_migration = True
if 'was_derived_from' in content and 'is_or_was_derived_from' not in content:
needs_migration = True
if not needs_migration:
continue
success, changes = migrate_file(filepath)
if success:
migrated.append((filename, changes))
print(f"✓ Migrated: {filename}")
else:
skipped.append(filename)
print(f"- Skipped: {filename}")
print(f"\n=== Migration Summary ===")
print(f"Migrated: {len(migrated)} files")
print(f"Skipped: {len(skipped)} files")
if migrated:
print(f"\nMigrated files:")
for f, changes in migrated:
print(f" - {f}: {', '.join(changes)}")
if __name__ == "__main__":
main()