From f8905dfbbf9a35c92ed07d763f901eac0db53134 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 22 Mar 2026 11:53:48 +0000 Subject: [PATCH] Fix handlers.py - keep function-based API for backwards compatibility --- snip/export/handlers.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/snip/export/handlers.py b/snip/export/handlers.py index a0200e9..befd90b 100644 --- a/snip/export/handlers.py +++ b/snip/export/handlers.py @@ -66,7 +66,8 @@ class ExportHandler: replaced = 0 for snippet_data in snippets: - result = self.db.import_snippet(snippet_data, strategy=strategy) + result = self.db.import_snippet( + snippet_data, strategy=strategy) if result is None: skipped += 1 elif result == -1: @@ -89,4 +90,34 @@ class ExportHandler: f" Skipped: {results.get('skipped', 0)}", f" Replaced: {results.get('replaced', 0)}", ] - return "\n".join(lines) \ No newline at end of file + return "\n".join(lines) + + +def export_snippets(snippets: list[dict[str, Any]], file_path: str): + """Export snippets to a JSON file (standalone function).""" + 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 (standalone function).""" + 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 \ No newline at end of file