Skip to main content

Add support for longer snippets from a file

ID
240b2a4
date
2024-04-09 09:25:24+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
1ad5130
message
Add support for longer snippets from a file
changed files
2 files, 48 additions, 16 deletions

Changed files

create_snippets.py (1613) → create_snippets.py (2090)

diff --git a/create_snippets.py b/create_snippets.py
index 8375ff7..925e536 100755
--- a/create_snippets.py
+++ b/create_snippets.py
@@ -36,6 +36,40 @@ SNIPPETS = {
 # fmt: on
 
 
+
+# fmt: off
+LONGER_SNIPPETS = {
+    # ==============================
+    # Programming: fragments of code
+    # ==============================
+    "!flapi": "flapi.py",
+}
+# fmt: on
+
+
+def add_snippet(zf: zipfile.ZipFile, shortcut: str, expansion: str):
+    """
+    Add a single snippet to a snippets bundle.
+    """
+    h = hashlib.md5()
+    h.update(shortcut.encode("utf8"))
+    h.update(expansion.encode("utf8"))
+
+    snippet_id = uuid.UUID(hex=h.hexdigest())
+
+    snippet_data = {
+        "alfredsnippet": {
+            "snippet": expansion,
+            "uid": str(snippet_id),
+            "name": "",
+            "keyword": shortcut,
+        }
+    }
+
+    zf.writestr(f"{snippet_id}.json", data=json.dumps(snippet_data))
+
+
+
 if __name__ == "__main__":
     curdir = pathlib.Path(os.getcwd()).absolute()
 
@@ -43,21 +77,12 @@ if __name__ == "__main__":
         zf.write("info.plist")
 
         for shortcut, expansion in SNIPPETS.items():
-            h = hashlib.md5()
-            h.update(shortcut.encode("utf8"))
-            h.update(expansion.encode("utf8"))
-
-            snippet_id = uuid.UUID(hex=h.hexdigest())
-
-            snippet_data = {
-                "alfredsnippet": {
-                    "snippet": expansion,
-                    "uid": str(snippet_id),
-                    "name": "",
-                    "keyword": shortcut,
-                }
-            }
-
-            zf.writestr(f"{snippet_id}.json", data=json.dumps(snippet_data))
+            add_snippet(zf, shortcut, expansion)
+
+        for shortcut, filename in LONGER_SNIPPETS.items():
+            with open(pathlib.Path("expansions") / filename) as f:
+                expansion = f.read()
+
+            add_snippet(zf, shortcut, expansion)
 
     subprocess.check_call(["open", "Alex’s snippets.alfredsnippets"])

expansions/flapi.py (0) → expansions/flapi.py (211)

diff --git a/expansions/flapi.py b/expansions/flapi.py
new file mode 100644
index 0000000..e3633c3
--- /dev/null
+++ b/expansions/flapi.py
@@ -0,0 +1,7 @@
+from flickr_photos_api import FlickrPhotosApi
+import keyring
+
+api = FlickrPhotosApi(
+    api_key=keyring.get_password("flickr_api", "key"),
+    user_agent="Alex Chan's personal scripts <alex@alexwlchan.net>",
+)