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