From 010d54bc87c75ab8991e4fb3b6d764c439df5df1 Mon Sep 17 00:00:00 2001 From: simonwt Date: Wed, 1 Apr 2026 22:14:57 +0100 Subject: [PATCH] get_parent_pksk_from_path utils function --- utils/__init__.py | 1 + utils/get_parent_pksk_from_path.py | 14 ++++++++++++++ wrappers/post/create_post.py | 18 +++--------------- 3 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/get_parent_pksk_from_path.py diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e8a61ad --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1 @@ +from .get_parent_pksk_from_path import get_parent_pksk_from_path \ No newline at end of file diff --git a/utils/get_parent_pksk_from_path.py b/utils/get_parent_pksk_from_path.py new file mode 100644 index 0000000..3a53842 --- /dev/null +++ b/utils/get_parent_pksk_from_path.py @@ -0,0 +1,14 @@ +def get_parent_pksk_from_path(parent_path): + if parent_path == '/': + return 'maintrunk#maintrunk' + + entity, slug = parent_path.strip('/').split('/') + if entity == 'branch': + entity = 'subwiki' + elif entity != 'user': + entity = 'userprofile' + + if entity not in ['userprofile', 'subwiki']: + raise ValueError(f"Invalid parent entity: {entity}. Must be 'userprofile' or 'branch'.") + + return f"{entity}#{slug}" \ No newline at end of file diff --git a/wrappers/post/create_post.py b/wrappers/post/create_post.py index 5e45012..9884e2f 100644 --- a/wrappers/post/create_post.py +++ b/wrappers/post/create_post.py @@ -1,3 +1,5 @@ +from utils.get_parent_pksk_from_path import get_parent_pksk_from_path + def create_post(post_text, parent_path='/', blur_label=None, card_url=None, collaborative=False): """ Creates a new post. @@ -14,22 +16,8 @@ def create_post(post_text, parent_path='/', blur_label=None, card_url=None, coll that will be processed by the API client wrapper function. """ - parent_pksk = None + parent_pksk = get_parent_pksk_from_path(parent_path) - if parent_path == '/': - parent_pksk = 'maintrunk#maintrunk' - else: - parent_path = parent_path.strip('/') - entity, slug = parent_path.split('/') - if entity == 'branch': - entity = 'subwiki' - - if entity not in ['userprofile', 'subwiki']: - raise ValueError(f"Invalid parent entity: {entity}. Must be 'userprofile' or 'branch'.") - - parent_pksk = f"{entity}#{slug}" - - return { "job_function": "post.create",