blob: 3cb988626974d2f1590ad5fb2ba294bc33976639 [file] [log] [blame]
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -07001import pdb
Sapan Bhatiac4f803f2017-04-21 11:50:39 +02002import re
Sapan Bhatia7886e122017-05-17 11:19:39 +02003from pattern import en
4
Sapan Bhatiaf7934b52017-06-12 05:04:23 -07005class FieldNotFound(Exception):
6 def __init__(self, message):
7 super(FieldNotFound, self).__init__(message)
8
Sapan Bhatiad4567592017-07-24 17:26:26 -04009def xproto_debug(**kwargs):
10 print kwargs
11 pdb.set_trace()
12
Sapan Bhatia943dad52017-05-19 18:41:01 +020013def xproto_unquote(s):
14 return unquote(s)
15
Sapan Bhatia49b54ae2017-05-19 17:11:32 +020016def unquote(s):
17 if (s.startswith('"') and s.endswith('"')):
18 return s[1:-1]
Matteo Scandolo292cc2a2017-07-31 19:02:12 -070019 else:
20 return s
Sapan Bhatia49b54ae2017-05-19 17:11:32 +020021
22def xproto_singularize(field):
23 try:
24 # The user has set a singular, as an exception that cannot be handled automatically
25 singular = field['options']['singular']
26 singular = unquote(singular)
27 except KeyError:
28 singular = en.singularize(field['name'])
29
30 return singular
31
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +020032def xproto_singularize_pluralize(field):
33 try:
34 # The user has set a plural, as an exception that cannot be handled automatically
35 plural = field['options']['plural']
36 plural = unquote(plural)
37 except KeyError:
38 plural = en.pluralize(en.singularize(field['name']))
39
40 return plural
41
Sapan Bhatia7886e122017-05-17 11:19:39 +020042def xproto_pluralize(field):
43 try:
44 # The user has set a plural, as an exception that cannot be handled automatically
45 plural = field['options']['plural']
Sapan Bhatia49b54ae2017-05-19 17:11:32 +020046 plural = unquote(plural)
Sapan Bhatia7886e122017-05-17 11:19:39 +020047 except KeyError:
48 plural = en.pluralize(field['name'])
49
50 return plural
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020051
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020052def xproto_links_to_modeldef_relations(llst):
53 outlist = []
54 seen = []
55 for l in llst:
56 try:
57 t = l['link_type']
58 except KeyError, e:
59 raise e
60
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020061 if l['peer']['fqn'] not in seen and t!='manytomany':
62 outlist.append('- {model: %s, type: %s}\n'%(l['peer']['name'], l['link_type']))
Sapan Bhatiad022aeb2017-06-07 15:49:55 +020063 seen.append(l['peer'])
64
65 return outlist
66
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070067
Sapan Bhatiaa3d2e622017-07-31 22:25:55 -040068def xproto_base_def(model_name, base, suffix='', suffix_list=[]):
Sapan Bhatia4e80a262017-05-19 23:10:51 +020069 if (model_name=='XOSBase'):
70 return '(models.Model, PlModelMixIn)'
71 elif (not base):
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070072 return ''
73 else:
Sapan Bhatiaa3d2e622017-07-31 22:25:55 -040074 int_base = [i['name']+suffix for i in base if i['name'] in suffix_list]
75 ext_base = [i['name'] for i in base if i['name'] not in suffix_list]
76 return '(' + ','.join(int_base + ext_base) + ')'
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070077
Sapan Bhatia504cc972017-04-27 01:56:28 +020078def xproto_first_non_empty(lst):
79 for l in lst:
80 if l: return l
81
Sapan Bhatia943dad52017-05-19 18:41:01 +020082def xproto_api_type(field):
83 try:
84 if (unquote(field['options']['content_type'])=='date'):
85 return 'float'
86 except KeyError:
87 pass
88
89 return field['type']
90
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020091
92def xproto_base_name(n):
93 # Hack - Refactor NetworkParameter* to make this go away
94 if (n.startswith('NetworkParameter')):
95 return '_'
96
Sapan Bhatia4e80a262017-05-19 23:10:51 +020097 expr = r'^[A-Z]+[a-z]*'
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020098
99 try:
100 match = re.findall(expr, n)[0]
101 except:
102 return '_'
103
104 return match
Sapan Bhatiaae9645c2017-05-05 15:35:54 +0200105
Sapan Bhatia943dad52017-05-19 18:41:01 +0200106def xproto_base_fields(m, table):
107 fields = []
108
109 for b in m['bases']:
Sapan Bhatia3cfdf632017-06-08 05:14:03 +0200110 option1 = b['fqn']
111 try:
112 option2 = m['package'] + '.' + b['name']
113 except TypeError:
114 option2 = option1
Sapan Bhatia943dad52017-05-19 18:41:01 +0200115
Sapan Bhatia3cfdf632017-06-08 05:14:03 +0200116 accessor = None
117 if option1 in table: accessor = option1
118 elif option2 in table: accessor = option2
119
120 if accessor:
121 base_fields = xproto_base_fields(table[accessor], table)
122
123 model_fields = table[accessor]['fields']
Sapan Bhatia943dad52017-05-19 18:41:01 +0200124 fields.extend(base_fields)
125 fields.extend(model_fields)
126
127 return fields
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200128
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200129def xproto_base_rlinks(m, table):
130 links = []
131
132 for base in m['bases']:
133 b = base['name']
134 if b in table:
135 base_rlinks = xproto_base_rlinks(table[b], table)
136
137 model_rlinks = table[b]['rlinks']
138 links.extend(base_rlinks)
139 links.extend(model_rlinks)
140
141 return links
142
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200143def xproto_base_links(m, table):
144 links = []
145
Sapan Bhatia3cfdf632017-06-08 05:14:03 +0200146 for base in m['bases']:
147 b = base['name']
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200148 if b in table:
149 base_links = xproto_base_links(table[b], table)
150
151 model_links = table[b]['links']
152 links.extend(base_links)
153 links.extend(model_links)
154 return links
155
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200156def xproto_string_type(xptags):
157 try:
158 max_length = eval(xptags['max_length'])
159 except:
160 max_length = 1024
161
162 if ('varchar' not in xptags):
163 return 'string'
164 else:
165 return 'text'
166
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700167def xproto_tuplify(nested_list_or_set):
168 if not isinstance(nested_list_or_set, list) and not isinstance(nested_list_or_set, set):
169 return nested_list_or_set
170 else:
171 return tuple([xproto_tuplify(i) for i in nested_list_or_set])
172
173def xproto_field_graph_components(fields, tag='unique_with'):
174 def find_components(graph):
175 pending = set(graph.keys())
176 components = []
177
178 while pending:
179 front = { pending.pop() }
180 component = set()
181
182 while front:
183 node = front.pop()
184 neighbours = graph[node]
185 neighbours-=component # These we have already visited
186 front |= neighbours
187
188 pending-=neighbours
189 component |= neighbours
190
191 components.append(component)
192
193 return components
194
195 field_graph = {}
196 field_names = {f['name'] for f in fields}
197
198 for f in fields:
199 try:
200 tagged_str = unquote(f['options'][tag])
201 tagged_fields = tagged_str.split(',')
202
203 for uf in tagged_fields:
204 if uf not in field_names:
205 raise FieldNotFound('Field %s not found'%uf)
206
207 field_graph.setdefault(f['name'],set()).add(uf)
208 field_graph.setdefault(uf,set()).add(f['name'])
209 except KeyError:
210 pass
211
212 components = find_components(field_graph)
213 return components
214
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200215def xproto_api_opts(field):
216 options = []
217 if 'max_length' in field['options'] and field['type']=='string':
218 options.append('(val).maxLength = %s'%field['options']['max_length'])
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700219
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200220 try:
221 if field['options']['null'] == 'False':
222 options.append('(val).nonNull = true')
223 except KeyError:
224 pass
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700225
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200226 if 'link' in field and 'model' in field['options']:
227 options.append('(foreignKey).modelName = "%s"'%field['options']['model'])
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700228
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200229 if options:
230 options_str = '[' + ', '.join(options) + ']'
231 else:
232 options_str = ''
233
234 return options_str
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700235
Matteo Scandolo3b7857b2017-06-30 16:22:33 -0700236def xproto_tosca_required(null, blank, default=None):
237
238 if null == 'True' or blank == 'True' or default != 'False':
239 return "false"
240 return "true"
241
242def xproto_tosca_field_type(type):
243 """
244 TOSCA requires fields of type 'bool' to be 'boolean'
245 """
246 if type == "bool":
247 return "boolean"
248 else:
249 return type