<commit_before>#!/usr/bin/env python
from __future__ import print_function
from mindmup_as_attack_trees import *

import sys,json
import re
from collections import OrderedDict
import math
import argparse
parser = argparse.ArgumentParser()

parser.add_argument("--only-severities", action='store_true', help="only generate markdown for the attacker objective severities, not the rest of the tree")
parser.add_argument('--safety-privacy-financial-operational', action='store_true', help="use this alternate ordering for the severities")
parser.add_argument('mupin', nargs='?', help="the mindmup file that will be processed -- transforming and augmenting the JSON")
args = parser.parse_args()

import ipdb
def info(type, value, tb):
	ipdb.pm()

sys.excepthook = info

levels_count = dict()
nodes_lookup = dict()
fixups_queue = list()
objective_node = None


def clamp_to_json_values(val):
    return max(-1 * sys.float_info.max, min(val, sys.float_info.max))

def parse_evita_raps(node):
	if not 'EVITA::' in get_raw_description(node):
		raise ValueError("couldn't find EVITA:: tag in attack vector node", node)

	for line in get_unclean_description(node).splitlines():
		if not 'EVITA::' in line:
			continue

		evita_line = line.strip().split('|')

		if node.get('attr', None) is None:
		    node.update({'attr': dict()})

		attr = node.get('attr')

		attr.update({'evita_et': clamp_to_json_values(float(evita_line[5]))})
		attr.update({'evita_e':  clamp_to_json_values(float(evita_line[6]))})
		attr.update({'evita_k':  clamp_to_json_values(float(evita_line[7]))})
		attr.update({'evita_wo': clamp_to_json_values(float(evita_line[8]))})
		attr.update({'evita_eq': clamp_to_json_values(float(evita_line[9]))})

	return

def get_evita_et_label(node):
	et = node.get('attr').get('evita_et')

	if et == 0:
		return "0 &lt; one day"
	elif et == 1:
		return "1 &lt; one week"
	elif et == 4:
		return "4 &lt; one month"
	elif et == 10:
		return "10 &lt; three months"
	elif et == 17:
		return "17 &lt; six months"
	elif et == 19:
		return "19 &gt; six months"
	elif et == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % et

def get_evita_e_label(node):
	e = node.get('attr').get('evita_e')

	if e == 0:
		return "0 Layman"
	elif e == 3:
		return "3 Proficient"
	elif e == 6:
		return "6 Expert"
	elif e == 8:
		return "8 Multiple Experts"
	elif e == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % e

def get_evita_k_label(node):
	k = node.get('attr').get('evita_k')

	if k == 0:
		return "0 Public"
	elif k == 3:
		return "3 Restricted"
	elif k == 7:
		return "7 Sensitive"
	elif k == 11:
		return "11 Critical"
	elif k == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % k

def get_evita_wo_label(node):
	wo = node.get('attr').get('evita_wo')

	if wo == 0:
		return "0 Unlimited"
	elif wo == 1:
		return "1 Easy"
	elif wo == 4:
		return "4 Moderate"
	elif wo == 10:
		return "10 Difficult"
	elif wo == float('inf'):
		return "None"
	else:
		return "%d unknown" % wo

def get_evita_eq_label(node):
	eq = node.get('attr').get('evita_eq')

	if eq == 0:
		return "0 Standard"
	elif eq == 4:
		return "4 Specialized"
	elif eq == 7:
		return "7 Bespoke"
	elif eq == 9:
		return "9 Multiple Bespoke"
	elif eq == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % eq

def append_evita_rap_table(node):
	description = get_raw_description(node)

	if description.endswith('|'):
	    print("warning. node %s. don't end node description in '|''" % get_node_title(node))

	html = detect_html(description)
	bookends = ("<div>", "</div>") if html else ('\n', '')

	update_raw_description(node, description +
		"%s%s" % bookends +
		"%s| Elapsed Time | Expertise | Knowledge | Window of Opportunity | Equipment |%s" % bookends +
		"%s|-----------------------------|-----------------------------|-----------------------------|-----------------------------|-----------------------------|%s" % bookends +
		("%s| %%s | %%s | %%s | %%s | %%s |%s" % bookends ) % (
			get_evita_et_label(node),
			get_evita_e_label(node),
			get_evita_k_label(node),
			get_evita_wo_label(node),
			get_evita_eq_label(node)
		)
	)

def derive_evita_apt(node):
	attrs = node.get('attr')

	total_rap = attrs.get('evita_et') + attrs.get('evita_e') + attrs.get('evita_k') + attrs.get('evita_wo') + attrs.get('evita_eq')
	if total_rap < 0:
		raise ValueError('encountered negative Total Required Attack Potential', node.get('attr'))
	elif total_rap < 10:
		apt = 5
	elif total_rap < 14:
		apt = 4
	elif total_rap < 20:
		apt = 3
	elif total_rap < 25:
		apt = 2
	else:
		apt = 1
	#TODO support non-zero controllability

	attrs.update({'evita_apt': apt})
	return

def get_evita_ss_label(node):
	ss = node.get('attr').get('evita_ss')
	ss = int(ss)

	if ss == 0:
		return "S%s No injuries" % ss
	elif ss == 1:
		return "S%s Light or moderate injuries" % ss
	elif ss == 2:
		return "S%s Severe injuries (survival probable); light/moderate injuries for multiple vehicles" % ss
	elif ss == 3:
		return "S%s Life threatening (survivaluncertain) or fatal injuries; severe injuries for multiple vehicles" % ss
	elif ss == 4:
		return "S%s Life threatening or fatal in-juries for multiple vehicles" % ss
	else:
		return "%d unknown" % ss

def get_evita_os_label(node):
	os = node.get('attr').get('evita_os')
	os = int(os)

	if os == 0:
		return "S%s No impact on operational performance" % os<commit_msg>correct 'survivaluncertain' typo<commit_after>#!/usr/bin/env python
from __future__ import print_function
from mindmup_as_attack_trees import *

import sys,json
import re
from collections import OrderedDict
import math
import argparse
parser = argparse.ArgumentParser()

parser.add_argument("--only-severities", action='store_true', help="only generate markdown for the attacker objective severities, not the rest of the tree")
parser.add_argument('--safety-privacy-financial-operational', action='store_true', help="use this alternate ordering for the severities")
parser.add_argument('mupin', nargs='?', help="the mindmup file that will be processed -- transforming and augmenting the JSON")
args = parser.parse_args()

import ipdb
def info(type, value, tb):
	ipdb.pm()

sys.excepthook = info

levels_count = dict()
nodes_lookup = dict()
fixups_queue = list()
objective_node = None


def clamp_to_json_values(val):
    return max(-1 * sys.float_info.max, min(val, sys.float_info.max))

def parse_evita_raps(node):
	if not 'EVITA::' in get_raw_description(node):
		raise ValueError("couldn't find EVITA:: tag in attack vector node", node)

	for line in get_unclean_description(node).splitlines():
		if not 'EVITA::' in line:
			continue

		evita_line = line.strip().split('|')

		if node.get('attr', None) is None:
		    node.update({'attr': dict()})

		attr = node.get('attr')

		attr.update({'evita_et': clamp_to_json_values(float(evita_line[5]))})
		attr.update({'evita_e':  clamp_to_json_values(float(evita_line[6]))})
		attr.update({'evita_k':  clamp_to_json_values(float(evita_line[7]))})
		attr.update({'evita_wo': clamp_to_json_values(float(evita_line[8]))})
		attr.update({'evita_eq': clamp_to_json_values(float(evita_line[9]))})

	return

def get_evita_et_label(node):
	et = node.get('attr').get('evita_et')

	if et == 0:
		return "0 &lt; one day"
	elif et == 1:
		return "1 &lt; one week"
	elif et == 4:
		return "4 &lt; one month"
	elif et == 10:
		return "10 &lt; three months"
	elif et == 17:
		return "17 &lt; six months"
	elif et == 19:
		return "19 &gt; six months"
	elif et == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % et

def get_evita_e_label(node):
	e = node.get('attr').get('evita_e')

	if e == 0:
		return "0 Layman"
	elif e == 3:
		return "3 Proficient"
	elif e == 6:
		return "6 Expert"
	elif e == 8:
		return "8 Multiple Experts"
	elif e == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % e

def get_evita_k_label(node):
	k = node.get('attr').get('evita_k')

	if k == 0:
		return "0 Public"
	elif k == 3:
		return "3 Restricted"
	elif k == 7:
		return "7 Sensitive"
	elif k == 11:
		return "11 Critical"
	elif k == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % k

def get_evita_wo_label(node):
	wo = node.get('attr').get('evita_wo')

	if wo == 0:
		return "0 Unlimited"
	elif wo == 1:
		return "1 Easy"
	elif wo == 4:
		return "4 Moderate"
	elif wo == 10:
		return "10 Difficult"
	elif wo == float('inf'):
		return "None"
	else:
		return "%d unknown" % wo

def get_evita_eq_label(node):
	eq = node.get('attr').get('evita_eq')

	if eq == 0:
		return "0 Standard"
	elif eq == 4:
		return "4 Specialized"
	elif eq == 7:
		return "7 Bespoke"
	elif eq == 9:
		return "9 Multiple Bespoke"
	elif eq == float('inf'):
		return "Not Practical"
	else:
		return "%d unknown" % eq

def append_evita_rap_table(node):
	description = get_raw_description(node)

	if description.endswith('|'):
	    print("warning. node %s. don't end node description in '|''" % get_node_title(node))

	html = detect_html(description)
	bookends = ("<div>", "</div>") if html else ('\n', '')

	update_raw_description(node, description +
		"%s%s" % bookends +
		"%s| Elapsed Time | Expertise | Knowledge | Window of Opportunity | Equipment |%s" % bookends +
		"%s|-----------------------------|-----------------------------|-----------------------------|-----------------------------|-----------------------------|%s" % bookends +
		("%s| %%s | %%s | %%s | %%s | %%s |%s" % bookends ) % (
			get_evita_et_label(node),
			get_evita_e_label(node),
			get_evita_k_label(node),
			get_evita_wo_label(node),
			get_evita_eq_label(node)
		)
	)

def derive_evita_apt(node):
	attrs = node.get('attr')

	total_rap = attrs.get('evita_et') + attrs.get('evita_e') + attrs.get('evita_k') + attrs.get('evita_wo') + attrs.get('evita_eq')
	if total_rap < 0:
		raise ValueError('encountered negative Total Required Attack Potential', node.get('attr'))
	elif total_rap < 10:
		apt = 5
	elif total_rap < 14:
		apt = 4
	elif total_rap < 20:
		apt = 3
	elif total_rap < 25:
		apt = 2
	else:
		apt = 1
	#TODO support non-zero controllability

	attrs.update({'evita_apt': apt})
	return

def get_evita_ss_label(node):
	ss = node.get('attr').get('evita_ss')
	ss = int(ss)

	if ss == 0:
		return "S%s No injuries" % ss
	elif ss == 1:
		return "S%s Light or moderate injuries" % ss
	elif ss == 2:
		return "S%s Severe injuries (survival probable); light/moderate injuries for multiple vehicles" % ss
	elif ss == 3:
		return "S%s Life threatening (survival uncertain) or fatal injuries; severe injuries for multiple vehicles" % ss
	elif ss == 4:
		return "S%s Life threatening or fatal in-juries for multiple vehicles" % ss
	else:
		return "%d unknown" % ss

def get_evita_os_label(node):
	os = node.get('attr').get('evita_os')
	os = int(os)

	if os == 0:
		return "S%s No impact on operational performance" % os
{'fraction_non_alphanumeric': 0.17175432144170652, 'fraction_numerical': 0.01471129091577786, 'mean_word_length': 3.7958553791887124, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5070286', 'n_tokens_mistral': 4275, 'n_tokens_neox': 3911, 'n_words': 1110}