- Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcheck-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
importfrontmatterasfm
frompathlibimportPath, PosixPath
importsys
# 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:
raiseException("You need to specify a path!")
# check to see if enforce flag was given at command line
enforce=False
iflen(sys.argv) ==3:
ifsys.argv[2] =='enforce':
enforce=True
categories= ["file_settings", "basic", "financial", "statistical", "scientific", "maps", "3d_charts", "multiple_axes", "ai_ml"]
defget_post(path):
returnfm.load(str(path))
defget_front_matter(post):
if"jupyter"inpost.metadata:
returnpost["jupyter"]["plotly"]
else:
returnpost.metadata
# this function will mutate the front-matter to enforce a sequential order
defenforceOrder(list_to_be_ordered):
print(list_to_be_ordered)
forindex, postinenumerate(list_to_be_ordered):
post_to_be_altered=fm.load(str(post))
iffolder_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))
defis_consecutive(list_to_be_checked):
print(sorted(list_to_be_checked))
returnsorted(list_to_be_checked) ==list(range(1, len(list_to_be_checked)+1))
defvalidate_front_matter(front_matter):
iflen(front_matter.keys()) >0:
if"display_as"infront_matterand"order"infront_matter:
iffront_matter['display_as'] incategories:
returnTrue
else:
returnFalse
else:
returnFalse
defget_paths_and_orders_by_category():
posts_by_category= {category: dict(orders=[], paths=[]) forcategoryincategories}
suffixes= ["md", "html"]
iffolder_path=="r":
suffixes= ["Rmd"]
forsuffixinsuffixes:
forpathinPath(folder_path).glob("**/*."+suffix):
if".ipynb_checkpoints"notinstr(path):
post=get_post(path)
front_matter=get_front_matter(post)
if"display_as"infront_matter:
post_category=front_matter['display_as']
ifpost_categoryinposts_by_categoryandvalidate_front_matter(front_matter):
posts_by_category[post_category]["paths"].append(path)
posts_by_category[post_category]["orders"].append(front_matter['order'])
returnposts_by_category
defcheck_order():
posts_by_category=get_paths_and_orders_by_category()
forcategoryincategories:
print(category)
orders=posts_by_category[category]["orders"]
paths=posts_by_category[category]["paths"]
sorted_paths= [pathfororder, pathinsorted(zip(orders, paths))]
ifnotis_consecutive(posts_by_category[category]["orders"]):
print("Order is not sequential! **CHECK NOT PASSED** in '{}' display_as!".format(category))
ifenforceisTrue:
print("ENFORCING CORRECT ORDER! for {}\n".format(category))
enforceOrder(sorted_paths)
else:
arg=folder_path
iffolder_path=="build/html":
arg="python"
iffolder_path=="build":
arg="r"
raiseException("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()
ifenforceisTrue:
print("*******************************************")
print("Order of '{}' After Enforcing!".format(folder_path))
print("*******************************************\n")
check_order()