Spaces:
Sleeping
Sleeping
#!/usr/bin/env python3 | |
""" | |
Script for managing SFOSR concepts (export/import). | |
""" | |
import argparse | |
import sys | |
import os | |
# Ensure the script can find the sfosr_core module | |
script_dir = os.path.dirname(os.path.abspath(__file__)) | |
project_root = script_dir # Assumes script is in the root | |
sys.path.insert(0, project_root) | |
try: | |
from sfosr_core.sfosr_database import SFOSRDatabase | |
except ImportError as e: | |
print(f"Error: Could not import SFOSRDatabase. Make sure sfosr_core is accessible.") | |
print(f"PYTHONPATH: {sys.path}") | |
sys.exit(1) | |
DEFAULT_DB_PATH = "sfosr.db" | |
DEFAULT_JSON_PATH_EXPORT = "concepts_export.json" # Default for export | |
DEFAULT_JSON_PATH_IMPORT = "concepts_to_import.json" # Default for import | |
def main(): | |
parser = argparse.ArgumentParser(description="Manage SFOSR concepts.") | |
subparsers = parser.add_subparsers(dest='action', help='Action to perform', required=True) | |
# Export command | |
parser_export = subparsers.add_parser('export', help='Export concepts to JSON') | |
parser_export.add_argument( | |
'--db-path', | |
default=DEFAULT_DB_PATH, | |
help=f"Path to the SFOSR database file (default: {DEFAULT_DB_PATH})" | |
) | |
parser_export.add_argument( | |
'--json-path', | |
default=DEFAULT_JSON_PATH_EXPORT, | |
help=f"Path to the output JSON file (default: {DEFAULT_JSON_PATH_EXPORT})" | |
) | |
# Import command | |
parser_import = subparsers.add_parser('import', help='Import concepts from JSON (adds only new concepts)') | |
parser_import.add_argument( | |
'--db-path', | |
default=DEFAULT_DB_PATH, | |
help=f"Path to the SFOSR database file (default: {DEFAULT_DB_PATH})" | |
) | |
parser_import.add_argument( | |
'--json-path', | |
default=DEFAULT_JSON_PATH_IMPORT, | |
help=f"Path to the input JSON file (default: {DEFAULT_JSON_PATH_IMPORT})" | |
) | |
args = parser.parse_args() | |
if not os.path.exists(args.db_path): | |
print(f"Error: Database file not found at {args.db_path}") | |
sys.exit(1) | |
db = SFOSRDatabase(db_path=args.db_path) | |
if args.action == 'export': | |
print(f"Exporting concepts from {args.db_path} to {args.json_path}...") | |
db.export_concepts_to_json(args.json_path) | |
elif args.action == 'import': | |
if not os.path.exists(args.json_path): | |
print(f"Error: Import JSON file not found at {args.json_path}") | |
sys.exit(1) | |
print(f"Importing concepts from {args.json_path} to {args.db_path}...") | |
try: | |
added, skipped = db.import_concepts_from_json(args.json_path) | |
# Optional: Add more feedback based on counts | |
except AttributeError: | |
print(f"Error: import_concepts_from_json method not found in SFOSRDatabase class in {db.__class__.__module__}.") | |
sys.exit(1) | |
except Exception as e: | |
print(f"An unexpected error occurred during import: {e}") | |
import traceback | |
traceback.print_exc() # Print stack trace for unexpected errors | |
sys.exit(1) | |
if __name__ == "__main__": | |
main() |