import os import yaml # Directory containing class files CLASSES_DIR = "/Users/kempersc/apps/glam/schemas/20251121/linkml/modules/classes/" # List of files identified by grep as having "- description" in slots FILES_TO_FIX = [ "StorageConditionPolicy.yaml", "FoundingEvent.yaml", "CustodianTimelineEvent.yaml", "EncompassingBody.yaml", "SocialMediaContent.yaml", "FindingAid.yaml", "BOLDIdentifier.yaml", "SocialMediaPost.yaml", "Taxon.yaml", "ContributingAgency.yaml", "Overview.yaml", "Diocese.yaml", "Jurisdiction.yaml", "FunctionType.yaml", "StaffRole.yaml", "Format.yaml", "Photography.yaml", "WebLink.yaml", "AuditOpinion.yaml", "TradeRegister.yaml", "Expenses.yaml", "Bookplate.yaml", "Publication.yaml", "Laptop.yaml" ] def fix_file(filename): filepath = os.path.join(CLASSES_DIR, filename) if not os.path.exists(filepath): print(f"File not found: {filename}") return try: with open(filepath, 'r') as f: lines = f.readlines() new_lines = [] import_added = False has_description_slot = False for line in lines: # Replace slot usage if line.strip() == "- description": # Check context (simple check if we are in slots section roughly) # Ideally we'd parse YAML but preserving comments/structure is hard with pyyaml # This simple replacement works if indentation matches standard # (which it seems to in this codebase: " - description") new_lines.append(line.replace("- description", "- has_or_had_description")) has_description_slot = True # Update imports elif line.strip() == "- ../slots/description": new_lines.append(line.replace("- ../slots/description", "- ../slots/has_or_had_description")) import_added = True else: new_lines.append(line) # If we replaced the slot but didn't see an explicit import line to replace, # we might need to add the import. # However, purely adding lines is tricky with simple string processing without knowing where 'imports:' ends. # But if 'description' was valid before, it must have been imported (or implicit?). # If it wasn't imported, adding the slot 'has_or_had_description' without import might fail validation # IF LinkML requires explicit import for everything. # BUT many files here seem to have imports. # Let's check if we need to add the import if it wasn't there. # A simple heuristic: find 'imports:' line and append after it if not present. final_lines = [] if has_description_slot and not import_added: # check if has_or_had_description is already imported already_imported = any("has_or_had_description" in l for l in new_lines) if not already_imported: in_imports = False for line in new_lines: final_lines.append(line) if line.strip().startswith("imports:"): final_lines.append(" - ../slots/has_or_had_description\n") import_added = True # Handled else: final_lines = new_lines else: final_lines = new_lines with open(filepath, 'w') as f: f.writelines(final_lines) print(f"Fixed {filename}") except Exception as e: print(f"Error processing {filename}: {e}") def main(): for filename in FILES_TO_FIX: fix_file(filename) if __name__ == "__main__": main()