-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgenerate-scp-markdown.py
82 lines (67 loc) · 2.15 KB
/
generate-scp-markdown.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
import os
from pathlib import Path
import json
import pandas
import jinja2
from jinja2 import Template, select_autoescape
prefix="../docs/guardrails"
scp_filename=f"{prefix}/scp-guardrails.md"
layout_header="""---
layout: default
---
# Table of Contents
"""
def to_pretty_json(value):
return json.dumps(value, sort_keys=True,
indent=4, separators=(',', ': '))
def get_template():
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader, autoescape=select_autoescape(['html', 'xml']))
templateEnv.filters['tojson_pretty']=to_pretty_json
TEMPLATE_FILE = "scp-template.md"
template = templateEnv.get_template(TEMPLATE_FILE)
return template
def generate_markdown_from_files(foldername):
scp_frames=[]
scp_guardrails=[]
#print("folder",foldername)
files=os.listdir(foldername)
files.sort()
for filename in files:
#print(filename)
with open(f"{foldername}/{filename}") as json_file:
data=json.load(json_file)
if "Policy-Type" in data and data["Policy-Type"]=="SCP":
conditions={}
if "Condition" in data:
for condition in data["Condition"]:
for key,value in condition.items():
conditions[key]=value
conditions=json.dumps(conditions,indent=4,sort_keys=True)
data["conditions"]=conditions
print(conditions)
scp_frames.append(data)
identifier=data['Identifier'].lower()
scp_guardrails.append(f"* [{data['Guardrail']}](#{identifier})")
scp_template=get_template()
outputtext=scp_template.render(scps=scp_frames)
print(outputtext)
print(scp_guardrails)
with open(scp_filename,'a') as out:
out.write(outputtext)
out.write("\n\n\n\n\n\n")
return scp_guardrails
if __name__ == "__main__":
with open(scp_filename,'w') as out:
out.write(layout_header)
out.write("\n\n\n")
indexes=[]
files = os.listdir(os.curdir)
files.sort()
for filename in files:
if os.path.isdir(filename):
index=generate_markdown_from_files(filename)
if index: indexes.append(index)
for index in indexes:
for i in index:
print(i)