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