22import logging
33from slack_sdk import WebClient
44from datetime import timedelta , date , datetime
5+ import json
56
67DEFAULT_VAL_FREQ = 6
8+ FILEPATH = "./" ## for local testing use "../" and shift filepath_list[] indexes +1
79
810def convert_to_date_and_delta (val_date , val_freq ):
911 "Converts validation date string to datetime and validation frequency string (months) to timedelta."
@@ -15,6 +17,26 @@ def convert_to_date_and_delta(val_date, val_freq):
1517 # handles the case where validation format is incorrect
1618 return None , None
1719
20+ def get_prod_cat_ref ():
21+ "Makes a dictionary where keys are product slugs and values are their category label and product label"
22+
23+ product_categories = {}
24+
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 ]
37+
38+ return (product_categories )
39+
1840def needs_review (val_date , val_freq ):
1941 "Returns true if doc needs to be reviewed, based on val date and frequency"
2042 val_date_conv , val_freq_conv = convert_to_date_and_delta (val_date , val_freq )
@@ -60,32 +82,38 @@ def process_files(directory):
6082 docs_to_review .append (filepath )
6183 return docs_to_review
6284
63- def get_doc_cat_name (filepath ):
64- "Returns a document-to-review's category and tidied-up filepath, based on its raw filepath."
85+ def get_doc_cat_name (filepath , prod_cat_ref ):
86+ "Returns a document-to-review's category and tidied-up filepath, based on its raw filepath and the prod_cat_ref dict ."
6587 trimmed_filepath = filepath [2 :- 4 ]
6688 filepath_list = trimmed_filepath .split ("/" )
6789
6890 if filepath_list [0 ] == "tutorials" :
69- category = filepath_list [ 0 ]
91+ category_product = "Tutorials"
7092 elif filepath_list [0 ] == "faq" :
71- category = filepath_list [ 1 ]
93+ category_product = "FAQ"
7294 else :
73- category = ' ' .join (filepath_list [0 :2 ])
74-
75- return category , trimmed_filepath
95+ # catches everything in pages
96+ category = prod_cat_ref .get (filepath_list [1 ], ["Unknown" , "Unknown" ])[0 ]
97+ product = prod_cat_ref .get (filepath_list [1 ], ["Unknown" , "Unknown" ])[1 ]
98+ category_product = category + ": " + product
99+
100+ return category_product , trimmed_filepath
76101
77102def organize_docs_by_category (docs_to_review ):
78103 "Organizes docs to review by category into a dictionary."
79104 print ("Organizing docs by category" )
80105 dict_by_cat = {}
81-
106+
107+ # one shot: make a dict of all products and their categories, based on menu file
108+ prod_cat_ref = get_prod_cat_ref ()
109+
82110 for filepath in docs_to_review :
83- category , trimmed_filepath = get_doc_cat_name (filepath )
111+ category_product , trimmed_filepath = get_doc_cat_name (filepath , prod_cat_ref )
84112
85- if category not in dict_by_cat :
86- dict_by_cat [category ] = [trimmed_filepath ]
113+ if category_product not in dict_by_cat :
114+ dict_by_cat [category_product ] = [trimmed_filepath ]
87115 else :
88- dict_by_cat [category ].append (trimmed_filepath )
116+ dict_by_cat [category_product ].append (trimmed_filepath )
89117
90118 # sort the dictionary alphabetically by category
91119 dict_by_cat_sorted = {key : value for key , value in sorted (dict_by_cat .items ())}
@@ -98,7 +126,7 @@ def prep_message(docs_to_review_by_cat):
98126 message = ":wave: Hi doc team, here are some docs to review: \n \n "
99127
100128 for key in docs_to_review_by_cat :
101- message += "*" + key . title () + "*" + "\n "
129+ message += "*" + key + "*" + "\n "
102130 for doc in docs_to_review_by_cat [key ]:
103131 message += doc + "\n "
104132 message += "\n "
@@ -116,7 +144,7 @@ def send_message(message):
116144 )
117145
118146def main ():
119- docs_to_review = process_files ("." )
147+ docs_to_review = process_files (FILEPATH )
120148 docs_to_review_by_cat = organize_docs_by_category (docs_to_review )
121149 message = prep_message (docs_to_review_by_cat )
122150 if os .environ .get ("DRY_RUN" ) != "true" :
0 commit comments