forked from F5Networks/f5-common-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_engine.py
More file actions
220 lines (194 loc) · 8.89 KB
/
Copy pathtemplate_engine.py
File metadata and controls
220 lines (194 loc) · 8.89 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright 2016 F5 Networks Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import print_function
import io
import jinja2
import json
import os
import re
from pprint import pprint as pp
from six import iterkeys
class UnexpectedOCItem(Exception):
pass
class Kindless(Exception):
pass
class TemplateEngine(object):
'''Instantiate this to handle generating Python from JSON
Currently supports generation of OrganizingCollection, and
Collection, strings. Also produces most of Resource classes.
'''
def __init__(self, template_dir, config_dir):
self.config_dir = config_dir
self.OC_pattern =\
r'tm:(?P<OrgColl>\w+):(?P=OrgColl)collectionstate'
# Initialize templates
self.templates = {}
self._consume_directory(template_dir, self._template_consumer)
self.import_template = self.templates['imports']
# Initialize configs
self.raw_configs = {}
self.formatted_configs = {}
self._consume_directory(config_dir, self._config_consumer)
def _template_consumer(self, STUB, dirname, fnames):
for fname in fnames:
if fname.endswith('.tmpl'):
with io.open(os.path.join(dirname, fname)) as fh:
templatekey = fname[:-len('.tmpl')]
self.templates[templatekey] = jinja2.Template(fh.read())
def _config_consumer(self, STUB, dirname, fnames):
for fname in fnames:
if fname.endswith('.json'):
with io.open(os.path.join(dirname, fname)) as fh:
configkey = fname[:-len('.json')]
self.raw_configs[configkey] = json.load(fh)
def _consume_directory(self, dirname, consumer):
os.path.walk(dirname, consumer, self)
def list_templates(self):
pp(list(iterkeys(self.templates)))
def list_raw_configs(self):
pp(list(iterkeys(self.raw_configs)))
def process_config_from_fname(self, fname):
if not fname.endswith('.json'):
raise Exception
config_name = fname[:-len('.json')]
try:
raw_conf = self.raw_configs[config_name]
except KeyError as ex:
error_message = "Expected file named %r.json to exist in"\
" directory %r" % (config_name, self.config_dir)
print(error_message)
raise ex
if 'kind' in raw_conf:
python_as_string = self._process_config_with_kind(raw_conf)
else:
raise Kindless(raw_conf)
return python_as_string, raw_conf['selfLink']
def _handle_dashes_dots_capitals(self, raw_klass):
print('raw_klass: %r' % raw_klass)
dashless_klass = raw_klass.replace('-', '_')
dotless_klass = dashless_klass.replace('.', '_')
return '_'.join([x.capitalize() for x in dotless_klass.split('_')])
def _build_orgcoll_import_dicts(self, raw_conf, klass):
imports = []
selfLinkstart = raw_conf[u"selfLink"].partition("?")[0]
items = raw_conf[u"items"]
for item in items:
if item[u"reference"][u"link"].startswith(selfLinkstart):
tempuri = item[u"reference"][u"link"]
pre_questionmark = tempuri.partition("?")[0]
post_selfLink = pre_questionmark[len(selfLinkstart)+1:]
KlassName = self._handle_dashes_dots_capitals(post_selfLink)
print(KlassName)
imports.append({'OC': '.'+klass.lower(),
'module': '.'+KlassName.lower(),
'klass': KlassName})
else:
raise UnexpectedOCItem(item)
return imports
def _build_module_docstring(self, kind, raw_conf):
kind_split = kind.split(':')
conf_dict = {'ORG_COL_DIR': kind_split[1],
'COL_MODULE': kind_split[2],
'REST_URI': raw_conf['selfLink'],
'GUI_PATH': 'XXX',
'KIND_PREFIX': ':'.join(kind_split[:3])}
mod_doc_string = self.templates['module_docstring'].render(**conf_dict)
return mod_doc_string
def _format_org_collection(self, org_match, kind, raw_conf):
KlassName =\
self._handle_dashes_dots_capitals(org_match.group('OrgColl'))
OC_template = self.templates['OrganizingCollection']
import_dicts = self._build_orgcoll_import_dicts(raw_conf, KlassName)
config_dict = {'klass': KlassName,
'kind': kind,
'import_dicts': import_dicts}
OrgCollstr = OC_template.render(**config_dict)
imports_as_str = self.import_template.render(import_dicts=import_dicts)
imps_as_list = imports_as_str.splitlines()
imps_as_list.append(
'from f5.bigip.resource import OrganizingCollection')
imps_as_list.sort()
imports = '\n'.join(imps_as_list)
mdoc_string = self._build_module_docstring(kind, raw_conf)
python_as_string =\
mdoc_string + 3*os.linesep + imports + 3*os.linesep +\
OrgCollstr + os.linesep
self.formatted_configs = {KlassName: {'Python_str': python_as_string,
'config_dict': config_dict,
'import_dicts': import_dicts}}
return python_as_string
def _build_CollectionName_from_KlassName(self, KlassName):
if KlassName.endswith('s'):
CollectionName = KlassName+'_s'
else:
CollectionName = KlassName+'s'
return CollectionName
def _format_resource(self, kind, raw_conf):
raw_string = kind.split(':')[-1][:-len('state')]
assert raw_string == kind.split(':')[-2]
KlassName = self._handle_dashes_dots_capitals(raw_string)
container = self._build_CollectionName_from_KlassName(KlassName)
template = self.templates['Resource']
Resourcestr = template.render(container=container,
klass=KlassName,
kind=kind,
attr_reg_dict={})
import_str = 'from f5.bigip.resource import Resource'
mdoc_string = self._build_module_docstring(kind, raw_conf)
python_as_string =\
mdoc_string + 3*os.linesep + import_str + 3*os.linesep +\
Resourcestr + os.linesep
return python_as_string
def _format_collection(self, kind, raw_conf):
raw_string = kind.split(':')[-1][:-len('collectionstate')]
KlassName = self._handle_dashes_dots_capitals(raw_string)
CollKlassName = self._build_CollectionName_from_KlassName(KlassName)
member_klass = KlassName
collection_template = self.templates['Collection']
memberkind = kind.replace('collection', '')
config_dict = {'coll_klass': CollKlassName,
'member_klass': member_klass,
'collection_container': kind.split(':')[-3],
'collection_attr_reg_dict': {memberkind: member_klass}}
import_str = 'from f5.bigip.resource import Collection'
Collstr = collection_template.render(**config_dict)
mdoc_string = self._build_module_docstring(kind, raw_conf)
python_as_string =\
mdoc_string + 3*os.linesep + import_str + 3*os.linesep +\
Collstr + os.linesep
return python_as_string
def _format_stats(self, kind, raw_conf):
klass = kind.split(':')[-1][:-len('stats')]
print(klass)
template = self.templates['Stats']
print(template)
# XXX
# return python_as_string
def _process_config_with_kind(self, raw_conf):
'''Use this to decide which format is called for by the kind.
NOTE, order matters since subsequent conditions are not evaluated is an
earlier condition is met. In the some cases, e.g. a kind contains both
"stats" and "state", this will become an issue.
'''
kind = raw_conf[u"kind"]
org_match = re.match(self.OC_pattern, kind)
if org_match:
return self._format_org_collection(org_match, kind, raw_conf)
elif 'collectionstate' in kind:
return self._format_collection(kind, raw_conf)
elif kind.endswith('stats'):
return self._format_stats(kind, raw_conf)
elif kind.endswith('state'):
return self._format_resource(kind, raw_conf)