-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack2md.py
executable file
·68 lines (56 loc) · 2.49 KB
/
pack2md.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
#!/usr/bin/env python3
# pack2md.py creates pack README.md file from pack metadata.
# Copyright (C) 2019 Carlos <nzlosh@yahoo.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import argparse
import yaml
from jinja2 import Environment, FileSystemLoader
def fetch_template(template="README.jinja", template_dir="."):
print("Fetching template ...")
file_loader = FileSystemLoader(template_dir)
env = Environment(loader=file_loader)
return env.get_template(template)
def fetch_yaml(dirname, display):
print(f"Fetching {display} ... ", end="")
items = {}
if os.path.exists(dirname):
filenames = [filename for filename in os.listdir(dirname) if filename.endswith(".yaml")]
print(f"{len(filenames)} file(s) found.")
for filename in filenames:
with open(f"{dirname}/{filename}", "r") as fhandle:
items[filename] = yaml.load(fhandle, Loader=yaml.FullLoader)
return items
def render_document(template, data, filename):
if not os.path.exists(f"{filename}.bak") and os.path.exists(f"{filename}"):
os.replace(f"{filename}", f"{filename}.bak")
with open(filename, "w") as f:
f.write(template.render(data))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create README markdown from pack metadata.")
parser.add_argument("pack_path", type=str, help="Path to StackStorm pack.")
args = parser.parse_args()
if os.path.exists(args.pack_path):
render_document(
fetch_template(template_dir=args.pack_path),
{
"pack": fetch_yaml(args.pack_path, "pack meta"),
"actions": fetch_yaml(f"{args.pack_path}/actions", "action meta"),
"sensors": fetch_yaml(f"{args.pack_path}/sensors", "sensor meta"),
},
f"{args.pack_path}/README.md",
)
else:
print("'{}' doesn't exist.".format(args.pack_path))