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 |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 16 | from .base import unquote, xproto_string_type |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 17 | import re |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 18 | import sys |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 19 | from six.moves import map |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 20 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 21 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 22 | def django_content_type_string(xptags): |
| 23 | # Check possibility of KeyError in caller |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 24 | content_type = xptags["content_type"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 25 | |
| 26 | try: |
| 27 | content_type = eval(content_type) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 28 | except BaseException: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 29 | pass |
| 30 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | if content_type == "url": |
| 32 | return "URLField" |
| 33 | if content_type == "date": |
| 34 | return "DateTimeField" |
| 35 | elif content_type == "ip": |
| 36 | return "GenericIPAddressField" |
| 37 | elif content_type == "stripped" or content_type == '"stripped"': |
| 38 | return "StrippedCharField" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 39 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 40 | raise Exception("Unknown Type: %s" % content_type) |
| 41 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 42 | |
| 43 | def django_string_type(xptags): |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 44 | # xproto_string_type will return "string" if the options.text=False or "text" if options.text=True |
| 45 | xtype = xproto_string_type(xptags) |
| 46 | if xtype == "string": |
| 47 | if "content_type" in xptags: |
| 48 | return django_content_type_string(xptags) |
| 49 | else: |
| 50 | # TODO(smbaker): This is a workaround for incorrect xproto in many services. Prior behavior was to |
| 51 | # toggle between Charfield and Textfield when max_length was unspecified, rather than to require |
| 52 | # max_length to be specified. Remove this workaround as soon as services have been migrated. |
| 53 | if "max_length" in xptags: |
| 54 | return "CharField" |
| 55 | else: |
| 56 | return "TextField" |
| 57 | elif xtype == "text": |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 58 | return "TextField" |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 59 | else: |
| 60 | raise Exception("Unknown xproto_string type %s" % xtype, xptags=xptags) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 61 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 62 | |
| 63 | def xproto_django_type(xptype, xptags): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 64 | if xptype == "string": |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 65 | return django_string_type(xptags) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 66 | elif xptype == "float": |
| 67 | return "FloatField" |
| 68 | elif xptype == "bool": |
| 69 | return "BooleanField" |
| 70 | elif xptype == "uint32": |
| 71 | return "IntegerField" |
| 72 | elif xptype == "int32": |
| 73 | return "IntegerField" |
| 74 | elif xptype == "int64": |
| 75 | return "BigIntegerField" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 76 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 77 | raise Exception("Unknown Type: %s" % xptype) |
| 78 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 79 | |
| 80 | def xproto_django_link_type(f): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 81 | if f["link_type"] == "manytoone": |
| 82 | return "ForeignKey" |
| 83 | elif f["link_type"] == "onetoone": |
| 84 | return "OneToOneField" |
| 85 | elif f["link_type"] == "manytomany": |
| 86 | if f["dst_port"]: |
| 87 | return "ManyToManyField" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 88 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | return "GenericRelation" |
| 90 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 91 | |
| 92 | def map_xproto_to_django(f): |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 93 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 94 | allowed_keys = [ |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 95 | "auto_now_add", |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 96 | "blank", |
| 97 | "choices", |
| 98 | "db_index", |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 99 | "default", |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 100 | "editable", |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 101 | "help_text", |
| 102 | "max_length", |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 103 | "max_value", |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 104 | "min_value", |
| 105 | "null", |
| 106 | "on_delete", |
| 107 | "unique", |
| 108 | "verbose_name", |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 109 | ] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 110 | |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 111 | out = {} # output dictionary |
Scott Baker | 2e856be | 2019-02-07 09:28:09 -0800 | [diff] [blame] | 112 | |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 113 | # filter options dict to only have allowed keys |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 114 | for k, v in f["options"].items(): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 115 | if k in allowed_keys: |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 116 | out[k] = v |
| 117 | |
| 118 | # deal with optional/required modifier fields, and manytomany links |
| 119 | # modifier is not added to "out" dict, but affects blank/null truth |
| 120 | modifier = f["options"].get('modifier') |
| 121 | link_type = f.get("link_type") |
| 122 | |
| 123 | # in some tests, there is no field type |
| 124 | if "type" in f: |
| 125 | field_type = f["type"] |
| 126 | else: |
| 127 | field_type = None |
| 128 | |
| 129 | mod_out = {} |
| 130 | |
| 131 | if modifier == "required": |
| 132 | |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 133 | mod_out["blank"] = 'False' |
Zack Williams | 00e22d6 | 2019-03-01 22:32:13 -0700 | [diff] [blame] | 134 | |
| 135 | if link_type != "manytomany": |
| 136 | mod_out["null"] = 'False' |
| 137 | |
| 138 | elif modifier == "optional": |
| 139 | |
| 140 | mod_out["blank"] = 'True' |
| 141 | |
| 142 | # set defaults on link types |
| 143 | if link_type != "manytomany" and field_type != "bool": |
| 144 | mod_out["null"] = 'True' |
| 145 | |
| 146 | else: |
| 147 | print("map_xproto_to_django - unknown modifier type: %s on %s" % (modifier, f), file=sys.stderr) |
| 148 | |
| 149 | # print an error if there's a field conflict |
| 150 | for kmo in mod_out.keys(): |
| 151 | if kmo in out: |
| 152 | if out[kmo] != mod_out[kmo]: |
| 153 | print("Option '%s' is manually set to value '%s', which " |
| 154 | "conflicts with value '%s' set automatically by modifier on field: %s" % |
| 155 | (kmo, out[kmo], mod_out[kmo], f), file=sys.stderr) |
| 156 | |
| 157 | out.update(mod_out) # overwrite out keys with mod_out |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 158 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 159 | return out |
| 160 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 161 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 162 | def xproto_django_link_options_str(field, dport=None): |
Scott Baker | 2e856be | 2019-02-07 09:28:09 -0800 | [diff] [blame] | 163 | # Note that this function is called for links (ForeignKeys, M2Ms) |
| 164 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 165 | output_dict = map_xproto_to_django(field) |
| 166 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 167 | if dport and (dport == "+" or "+" not in dport): |
| 168 | output_dict["related_name"] = "%r" % dport |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 169 | |
| 170 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 171 | if field["through"]: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 172 | d = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 173 | if isinstance(field["through"], str): |
| 174 | split = field["through"].rsplit(".", 1) |
| 175 | d["name"] = split[-1] |
| 176 | if len(split) == 2: |
| 177 | d["package"] = split[0] |
| 178 | d["fqn"] = "package" + "." + d["name"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 179 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 180 | d["fqn"] = d["name"] |
| 181 | d["package"] = "" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 182 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 183 | d = field["through"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 184 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 185 | if not d["name"].endswith("_" + field["name"]): |
| 186 | output_dict["through"] = "%r" % d["fqn"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 187 | except KeyError: |
| 188 | pass |
| 189 | |
| 190 | return format_options_string(output_dict) |
| 191 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 192 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 193 | def use_native_django_validators(k, v): |
| 194 | |
| 195 | validators_map = { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 196 | "min_value": "MinValueValidator", |
| 197 | "max_value": "MaxValueValidator", |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return "%s(%s)" % (validators_map[k], v) |
| 201 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 202 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 203 | def format_options_string(d): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 204 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 205 | known_validators = ["min_value", "max_value"] |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 206 | validator_lst = [] |
| 207 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 208 | if not d: |
| 209 | return "" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 210 | else: |
| 211 | |
| 212 | lst = [] |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 213 | for k, v in sorted(d.items(), key=lambda t: t[0]): # sorted by key |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 214 | if k in known_validators: |
| 215 | validator_lst.append(use_native_django_validators(k, v)) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 216 | elif isinstance(v, str) and k == "default" and v.endswith('()"'): |
| 217 | lst.append("%s = %s" % (k, v[1:-3])) |
| 218 | elif isinstance(v, str) and v.startswith('"'): |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 219 | try: |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 220 | # unquote the value if necessary |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 221 | tup = eval(v[1:-1]) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 222 | if isinstance(tup, tuple): |
| 223 | lst.append("%s = %r" % (k, tup)) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 224 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 225 | lst.append("%s = %s" % (k, v)) |
| 226 | except BaseException: |
| 227 | lst.append("%s = %s" % (k, v)) |
| 228 | elif isinstance(v, bool): |
| 229 | lst.append("%s = %r" % (k, bool(v))) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 230 | else: |
| 231 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 232 | lst.append("%s = %r" % (k, int(v))) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 233 | except ValueError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 234 | lst.append("%s = %s" % (k, v)) |
| 235 | validator_string = "validators=[%s]" % ", ".join(validator_lst) |
| 236 | option_string = ", ".join(lst) |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 237 | if len(validator_lst) == 0: |
| 238 | return option_string |
| 239 | elif len(lst) == 0: |
| 240 | return validator_string |
| 241 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 242 | return option_string + ", " + validator_string |
| 243 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 244 | |
| 245 | def xproto_django_options_str(field, dport=None): |
Scott Baker | 2e856be | 2019-02-07 09:28:09 -0800 | [diff] [blame] | 246 | # This function is called for non-links (Strings, Ints, Booleans, ...) |
| 247 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 248 | output_dict = map_xproto_to_django(field) |
| 249 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 250 | if dport == "_": |
| 251 | dport = "+" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 252 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 253 | if dport and (dport == "+" or "+" not in dport): |
| 254 | output_dict["related_name"] = "%r" % dport |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 255 | |
| 256 | return format_options_string(output_dict) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 257 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 258 | |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 259 | def xproto_camel_to_underscore(name): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 260 | return re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) |
| 261 | |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 262 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 263 | def xproto_validations(options): |
| 264 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 265 | return [ |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 266 | list(map(str.strip, validation.split(":"))) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 267 | for validation in unquote(options["validators"]).split(",") |
| 268 | ] |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 269 | except KeyError: |
| 270 | return [] |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 271 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 272 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 273 | def xproto_optioned_fields_to_list(fields, option, val): |
| 274 | """ |
| 275 | List all the field that have a particural option |
| 276 | :param fields: (list) an array of message fields |
| 277 | :param option: (string) the option to look for |
| 278 | :param val: (any) the value of the option |
| 279 | :return: list of strings, field names where option is set |
| 280 | """ |
| 281 | |
| 282 | optioned_fields = [] |
| 283 | for f in fields: |
| 284 | option_names = [] |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 285 | for k, v in sorted(f["options"].items(), key=lambda t: t[0]): # sorted by key |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 286 | option_names.append(k) |
| 287 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 288 | if option in option_names and f["options"][option] == val: |
| 289 | optioned_fields.append(f["name"]) |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 290 | |
| 291 | return optioned_fields |
| 292 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 293 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 294 | # TODO |
| 295 | # - in modeldefs add info about this fields |
| 296 | # - update the gui to have this fields as readonly |