-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
check-or-enforce-order.py
105 lines (92 loc) · 4.18 KB
/
check-or-enforce-order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import frontmatter as fm
from pathlib import Path, PosixPath
import sys
# path here is intended to include only posts from a single language
# _posts/r, _posts/plotly_js, _posts/python-v3, _posts/python in 'documentation'
# build/html in 'plotly.py-docs'
try:
folder_path = str(sys.argv[1])
except:
raise Exception("You need to specify a path!")
# check to see if enforce flag was given at command line
enforce = False
if len(sys.argv) == 3:
if sys.argv[2] == 'enforce':
enforce = True
categories = ["file_settings", "basic", "financial", "statistical", "scientific", "maps", "3d_charts", "multiple_axes", "ai_ml"]
def get_post(path):
return fm.load(str(path))
def get_front_matter(post):
if "jupyter" in post.metadata:
return post["jupyter"]["plotly"]
else:
return post.metadata
# this function will mutate the front-matter to enforce a sequential order
def enforceOrder(list_to_be_ordered):
print(list_to_be_ordered)
for index, post in enumerate(list_to_be_ordered):
post_to_be_altered = fm.load(str(post))
if folder_path == "python": # accounts for the fact that this is also run in the plotly.py-docs repo
post_to_be_altered.metadata["jupyter"]["plotly"]['order'] = index+1
fm.dump(post_to_be_altered, str(post))
else:
post_to_be_altered.metadata['order'] = index+1
fm.dump(post_to_be_altered, str(post))
def is_consecutive(list_to_be_checked):
print(sorted(list_to_be_checked))
return sorted(list_to_be_checked) == list(range(1, len(list_to_be_checked)+1))
def validate_front_matter(front_matter):
if len(front_matter.keys()) > 0:
if "display_as" in front_matter and "order" in front_matter:
if front_matter['display_as'] in categories:
return True
else:
return False
else:
return False
def get_paths_and_orders_by_category():
posts_by_category = {category: dict(orders=[], paths=[]) for category in categories}
suffixes = ["md", "html"]
if folder_path == "r":
suffixes = ["Rmd"]
for suffix in suffixes:
for path in Path(folder_path).glob("**/*."+suffix):
if ".ipynb_checkpoints" not in str(path):
post = get_post(path)
front_matter = get_front_matter(post)
if "display_as" in front_matter:
post_category = front_matter['display_as']
if post_category in posts_by_category and validate_front_matter(front_matter):
posts_by_category[post_category]["paths"].append(path)
posts_by_category[post_category]["orders"].append(front_matter['order'])
return posts_by_category
def check_order():
posts_by_category = get_paths_and_orders_by_category()
for category in categories:
print(category)
orders = posts_by_category[category]["orders"]
paths = posts_by_category[category]["paths"]
sorted_paths = [path for order, path in sorted(zip(orders, paths))]
if not is_consecutive(posts_by_category[category]["orders"]):
print("Order is not sequential! **CHECK NOT PASSED** in '{}' display_as!".format(category))
if enforce is True:
print("ENFORCING CORRECT ORDER! for {}\n".format(category))
enforceOrder(sorted_paths)
else:
arg = folder_path
if folder_path == "build/html":
arg = "python"
if folder_path == "build":
arg = "r"
raise Exception("Order is not sequential! **CHECK NOT PASSED** in '{}' display_as! Run 'python check-or-enforce-order.py {} enforce' to resolve!".format(category, arg))
else:
print("*Check Passed!*\n")
print("**********************************************")
print("Order of '{}' Before Enforcing!".format(folder_path))
print("**********************************************\n")
check_order()
if enforce is True:
print("*******************************************")
print("Order of '{}' After Enforcing!".format(folder_path))
print("*******************************************\n")
check_order()