2
2
import logging
3
3
from slack_sdk import WebClient
4
4
from datetime import timedelta , date , datetime
5
+ import json
5
6
6
7
DEFAULT_VAL_FREQ = 6
8
+ FILEPATH = "./" ## for local testing use "../" and shift filepath_list[] indexes +1
7
9
8
10
def convert_to_date_and_delta (val_date , val_freq ):
9
11
"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):
15
17
# handles the case where validation format is incorrect
16
18
return None , None
17
19
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
+
18
40
def needs_review (val_date , val_freq ):
19
41
"Returns true if doc needs to be reviewed, based on val date and frequency"
20
42
val_date_conv , val_freq_conv = convert_to_date_and_delta (val_date , val_freq )
@@ -60,32 +82,38 @@ def process_files(directory):
60
82
docs_to_review .append (filepath )
61
83
return docs_to_review
62
84
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 ."
65
87
trimmed_filepath = filepath [2 :- 4 ]
66
88
filepath_list = trimmed_filepath .split ("/" )
67
89
68
90
if filepath_list [0 ] == "tutorials" :
69
- category = filepath_list [ 0 ]
91
+ category_product = "Tutorials"
70
92
elif filepath_list [0 ] == "faq" :
71
- category = filepath_list [ 1 ]
93
+ category_product = "FAQ"
72
94
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
76
101
77
102
def organize_docs_by_category (docs_to_review ):
78
103
"Organizes docs to review by category into a dictionary."
79
104
print ("Organizing docs by category" )
80
105
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
+
82
110
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 )
84
112
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 ]
87
115
else :
88
- dict_by_cat [category ].append (trimmed_filepath )
116
+ dict_by_cat [category_product ].append (trimmed_filepath )
89
117
90
118
# sort the dictionary alphabetically by category
91
119
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):
98
126
message = ":wave: Hi doc team, here are some docs to review: \n \n "
99
127
100
128
for key in docs_to_review_by_cat :
101
- message += "*" + key . title () + "*" + "\n "
129
+ message += "*" + key + "*" + "\n "
102
130
for doc in docs_to_review_by_cat [key ]:
103
131
message += doc + "\n "
104
132
message += "\n "
@@ -116,7 +144,7 @@ def send_message(message):
116
144
)
117
145
118
146
def main ():
119
- docs_to_review = process_files ("." )
147
+ docs_to_review = process_files (FILEPATH )
120
148
docs_to_review_by_cat = organize_docs_by_category (docs_to_review )
121
149
message = prep_message (docs_to_review_by_cat )
122
150
if os .environ .get ("DRY_RUN" ) != "true" :
0 commit comments