-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_validation.py
47 lines (32 loc) · 1.47 KB
/
agent_validation.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
import json
import copy
from bioagents_dev import validate_agent
def is_html_error(message):
return '<html' in message.lower() or '<body' in message.lower()
def attempt_fix_agent_name(agent, token):
"""Attempt to fix the agent name and revalidate."""
agent_temp = copy.deepcopy(agent)
agent_temp['name'] += '_autogenerated'
valid, error_message = validate_agent(agent_temp, token)
return valid, agent_temp if valid else error_message
def validate_agents(agents, token):
to_add = []
problem_agents = []
for agent in agents:
valid, txt = validate_agent(agent, token)
if valid:
to_add.append(agent)
continue
print("Agent with name:{name} has the errors: {errors}".format(name=agent['name'], errors=txt))
if not is_html_error(txt):
e = json.loads(txt)
if type(e) is dict and e.get('name') != None:
print('Error with name {name}'.format(name=agent['name']))
valid, result = attempt_fix_agent_name(agent, token)
if valid:
print('The error was fixed by changing the name to {name}'.format(name=result['name']))
agent['name'] = result['name']
to_add.append(agent)
continue
problem_agents.append({'agent_name':agent['name'],'error': 'html' if is_html_error(txt) else txt})
return to_add, problem_agents