glam/populate_01_imports.py
kempersc fc405445c6 Refactor and update schema definitions
- Removed obsolete slots: `has_or_had_custodian_observation`, `provider`, and `specificity_annotation`.
- Updated `has_or_had_score` slot to use `SpecificityScore` class and modified its description and examples.
- Added new slots: `end_seconds`, `end_time`, `has_archive_path`, `has_or_had_custodian_name`, `protocol_name`, and `protocol_version`.
- Introduced a script `check_annotation_types.py` to validate the presence and structure of `custodian_types` in YAML files.
- Added a script `update_specificity.py` to automate updates related to `SpecificityAnnotation` to `SpecificityScore`.
2026-02-01 19:55:38 +01:00

79 lines
2.6 KiB
Python

import os
def populate_imports(main_schema_path, classes_dir):
with open(main_schema_path, 'r') as f:
lines = f.readlines()
# Find start and end of imports section
start_idx = -1
end_idx = -1
for i, line in enumerate(lines):
if line.strip() == "imports:":
start_idx = i
break
if start_idx == -1:
print("imports: section not found")
return
# Find where imports block ends (next top-level key or end of file)
for i in range(start_idx + 1, len(lines)):
line = lines[i]
if line.strip() and not line.startswith(" ") and not line.startswith("#"):
end_idx = i
break
if end_idx == -1:
end_idx = len(lines)
# Keep existing non-class imports (slots, enums, metadata, types)
# Filter out modules/classes/* imports
preserved_imports = []
for i in range(start_idx + 1, end_idx):
line = lines[i]
stripped = line.strip()
if stripped.startswith("- modules/classes/"):
continue # We will regenerate these
if stripped.startswith("- ./"): # Should not happen in 01_...
continue
if stripped.strip():
preserved_imports.append(line)
# Collect all class files
class_imports = []
for root, dirs, files in os.walk(classes_dir):
if 'archive' in dirs:
dirs.remove('archive')
if 'deprecated' in dirs:
dirs.remove('deprecated')
if 'archive' in root.split(os.sep):
continue
if 'deprecated' in root.split(os.sep):
continue
for file in files:
if file.endswith(".yaml"):
# Path relative to 01_... location (schemas/20251121/linkml)
# classes_dir is schemas/20251121/linkml/modules/classes
# relative path: modules/classes/Filename
# Check for duplicate base names? e.g. .yaml vs .yaml.bak?
# assume clean dir.
rel_path = f"modules/classes/{file.replace('.yaml', '')}"
class_imports.append(f" - {rel_path}\n")
class_imports.sort()
# Reassemble file
new_lines = lines[:start_idx + 1] + preserved_imports + class_imports + lines[end_idx:]
with open(main_schema_path, 'w') as f:
f.writelines(new_lines)
print(f"Updated {main_schema_path} with {len(class_imports)} class imports.")
populate_imports("schemas/20251121/linkml/01_custodian_name_modular.yaml", "schemas/20251121/linkml/modules/classes")