Last active
April 14, 2021 17:11
-
-
Save ioggstream/cd961ddf7f522b154688cd6500b11235 to your computer and use it in GitHub Desktop.
Parse SAMM2 model yaml files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Parses yaml files contained in https://github.com/OWASP/samm/tree/master/Supporting%20Resources/v2.0/Datamodel/Datafiles | |
# | |
from glob import glob | |
from pathlib import Path | |
import yaml | |
def read_yaml(f): | |
return yaml.safe_load(Path(f).read_text()) | |
class SammResolver: | |
def __init__(self): | |
self.functions = [f[:-4].split(" ")[1] for f in glob("Function *yml")] | |
self.functions_map = {x[0]: x for x in self.functions} | |
self.practices_map = self._parse_practices() | |
self.streams = self._parse_streams() | |
self.activities = list( | |
set([f[:-4].split(" ")[1] for f in glob("Activit*.yml")]) | |
) | |
def _parse_practices(self): | |
practices = {} | |
for f in Path(".").glob("Practice *yml"): | |
p = read_yaml(f) | |
if "shortDescription" not in p: | |
continue | |
practices[p["shortName"]] = p["name"] | |
return practices | |
def _parse_streams(self): | |
streams = {} | |
for f in glob("Stream *yml"): | |
s = read_yaml(f) | |
s_id = f[7:-4] | |
s_name = s["name"] | |
streams[s_id] = s_name | |
return streams | |
def parse_activity(self, a): | |
function, practice, maturity, stream = a.split("-") | |
stream_id = f"{function}-{practice}-{stream}" | |
return { | |
"id": a, | |
"function": self.functions_map[function], | |
"practice": self.practices_map[practice], | |
"maturity": maturity, | |
"stream": self.streams[stream_id], | |
} | |
def test_parse_activities(): | |
samm = SammResolver() | |
for a in samm.activities: | |
print(samm.parse_activity(a)) | |
def test_samm_to_csv(): | |
samm = SammResolver() | |
import pandas as pd | |
df = pd.DataFrame([samm.parse_activity(a) for a in samm.activities]) | |
df.to_csv("samm_activities.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment