forked from openstack-k8s-operators/data-plane-adoption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod-docs-content-type.py
More file actions
72 lines (58 loc) · 2.25 KB
/
mod-docs-content-type.py
File metadata and controls
72 lines (58 loc) · 2.25 KB
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
#!/usr/bin/python
import os
import re
class highlighter(object):
def __init__(self, text):
self.text = text
def warn(self):
return('\033[0;31m' + self.text + '\033[0m')
def bold(self):
return('\033[1m' + self.text + '\033[0m')
def highlight(self):
return('\033[0;36m' + self.text + '\033[0m')
def editor(filepath, label):
# If the label does not exist, the editor adds it.
try:
with open(filepath, 'r+', encoding='utf-8') as f:
lines = f.readlines()
label_exists = False
for line_content in lines:
if re.search('(?:^|[\n\r]):_(?:mod-docs-content|content|module)-type:[ \t]+\S', line_content.strip()):
label_exists = True
break
if label_exists:
print(f"Skipping {filepath}, label already present")
else:
print(highlighter(f"Editing: Adding content type to {filepath}").bold())
f.seek(0, 0)
f.write(f":_mod-docs-content-type: {label}" + "\n")
# Adds content back
for line in lines:
f.write(line)
# Handle errors gracefully
except FileNotFoundError:
print(highlighter(f"Error: File not found: {filepath}").warn())
except Exception as e:
print(highlighter(f"Error: {e}").warn())
def tree_walker(directory="."):
for root, dirs, files in os.walk(directory):
for file in files:
if file.startswith(".") or not file.endswith(".adoc"):
continue
filepath = os.path.join(root, file)
label = None
if file.startswith("assembly_") or file.startswith("assembly-"):
label = "ASSEMBLY"
elif file.startswith("con_") or file.startswith("con-"):
label = "CONCEPT"
elif file.startswith("proc_") or file.startswith("proc-"):
label = "PROCEDURE"
elif file.startswith("ref_") or file.startswith("ref-"):
label = "REFERENCE"
if label:
editor(filepath, label)
else:
pass
if __name__ == "__main__":
tree_walker(".")
print(highlighter("Complete!").bold())