-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathautogen.py
More file actions
201 lines (150 loc) · 7 KB
/
Copy pathautogen.py
File metadata and controls
201 lines (150 loc) · 7 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import jinja2
import urllib3
import typing
import requests
import attrs
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
__all__ = ['generate_ai_plugins_classes']
IF_PLUGINS = ['ZDX112021', 'ABR092025', 'SBT032025', 'MAT022025', 'ASM0220223', 'TDO042024', 'NFI042024', 'PBI082024', 'VBR062025', 'BOO032022', 'ADO112023', 'ABK042023', 'DSL122024', 'GVM062022', 'RBK072025', 'MFB062025', 'OVM012024', 'ADS102024', 'ZVM062022', 'GLU062021', 'GDR052023', 'AMM082023', 'ODS052024', 'CCI012025', 'COM032023', 'GCR042024', 'APP022025', 'SOP072023', 'ICS032022', 'GBA032023', 'ADM062025', 'RST062023', 'ASF012023', 'AEC082022', 'ABA092022', 'ACS042023', 'PDY092024', 'ALM012024', 'ZRM082023', 'GBQ102022', 'TAB072023', 'AAF112024', 'DBX032022', 'AAP092024', 'TDM052022', 'QLC092022', 'MFW022023', 'ZFN032022', 'SNF092022', 'RMQ082024', 'GFU012023', 'SFI122022', 'ASB012025', 'BEC032025', 'AMW112024', 'GDF032022', 'CRW122025', 'VMW102024', 'ZWB072025', 'VNB032025', 'BAK092023', 'ODI032024', 'GEA072025', 'DTG022025', 'ZML022023', 'GVA092025', 'GIT092025', 'TER102023', 'ASQ032024', 'GDU102023', 'ADY122023', 'ZBA042022', 'LAN012026', 'ADF062021', 'JEN022024', 'CMR022024', 'AEM072022', 'JCP102025', 'AWX122024', 'ZSY062022', 'KBN062023', 'ARS112024', 'GCC052024', 'ATR122022', 'FVT032025', 'ANS032024', 'ODF0420241', 'AAT052023', 'GDP042022', 'ADP122022', 'ABB092025', 'NBU032025', 'ABY122023', 'GDM082023', 'ZLA112022', 'GDQ112023', 'AAR072022', 'DBT042023', 'DTG052025', 'UIP072021', 'GWF092023', 'AST072024', 'MFL022023', 'ADB112022', 'AHD062022', 'MBI042022', 'DSW122024', 'ACF082023', 'AQS012023']
imports_str = '''from aapi import *
import attrs
'''
template_job = jinja2.Template('''
@attrs.define
class {{plugin.class_name}}(AIJob):
_type = AIJob.type_field('{{plugin.type_field}}')
{% for p in plugin.mandatory_properties %}
{{p[0]}} = AIJob.field('{{p[1]}}')
{%endfor%}
{% for p in plugin.optional_properties %}
{{p[0]}} = AIJob.field_optional('{{p[1]}}', default='{{p[2]}}')
{%endfor%}
''')
template_cp = jinja2.Template('''
@attrs.define
class {{plugin.class_name}}(AIConnectionProfile):
_type = AIConnectionProfile.type_field('{{plugin.type_field}}')
{% for p in plugin.mandatory_properties %}
{{p[0]}} = AIConnectionProfile.field('{{p[1]}}')
{%endfor%}
{% for p in plugin.optional_properties %}
{{p[0]}} = AIConnectionProfile.field_optional('{{p[1]}}', default='{{p[2]}}')
{%endfor%}
''')
@attrs.define
class Plugin:
job_name: str
cp_name: str
job_definition: typing.Dict[str, str]
cp_definition: typing.Dict[str, str]
plugin_type: str
@attrs.define
class AIPluginCustomDefinition:
class_name: str
type_field: str
mandatory_properties: typing.List[typing.Tuple[str]]
optional_properties: typing.List[typing.Tuple[str]]
comments: typing.List[str]
def create_ai_custom_definitions(ai_plugin: Plugin) -> typing.Tuple[AIPluginCustomDefinition, AIPluginCustomDefinition]:
plugin_name = ai_plugin.job_name.split(':')[-1]
plugin_cp_name = ai_plugin.cp_name.split(':')[-1]
job = {
'class_name': 'AIJob'+plugin_name.capitalize().replace(" ", ""),
'type_field': plugin_name,
'mandatory_properties': [],
'optional_properties': [],
'comments': [f'Auto generated class for {plugin_name}']
}
cp = {
'class_name': 'ConnectionProfileAI'+plugin_cp_name.capitalize().replace(" ", ""),
'type_field': plugin_cp_name,
'mandatory_properties': [],
'optional_properties': [],
'comments': [f'Auto generated class for {plugin_cp_name}']
}
for k, v in ai_plugin.job_definition.items():
if not k.startswith('AI-'):
continue
if k == 'AI-Connection Profile':
continue
domain_name = v.get('DomainName')
if domain_name in ai_plugin.job_definition['Defaults']:
default = ai_plugin.job_definition['Defaults'][domain_name]
job['optional_properties'].append((
domain_name.split(r'%%UCM-')[1].lower(),
k.split('AI-')[1],
default
))
else:
job['mandatory_properties'].append((
domain_name.split(r'%%UCM-')[1].lower(),
k.split('AI-')[1],
None
))
for k, v in ai_plugin.cp_definition.items():
if not k.startswith('AI-'):
continue
if k in ['AI-Run As', 'AI-RunAs-Pass']:
continue
domain_name = v.get('DomainName')
if domain_name in ai_plugin.cp_definition['Defaults']:
default = ai_plugin.cp_definition['Defaults'][domain_name]
cp['optional_properties'].append((
domain_name.lower(),
k.split('AI-')[1],
default
))
else:
cp['mandatory_properties'].append((
domain_name.lower(),
k.split('AI-')[1],
None
))
return (AIPluginCustomDefinition(**job), AIPluginCustomDefinition(**cp))
def get_all_plugins_versions_from_url(url: str) -> typing.Dict[str, str]:
r = requests.get(url=url, verify=False)
return r.json()
def parse_plugin(url):
r = requests.get(url, verify=False)
plugin = {}
for k, v in r.json().items():
if k.startswith('Job:'):
plugin["job_name"] = k
plugin["job_definition"] = v
continue
if k.startswith('ConnectionProfile:'):
plugin["cp_name"] = k
plugin["cp_definition"] = v
continue
if 'Aliases' in r.json():
plugin['plugin_type'] = 'AI'
else:
plugin['plugin_type'] = 'IF'
return Plugin(**plugin)
# return plugin
def get_all_plugins(host: str, port: str = '8443', exlude_list=[]):
versions = get_all_plugins_versions_from_url(
f'https://{host}:{port}/automation-api/ai-repo/deployed_plugins.json')
res = []
for plugin, version in versions.items():
if plugin in exlude_list:
continue
url = f'https://{host}:{port}/automation-api/ai-repo/{plugin}/v{version}/descriptorDictionary{plugin}.json'
res.append(parse_plugin(url))
return res
def create_class_code(plugin_definitions):
job_str = template_job.render(plugin=plugin_definitions[0])
cp_str = template_cp.render(plugin=plugin_definitions[1])
return (job_str, cp_str)
def generate_ai_plugins_classes(host: str = 'localhost', port: str = '8443', output_file: str = 'generated.py'):
plugins = get_all_plugins(host=host, port=port, exlude_list=IF_PLUGINS)
definitions = [create_ai_custom_definitions(plugin) for plugin in plugins]
with open(output_file, 'w') as f:
f.write(imports_str)
for definition in definitions:
def_str = create_class_code(definition)
f.write(f'''
# Auto generated code for AI Plugin {definition[0].type_field}
''')
f.write(def_str[0]) # JOB
f.write(def_str[1]) # Connection Profile