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