File size: 3,118 Bytes
53b8615
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/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()