-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscen_parser.py
66 lines (53 loc) · 2.01 KB
/
scen_parser.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
import os, logging, io
from pathlib import Path
import subprocess
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element
from defusedxml.ElementTree import parse
from lib.xml.disablexmlnamespace import DisableXmlNamespaces
class SecGenScenarioController(object):
"""
Provides a means of interacting with scenario based behaviours.
Allows prompts and interactions between the GPT4all to be controlled to provide
a specific behaviour.
"""
sg_tree: ET
root: Element
isParsed = False
def __init__(self, scenario_path):
self.isParsed = self.parse_scenario(scenario_path)
self.set_root()
def parse_scenario(self, scenario_path):
if Path(scenario_path).exists():
p = Path(scenario_path)
try:
scenario = open(p, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
with DisableXmlNamespaces():
try:
self.sg_tree = parse(scenario) # Use defusedxml for safe parsing
except DefusedXmlException as ex:
logging.error(ex)
scenario.close()
return False
scenario.close()
return True
except OSError as scenEx:
scenario.close()
print(scenEx)
logging.error(scenEx)
return False
else:
logging.error("Scenario path does not exist: " + scenario_path + "\n")
print("Scenario not found exiting...")
return False
return False
def set_root(self):
self.root = self.sg_tree.getroot()
def get_channel(self):
return self.root.find('channel').text
def get_bots(self):
return self.root.findall('bot')
def get_bot_name(self, bot):
return bot.find('name').text
def get_bot_prompt(self, bot):
return bot.find('prompt').text