Skip to content

Commit 23f219e

Browse files
more refactoring
1 parent e8bd402 commit 23f219e

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

front-matter-ci.py

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,98 +4,104 @@
44

55
# 'path' == '_posts' in 'documentation'
66
# 'path' == 'build/html' in 'py-docs'
7-
# 'path' == 'r' in 'r-docs'
7+
# 'path' == 'build' in 'r-docs'
88
try:
99
path = str(sys.argv[1])
1010
except:
11-
raise Exception("You need to specify a path that contains the files with front matter.")
11+
raise Exception(
12+
"You need to specify a path that contains the files with front matter."
13+
)
1214

1315

14-
def postsWithNoName(meta_to_check):
16+
def check_postsWithNoName(meta_to_check):
1517
failures = []
1618
for meta in meta_to_check:
1719
# Check #1 - do all non-redirect posts have names?
1820
if "name" not in meta and "redirect_to" not in meta:
19-
failures.append(post.metadata['permalink'])
20-
return failures
21+
failures.append(post.metadata["permalink"])
22+
return "do all non-redirect posts have names?", failures
2123

22-
def postsWithTitle(meta_to_check):
24+
25+
def check_postsWithTitle(meta_to_check):
2326
failures = []
2427
for meta in meta_to_check:
2528
# Check #2 - do any posts have titles?
2629
if "title" in meta:
27-
failures.append(post.metadata['permalink'])
28-
return failures
30+
failures.append(post.metadata["permalink"])
31+
return "do any posts have titles?", failures
32+
2933

30-
def duplicatePermalinks(meta_to_check):
34+
def check_duplicatePermalinks(meta_to_check):
3135
failures = []
3236
allPermalinks = []
3337
for meta in meta_to_check:
3438
# Check #3 - are there duplicate permalinks/redirect_froms?
35-
if "permalink" in meta and meta['permalink'] != '//plot.ly/products/dash/':
36-
if meta['permalink'] in allPermalinks:
37-
print(meta['permalink'])
38-
failures.append(meta['permalink'])
39+
if "permalink" in meta and meta["permalink"] != "//plot.ly/products/dash/":
40+
if meta["permalink"] in allPermalinks:
41+
failures.append(meta["permalink"])
3942
else:
40-
allPermalinks.append(meta['permalink'])
43+
allPermalinks.append(meta["permalink"])
4144
if "redirect_from" in meta:
42-
if meta['redirect_from'] in allPermalinks:
43-
failures.append(meta['redirect_from'])
45+
if meta["redirect_from"] in allPermalinks:
46+
failures.append(meta["redirect_from"])
4447
else:
45-
allPermalinks.append(meta['redirect_from'])
46-
return failures
48+
allPermalinks.append(meta["redirect_from"])
49+
return "are there posts with order > 5 and 'page_type: example_index'?", failures
4750

4851

49-
def indexOverflow(meta_to_check):
52+
def check_indexOverflow(meta_to_check):
5053
failures = []
5154
for meta in meta_to_check:
5255
# Check #4 - are there posts with order > 5 and 'page_type: example_index'?
53-
if "display_as" in meta and meta['display_as'] in categories:
54-
if "language" in meta and meta['language'] in languages:
55-
if "order" in meta and meta['order'] > 5:
56-
if "page_type" in meta and meta['page_type'] == "example_index":
57-
failures.append(meta['permalink'])
58-
return failures
59-
60-
def postsWithNoThumbnail(meta_to_check):
56+
if "display_as" in meta and meta["display_as"] in categories:
57+
if "language" in meta and meta["language"] in languages:
58+
if "order" in meta and meta["order"] > 5:
59+
if "page_type" in meta and meta["page_type"] == "example_index":
60+
failures.append(meta["permalink"])
61+
return "are there duplicate permalinks/redirect_froms?", failures
62+
63+
64+
def check_postsWithNoThumbnail(meta_to_check):
6165
failures = []
6266
for meta in meta_to_check:
6367
# Check #5 - does every post have a thumbnail?
6468
if "thumbnail" not in meta:
65-
if "display_as" in meta and meta['display_as'] in categories:
66-
if "language" in meta and meta['language'] in languages:
67-
failures.append(meta['permalink'])
68-
return failures
69+
if "display_as" in meta and meta["display_as"] in categories:
70+
if "language" in meta and meta["language"] in languages:
71+
failures.append(meta["permalink"])
72+
return "does every post have a thumbnail?", failures
73+
6974

70-
def noTrailingSlash(meta_to_check):
75+
def check_noTrailingSlash(meta_to_check):
7176
failures = []
7277
for meta in meta_to_check:
7378
# Check #6 - do any permalinks not end with a trailing slash?
7479
if "permalink" in meta:
75-
if meta['permalink'][-1] != "/":
76-
failures.append(meta['permalink'])
77-
return failures
78-
79-
checks = {
80-
"do all non-redirect posts have names?": postsWithNoName,
81-
"do any posts have titles?": postsWithTitle,
82-
"are there posts with order > 5 and 'page_type: example_index'?": indexOverflow,
83-
"are there duplicate permalinks/redirect_froms?": duplicatePermalinks,
84-
"does every post have a thumbnail?": postsWithNoThumbnail,
85-
"do any permalinks not end with a trailing slash?": noTrailingSlash,
86-
}
87-
88-
categories = ["file_settings", "basic", "financial", "statistical", "scientific", "maps", "3d_charts", "multiple_axes"]
80+
if meta["permalink"][-1] != "/":
81+
failures.append(meta["permalink"])
82+
return "do any permalinks not end with a trailing slash?", failures
83+
84+
85+
categories = [
86+
"file_settings",
87+
"basic",
88+
"financial",
89+
"statistical",
90+
"scientific",
91+
"maps",
92+
"3d_charts",
93+
"multiple_axes",
94+
]
8995
languages = ["python", "python/v3", "plotly_js", "r"]
9096

9197
paths = []
9298
if path == "r":
9399
for suffix in ["Rmd"]:
94-
paths += [x for x in Path(path).glob("*."+suffix)]
100+
paths += [x for x in Path(path).glob("*." + suffix)]
95101
else:
96102
# collect all paths
97103
for suffix in ["md", "html"]:
98-
paths += [x for x in Path(path).glob("**/*."+suffix)]
104+
paths += [x for x in Path(path).glob("**/*." + suffix)]
99105

100106
# collect all posts
101107
meta_to_check = []
@@ -105,11 +111,11 @@ def noTrailingSlash(meta_to_check):
105111
meta_to_check.append(post.metadata)
106112

107113

108-
109114
print("Begin CI Checks!\n")
110115
passed = True
111-
for message in checks:
112-
failures = checks[message](meta_to_check)
116+
check_functions = [v for k, v in globals().items() if k.startswith("check_")]
117+
for check_function in check_functions:
118+
message, failures = check_function(meta_to_check)
113119
print("***********************************!")
114120
print("Checking... {}".format(message))
115121
if len(failures) > 0:
@@ -123,4 +129,4 @@ def noTrailingSlash(meta_to_check):
123129
print("End CI Checks!\n")
124130

125131
if not passed:
126-
raise Exception("***********CI Checks Not Passed! Check Error Messages!*********************")
132+
raise Exception("**One or more CI Check failed! Check Error Messages!")

0 commit comments

Comments
 (0)