Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 16 | from __future__ import print_function |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 17 | import pdb |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 18 | import re |
Scott Baker | 391f5d8 | 2018-10-02 16:34:41 -0700 | [diff] [blame] | 19 | from inflect import engine as inflect_engine_class |
| 20 | |
| 21 | inflect_engine = inflect_engine_class() |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 22 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 23 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 24 | class FieldNotFound(Exception): |
| 25 | def __init__(self, message): |
| 26 | super(FieldNotFound, self).__init__(message) |
| 27 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 28 | |
Sapan Bhatia | d456759 | 2017-07-24 17:26:26 -0400 | [diff] [blame] | 29 | def xproto_debug(**kwargs): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 30 | print(kwargs) |
Sapan Bhatia | d456759 | 2017-07-24 17:26:26 -0400 | [diff] [blame] | 31 | pdb.set_trace() |
| 32 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 33 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 34 | def xproto_unquote(s): |
| 35 | return unquote(s) |
| 36 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 37 | |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 38 | def unquote(s): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 39 | if s.startswith('"') and s.endswith('"'): |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 40 | return s[1:-1] |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 41 | else: |
| 42 | return s |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 43 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 44 | |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 45 | def xproto_singularize(field): |
| 46 | try: |
| 47 | # The user has set a singular, as an exception that cannot be handled automatically |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 48 | singular = field["options"]["singular"] |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 49 | singular = unquote(singular) |
| 50 | except KeyError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 51 | singular = inflect_engine.singular_noun(field["name"]) |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 52 | if singular is False: |
| 53 | # singular_noun returns False on a noun it can't singularize |
| 54 | singular = field["name"] |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 55 | |
| 56 | return singular |
| 57 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 58 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 59 | def xproto_singularize_pluralize(field): |
| 60 | try: |
| 61 | # The user has set a plural, as an exception that cannot be handled automatically |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 62 | plural = field["options"]["plural"] |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 63 | plural = unquote(plural) |
| 64 | except KeyError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 65 | singular = inflect_engine.singular_noun(field["name"]) |
Scott Baker | a1b089a | 2018-10-05 09:59:17 -0700 | [diff] [blame] | 66 | if singular is False: |
| 67 | # singular_noun returns False on a noun it can't singularize |
| 68 | singular = field["name"] |
| 69 | |
| 70 | plural = inflect_engine.plural_noun(singular) |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 71 | |
| 72 | return plural |
| 73 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 74 | |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 75 | def xproto_pluralize(field): |
| 76 | try: |
| 77 | # The user has set a plural, as an exception that cannot be handled automatically |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 78 | plural = field["options"]["plural"] |
Sapan Bhatia | 49b54ae | 2017-05-19 17:11:32 +0200 | [diff] [blame] | 79 | plural = unquote(plural) |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 80 | except KeyError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 81 | plural = inflect_engine.plural_noun(field["name"]) |
Sapan Bhatia | 7886e12 | 2017-05-17 11:19:39 +0200 | [diff] [blame] | 82 | |
| 83 | return plural |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 84 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 85 | |
| 86 | def xproto_base_def(model_name, base, suffix="", suffix_list=[]): |
| 87 | if model_name == "XOSBase": |
| 88 | return "(models.Model, PlModelMixIn)" |
| 89 | elif not base: |
| 90 | return "" |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 91 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 92 | int_base = [i["name"] + suffix for i in base if i["name"] in suffix_list] |
| 93 | ext_base = [i["name"] for i in base if i["name"] not in suffix_list] |
| 94 | return "(" + ",".join(int_base + ext_base) + ")" |
| 95 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 96 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 97 | def xproto_first_non_empty(lst): |
| 98 | for l in lst: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 99 | if l: |
| 100 | return l |
| 101 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 102 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 103 | def xproto_api_type(field): |
| 104 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 105 | if unquote(field["options"]["content_type"]) == "date": |
| 106 | return "double" |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 107 | except KeyError: |
| 108 | pass |
| 109 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 110 | return field["type"] |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 111 | |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 112 | |
| 113 | def xproto_base_name(n): |
| 114 | # Hack - Refactor NetworkParameter* to make this go away |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 115 | if n.startswith("NetworkParameter"): |
| 116 | return "_" |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 117 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 118 | expr = r"^[A-Z]+[a-z]*" |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 119 | |
| 120 | try: |
| 121 | match = re.findall(expr, n)[0] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 122 | except BaseException: |
| 123 | return "_" |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 124 | |
| 125 | return match |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 126 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 127 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 128 | def xproto_base_fields(m, table): |
| 129 | fields = [] |
| 130 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 131 | for b in m["bases"]: |
| 132 | option1 = b["fqn"] |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 133 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 134 | option2 = m["package"] + "." + b["name"] |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 135 | except TypeError: |
| 136 | option2 = option1 |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 137 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 138 | accessor = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 139 | if option1 in table: |
| 140 | accessor = option1 |
| 141 | elif option2 in table: |
| 142 | accessor = option2 |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 143 | |
| 144 | if accessor: |
| 145 | base_fields = xproto_base_fields(table[accessor], table) |
| 146 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 147 | model_fields = [x.copy() for x in table[accessor]["fields"]] |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 148 | for field in model_fields: |
| 149 | field["accessor"] = accessor |
| 150 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 151 | fields.extend(base_fields) |
| 152 | fields.extend(model_fields) |
| 153 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 154 | if "no_sync" in m["options"] and m["options"]["no_sync"]: |
| 155 | fields = [ |
| 156 | f |
| 157 | for f in fields |
| 158 | if f["name"] != "backend_status" and f["name"] != "backend_code" |
| 159 | ] |
Matteo Scandolo | 39b4a27 | 2017-11-17 11:09:21 -0800 | [diff] [blame] | 160 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 161 | if "no_policy" in m["options"] and m["options"]["no_policy"]: |
| 162 | fields = [ |
| 163 | f |
| 164 | for f in fields |
| 165 | if f["name"] != "policy_status" and f["name"] != "policy_code" |
| 166 | ] |
Matteo Scandolo | 39b4a27 | 2017-11-17 11:09:21 -0800 | [diff] [blame] | 167 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 168 | return fields |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 169 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 170 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 171 | def xproto_fields(m, table): |
| 172 | """ Generate the full list of models for the xproto message `m` including fields from the classes it inherits. |
| 173 | |
| 174 | Inserts the special field "id" at the very beginning. |
| 175 | |
| 176 | Each time we descend a new level of inheritance, increment the offset field numbers by 100. The base |
| 177 | class's fields will be numbered from 1-99, the first descendant will be number 100-199, the second |
| 178 | descdendant numbered from 200-299, and so on. This assumes any particular model as at most 100 |
| 179 | fields. |
| 180 | """ |
| 181 | |
| 182 | model_fields = [x.copy() for x in m["fields"]] |
| 183 | for field in model_fields: |
| 184 | field["accessor"] = m["fqn"] |
| 185 | |
| 186 | fields = xproto_base_fields(m, table) + model_fields |
| 187 | |
| 188 | # The "id" field is a special field. Every model has one. Put it up front and pretend it's part of the |
| 189 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 190 | if not fields: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 191 | raise Exception( |
| 192 | "Model %s has no fields. Check for missing base class." % m["name"] |
| 193 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 194 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 195 | id_field = { |
| 196 | "type": "int32", |
| 197 | "name": "id", |
| 198 | "options": {}, |
| 199 | "id": "1", |
| 200 | "accessor": fields[0]["accessor"], |
| 201 | } |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 202 | |
| 203 | fields = [id_field] + fields |
| 204 | |
| 205 | # Walk through the list of fields. They will be in depth-first search order from the base model forward. Each time |
| 206 | # the model changes, offset the protobuf field numbers by 100. |
| 207 | offset = 0 |
| 208 | last_accessor = fields[0]["accessor"] |
| 209 | for field in fields: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 210 | if field["accessor"] != last_accessor: |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 211 | last_accessor = field["accessor"] |
| 212 | offset += 100 |
| 213 | field_id = int(field["id"]) |
| 214 | if (field_id < 1) or (field_id >= 100): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 215 | raise Exception( |
| 216 | "Only field numbers from 1 to 99 are permitted, field %s in model %s" |
| 217 | % (field["name"], field["accessor"]) |
| 218 | ) |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 219 | field["id"] = int(field["id"]) + offset |
| 220 | |
| 221 | # Check for duplicates |
| 222 | fields_by_number = {} |
| 223 | for field in fields: |
| 224 | id = field["id"] |
| 225 | dup = fields_by_number.get(id) |
| 226 | if dup: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 227 | raise Exception( |
| 228 | "Field %s has duplicate number %d with field %s in model %s" |
| 229 | % (field["name"], id, dup["name"], field["accessor"]) |
| 230 | ) |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 231 | fields_by_number[id] = field |
| 232 | |
| 233 | return fields |
| 234 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 235 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 236 | def xproto_base_rlinks(m, table): |
| 237 | links = [] |
| 238 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 239 | for base in m["bases"]: |
| 240 | b = base["name"] |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 241 | if b in table: |
| 242 | base_rlinks = xproto_base_rlinks(table[b], table) |
| 243 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 244 | model_rlinks = [x.copy() for x in table[b]["rlinks"]] |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 245 | for link in model_rlinks: |
| 246 | link["accessor"] = b |
| 247 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 248 | links.extend(base_rlinks) |
| 249 | links.extend(model_rlinks) |
| 250 | |
| 251 | return links |
| 252 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 253 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 254 | def xproto_rlinks(m, table): |
| 255 | """ Return the reverse links for the xproto message `m`. |
| 256 | |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 257 | If the link includes a reverse_id, then it will be used for the protobuf field id. Each level of inheritance |
| 258 | will add an offset of 100 to the supplied reverse_id. |
| 259 | |
| 260 | If there is no reverse_id, then one will automatically be allocated started at id 1900. It is encouraged that |
| 261 | all links include reverse_ids, so that field identifiers are deterministic across all protobuf messages. |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 262 | """ |
| 263 | |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 264 | model_rlinks = [x.copy() for x in m["rlinks"]] |
| 265 | for link in model_rlinks: |
| 266 | link["accessor"] = m["fqn"] |
| 267 | |
| 268 | links = xproto_base_rlinks(m, table) + model_rlinks |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 269 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 270 | links = [ |
| 271 | x for x in links if ("+" not in x["src_port"]) and ("+" not in x["dst_port"]) |
| 272 | ] |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 273 | |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 274 | if links: |
| 275 | last_accessor = links[0]["accessor"] |
| 276 | offset = 0 |
| 277 | index = 1900 |
| 278 | for link in links: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 279 | if link["accessor"] != last_accessor: |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 280 | last_accessor = link["accessor"] |
| 281 | offset += 100 |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 282 | |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 283 | if link["reverse_id"]: |
| 284 | # Statically numbered reverse links. Use the id that the developer supplied, adding the offset based on |
| 285 | # inheritance depth. |
| 286 | link["id"] = int(link["reverse_id"]) + offset |
| 287 | else: |
| 288 | # Automatically numbered reverse links. These will eventually go away. |
| 289 | link["id"] = index |
| 290 | index += 1 |
| 291 | |
| 292 | # check for duplicates |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 293 | links_by_number = {} |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 294 | for link in links: |
| 295 | id = link["id"] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 296 | dup = links_by_number.get(id) |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 297 | if dup: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 298 | raise Exception( |
| 299 | "Field %s has duplicate number %d in model %s with reverse field %s" |
| 300 | % (link["src_port"], id, m["name"], dup["src_port"]) |
| 301 | ) |
Scott Baker | d87c02a | 2018-10-29 16:24:29 -0700 | [diff] [blame] | 302 | links_by_number[id] = link |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 303 | |
| 304 | return links |
| 305 | |
| 306 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 307 | def xproto_base_links(m, table): |
| 308 | links = [] |
| 309 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 310 | for base in m["bases"]: |
| 311 | b = base["name"] |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 312 | if b in table: |
| 313 | base_links = xproto_base_links(table[b], table) |
| 314 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 315 | model_links = table[b]["links"] |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 316 | links.extend(base_links) |
| 317 | links.extend(model_links) |
| 318 | return links |
| 319 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 320 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 321 | def xproto_string_type(xptags): |
| 322 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 323 | max_length = eval(xptags["max_length"]) |
| 324 | except BaseException: |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 325 | max_length = 1024 |
| 326 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 327 | if "varchar" not in xptags: |
| 328 | return "string" |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 329 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 330 | return "text" |
| 331 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 332 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 333 | def xproto_tuplify(nested_list_or_set): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 334 | if not isinstance(nested_list_or_set, list) and not isinstance( |
| 335 | nested_list_or_set, set |
| 336 | ): |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 337 | return nested_list_or_set |
| 338 | else: |
| 339 | return tuple([xproto_tuplify(i) for i in nested_list_or_set]) |
| 340 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 341 | |
| 342 | def xproto_field_graph_components(fields, model, tag="unique_with"): |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 343 | def find_components(graph): |
| 344 | pending = set(graph.keys()) |
| 345 | components = [] |
| 346 | |
| 347 | while pending: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 348 | front = {pending.pop()} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 349 | component = set() |
| 350 | |
| 351 | while front: |
| 352 | node = front.pop() |
| 353 | neighbours = graph[node] |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 354 | neighbours -= component # These we have already visited |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 355 | front |= neighbours |
| 356 | |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 357 | pending -= neighbours |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 358 | component |= neighbours |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 359 | |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 360 | components.append(component) |
| 361 | |
| 362 | return components |
| 363 | |
| 364 | field_graph = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 365 | field_names = {f["name"] for f in fields} |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 366 | |
| 367 | for f in fields: |
| 368 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 369 | tagged_str = unquote(f["options"][tag]) |
| 370 | tagged_fields = tagged_str.split(",") |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 371 | |
| 372 | for uf in tagged_fields: |
| 373 | if uf not in field_names: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 374 | raise FieldNotFound( |
| 375 | 'Field "%s" not found in model "%s", referenced from field "%s" by option "%s"' |
| 376 | % (uf, model["name"], f["name"], tag) |
| 377 | ) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 378 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 379 | field_graph.setdefault(f["name"], set()).add(uf) |
| 380 | field_graph.setdefault(uf, set()).add(f["name"]) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 381 | except KeyError: |
| 382 | pass |
| 383 | |
Matteo Scandolo | a17e6e4 | 2018-05-25 10:28:25 -0700 | [diff] [blame] | 384 | return find_components(field_graph) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 385 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 386 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 387 | def xproto_api_opts(field): |
| 388 | options = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 389 | if "max_length" in field["options"] and field["type"] == "string": |
| 390 | options.append("(val).maxLength = %s" % field["options"]["max_length"]) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 391 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 392 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 393 | if field["options"]["null"] == "False": |
| 394 | options.append("(val).nonNull = true") |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 395 | except KeyError: |
| 396 | pass |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 397 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 398 | if "link" in field and "model" in field["options"]: |
| 399 | options.append('(foreignKey).modelName = "%s"' % field["options"]["model"]) |
Scott Baker | c4156c3 | 2017-12-08 10:58:21 -0800 | [diff] [blame] | 400 | if ("options" in field) and ("port" in field["options"]): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 401 | options.append( |
| 402 | '(foreignKey).reverseFieldName = "%s"' % field["options"]["port"] |
| 403 | ) |
Sapan Bhatia | f7934b5 | 2017-06-12 05:04:23 -0700 | [diff] [blame] | 404 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 405 | if options: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 406 | options_str = "[" + ", ".join(options) + "]" |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 407 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 408 | options_str = "" |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 409 | |
| 410 | return options_str |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 411 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 412 | |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 413 | def xproto_type_to_swagger_type(f): |
| 414 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 415 | content_type = f["options"]["content_type"] |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 416 | content_type = eval(content_type) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 417 | except BaseException: |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 418 | content_type = None |
| 419 | pass |
| 420 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 421 | if "choices" in f["options"]: |
| 422 | return "string" |
| 423 | elif content_type == "date": |
| 424 | return "string" |
| 425 | elif f["type"] == "bool": |
| 426 | return "boolean" |
| 427 | elif f["type"] == "string": |
| 428 | return "string" |
| 429 | elif f["type"] in ["int", "uint32", "int32"] or "link" in f: |
| 430 | return "integer" |
| 431 | elif f["type"] in ["double", "float"]: |
| 432 | return "string" |
| 433 | |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 434 | |
| 435 | def xproto_field_to_swagger_enum(f): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 436 | if "choices" in f["options"]: |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 437 | list = [] |
| 438 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 439 | for c in eval(xproto_unquote(f["options"]["choices"])): |
Matteo Scandolo | 431781c | 2017-09-06 15:33:07 -0700 | [diff] [blame] | 440 | list.append(c[0]) |
| 441 | |
| 442 | return list |
| 443 | else: |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 444 | return False |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 445 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 446 | |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 447 | def xproto_is_true(x): |
| 448 | # TODO: Audit xproto and make specification of trueness more uniform |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 449 | if x is True or (x == "True") or (x == '"True"'): |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 450 | return True |
| 451 | return False |