Initial upload: snippet-manager with CI/CD workflow
This commit is contained in:
38
snip/export/handlers.py
Normal file
38
snip/export/handlers.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
"""JSON import/export handlers for snippets."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from snip.db.database import Database
|
||||||
|
|
||||||
|
|
||||||
|
def export_snippets(snippets: list[dict[str, Any]], file_path: str):
|
||||||
|
"""Export snippets to a JSON file."""
|
||||||
|
export_data = {
|
||||||
|
"version": "1.0",
|
||||||
|
"exported_at": datetime.utcnow().isoformat() + "Z",
|
||||||
|
"snippets": snippets,
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(file_path, "w") as f:
|
||||||
|
json.dump(export_data, f, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def import_snippets(db: Database, file_path: str, strategy: str = "skip") -> tuple[int, int]:
|
||||||
|
"""Import snippets from a JSON file."""
|
||||||
|
with open(file_path, "r") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
snippets = data.get("snippets", [])
|
||||||
|
imported = 0
|
||||||
|
skipped = 0
|
||||||
|
|
||||||
|
for snippet_data in snippets:
|
||||||
|
result = db.import_snippet(snippet_data, strategy=strategy)
|
||||||
|
if result is None:
|
||||||
|
skipped += 1
|
||||||
|
else:
|
||||||
|
imported += 1
|
||||||
|
|
||||||
|
return imported, skipped
|
||||||
Reference in New Issue
Block a user