blob: 40afa480140f715023d36016f363eef72cc77ae0 [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 Bhatiaa3d2e622017-07-31 22:25:55 -040052def xproto_base_def(model_name, base, suffix='', suffix_list=[]):
Sapan Bhatia4e80a262017-05-19 23:10:51 +020053 if (model_name=='XOSBase'):
54 return '(models.Model, PlModelMixIn)'
55 elif (not base):
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070056 return ''
57 else:
Sapan Bhatiaa3d2e622017-07-31 22:25:55 -040058 int_base = [i['name']+suffix for i in base if i['name'] in suffix_list]
59 ext_base = [i['name'] for i in base if i['name'] not in suffix_list]
60 return '(' + ','.join(int_base + ext_base) + ')'
Sapan Bhatiaff1b8fa2017-04-10 19:44:38 -070061
Sapan Bhatia504cc972017-04-27 01:56:28 +020062def xproto_first_non_empty(lst):
63 for l in lst:
64 if l: return l
65
Sapan Bhatia943dad52017-05-19 18:41:01 +020066def xproto_api_type(field):
67 try:
68 if (unquote(field['options']['content_type'])=='date'):
69 return 'float'
70 except KeyError:
71 pass
72
73 return field['type']
74
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020075
76def xproto_base_name(n):
77 # Hack - Refactor NetworkParameter* to make this go away
78 if (n.startswith('NetworkParameter')):
79 return '_'
80
Sapan Bhatia4e80a262017-05-19 23:10:51 +020081 expr = r'^[A-Z]+[a-z]*'
Sapan Bhatiac4f803f2017-04-21 11:50:39 +020082
83 try:
84 match = re.findall(expr, n)[0]
85 except:
86 return '_'
87
88 return match
Sapan Bhatiaae9645c2017-05-05 15:35:54 +020089
Sapan Bhatia943dad52017-05-19 18:41:01 +020090def xproto_base_fields(m, table):
91 fields = []
92
93 for b in m['bases']:
Sapan Bhatia3cfdf632017-06-08 05:14:03 +020094 option1 = b['fqn']
95 try:
96 option2 = m['package'] + '.' + b['name']
97 except TypeError:
98 option2 = option1
Sapan Bhatia943dad52017-05-19 18:41:01 +020099
Sapan Bhatia3cfdf632017-06-08 05:14:03 +0200100 accessor = None
101 if option1 in table: accessor = option1
102 elif option2 in table: accessor = option2
103
104 if accessor:
105 base_fields = xproto_base_fields(table[accessor], table)
106
107 model_fields = table[accessor]['fields']
Sapan Bhatia943dad52017-05-19 18:41:01 +0200108 fields.extend(base_fields)
109 fields.extend(model_fields)
110
111 return fields
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200112
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200113def xproto_base_rlinks(m, table):
114 links = []
115
116 for base in m['bases']:
117 b = base['name']
118 if b in table:
119 base_rlinks = xproto_base_rlinks(table[b], table)
120
121 model_rlinks = table[b]['rlinks']
122 links.extend(base_rlinks)
123 links.extend(model_rlinks)
124
125 return links
126
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200127def xproto_base_links(m, table):
128 links = []
129
Sapan Bhatia3cfdf632017-06-08 05:14:03 +0200130 for base in m['bases']:
131 b = base['name']
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200132 if b in table:
133 base_links = xproto_base_links(table[b], table)
134
135 model_links = table[b]['links']
136 links.extend(base_links)
137 links.extend(model_links)
138 return links
139
Sapan Bhatiad022aeb2017-06-07 15:49:55 +0200140def xproto_string_type(xptags):
141 try:
142 max_length = eval(xptags['max_length'])
143 except:
144 max_length = 1024
145
146 if ('varchar' not in xptags):
147 return 'string'
148 else:
149 return 'text'
150
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700151def xproto_tuplify(nested_list_or_set):
152 if not isinstance(nested_list_or_set, list) and not isinstance(nested_list_or_set, set):
153 return nested_list_or_set
154 else:
155 return tuple([xproto_tuplify(i) for i in nested_list_or_set])
156
157def xproto_field_graph_components(fields, tag='unique_with'):
158 def find_components(graph):
159 pending = set(graph.keys())
160 components = []
161
162 while pending:
163 front = { pending.pop() }
164 component = set()
165
166 while front:
167 node = front.pop()
168 neighbours = graph[node]
169 neighbours-=component # These we have already visited
170 front |= neighbours
171
172 pending-=neighbours
173 component |= neighbours
174
175 components.append(component)
176
177 return components
178
179 field_graph = {}
180 field_names = {f['name'] for f in fields}
181
182 for f in fields:
183 try:
184 tagged_str = unquote(f['options'][tag])
185 tagged_fields = tagged_str.split(',')
186
187 for uf in tagged_fields:
188 if uf not in field_names:
189 raise FieldNotFound('Field %s not found'%uf)
190
191 field_graph.setdefault(f['name'],set()).add(uf)
192 field_graph.setdefault(uf,set()).add(f['name'])
193 except KeyError:
194 pass
195
196 components = find_components(field_graph)
197 return components
198
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200199def xproto_api_opts(field):
200 options = []
201 if 'max_length' in field['options'] and field['type']=='string':
202 options.append('(val).maxLength = %s'%field['options']['max_length'])
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700203
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200204 try:
205 if field['options']['null'] == 'False':
206 options.append('(val).nonNull = true')
207 except KeyError:
208 pass
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700209
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200210 if 'link' in field and 'model' in field['options']:
211 options.append('(foreignKey).modelName = "%s"'%field['options']['model'])
Sapan Bhatiaf7934b52017-06-12 05:04:23 -0700212
Sapan Bhatiacb35e7f2017-05-24 12:17:28 +0200213 if options:
214 options_str = '[' + ', '.join(options) + ']'
215 else:
216 options_str = ''
217
218 return options_str
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700219
Matteo Scandolo3b7857b2017-06-30 16:22:33 -0700220def xproto_tosca_required(null, blank, default=None):
221
222 if null == 'True' or blank == 'True' or default != 'False':
223 return "false"
224 return "true"
225
226def xproto_tosca_field_type(type):
227 """
228 TOSCA requires fields of type 'bool' to be 'boolean'
229 """
230 if type == "bool":
231 return "boolean"
232 else:
233 return type