22import logging
33from slack_sdk import WebClient
44from datetime import timedelta , date , datetime
5- import json
5+ import re
6+ import ast
67
78DEFAULT_VAL_FREQ = 6
89FILEPATH = "./" ## for local testing use "../" and shift filepath_list[] indexes +1
@@ -22,18 +23,53 @@ def get_prod_cat_ref():
2223
2324 product_categories = {}
2425
25- # Load the menu file
26- with open (FILEPATH + 'menu/navigation.json' , 'r' ) as file :
27- data = json .load (file ) # Parse the JSON content into a Python dictionary or list
28-
29- for grouping in data :
30- for category in grouping ["items" ]:
31- category_label = category ["label" ]
32- for product in category ["items" ]:
33- product_label = product ["label" ]
34- product_slug = product ["slug" ]
35-
36- product_categories [product_slug ] = [category_label , product_label ]
26+ # Read the menu file
27+ with open (FILEPATH + "menu/navigation.ts" , "r" , encoding = "utf-8" ) as f :
28+ js = f .read ()
29+
30+ # remove all lines up to "export default"
31+ js = re .sub (r"^import .*" , "" , js , flags = re .M ) # remove import lines
32+ js = re .sub (r"//.*" , "" , js ) # strip line comments
33+ js = re .sub (r"/\*.*?\*/" , "" , js , flags = re .S ) # strip block comments
34+ js = js .replace ("export default" , "" ).strip () # strip "export default" and cleanup
35+
36+ # Convert items - [accountMenu, billingMenu] → ['account', 'billing']
37+ js = re .sub (r"\b([a-zA-Z0-9_]+)Menu\b(?!')" , r"'\1'" , js )
38+
39+ # Convert icons - OrganizationDashboardCategoryIcon → 'OrganizationDashboardCategory'
40+ js = re .sub (r"\b([A-Z][A-Za-z0-9]+)Icon\b(?!')" , r"'\1'" , js )
41+
42+ # Normalize JS → Python syntax ---
43+ js = re .sub (r"(\w+):" , r"'\1':" , js ) # quote keys
44+ js = js .replace ("null" , "None" ) # replace nulls with Nones
45+ js = re .sub (r",(\s*[}\]])" , r"\1" , js ) # remove trailing commas
46+
47+ # Convert the js string now containing Python literal syntax, into an actual Python object.
48+ data = ast .literal_eval (js )
49+
50+ # Helper function
51+ def camel_to_kebab (name : str ) -> str :
52+ """Convert camelCase or PascalCase to kebab-case."""
53+ s1 = re .sub ('(.)([A-Z][a-z]+)' , r'\1-\2' , name )
54+ s2 = re .sub ('([a-z0-9])([A-Z])' , r'\1-\2' , s1 )
55+ return s2 .lower ()
56+
57+ # Helper function
58+ def prettify_label (slug : str ) -> str :
59+ """Convert kebab-case to title case."""
60+ return slug .replace ("-" , " " ).title ()
61+
62+ # Walk structure and make new dict
63+
64+ product_categories = {}
65+
66+ for section in data :
67+ for category in section ["items" ]:
68+ category_label = category ["label" ]
69+ for item in category ["items" ]:
70+ key = camel_to_kebab (item ).lower ().replace ("_" , "-" )
71+ label = prettify_label (key )
72+ product_categories [key ] = [category_label , label ]
3773
3874 return (product_categories )
3975
0 commit comments