Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 16 | from base import * |
| 17 | import pdb |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 18 | import re |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 19 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 20 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 21 | def django_content_type_string(xptags): |
| 22 | # Check possibility of KeyError in caller |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 23 | content_type = xptags["content_type"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 24 | |
| 25 | try: |
| 26 | content_type = eval(content_type) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 27 | except BaseException: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 28 | pass |
| 29 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 30 | if content_type == "url": |
| 31 | return "URLField" |
| 32 | if content_type == "date": |
| 33 | return "DateTimeField" |
| 34 | elif content_type == "ip": |
| 35 | return "GenericIPAddressField" |
| 36 | elif content_type == "stripped" or content_type == '"stripped"': |
| 37 | return "StrippedCharField" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 38 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 39 | raise Exception("Unknown Type: %s" % content_type) |
| 40 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 41 | |
| 42 | def django_string_type(xptags): |
| 43 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 44 | max_length = eval(xptags["max_length"]) |
| 45 | except BaseException: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 46 | max_length = 1024 * 1024 |
| 47 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 48 | if "content_type" in xptags: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 49 | return django_content_type_string(xptags) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 50 | elif max_length < 1024 * 1024: |
| 51 | return "CharField" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 52 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 53 | return "TextField" |
| 54 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 55 | |
| 56 | def xproto_django_type(xptype, xptags): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 57 | if xptype == "string": |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 58 | return django_string_type(xptags) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 59 | elif xptype == "float": |
| 60 | return "FloatField" |
| 61 | elif xptype == "bool": |
| 62 | return "BooleanField" |
| 63 | elif xptype == "uint32": |
| 64 | return "IntegerField" |
| 65 | elif xptype == "int32": |
| 66 | return "IntegerField" |
| 67 | elif xptype == "int64": |
| 68 | return "BigIntegerField" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 69 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 70 | raise Exception("Unknown Type: %s" % xptype) |
| 71 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 72 | |
| 73 | def xproto_django_link_type(f): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 74 | if f["link_type"] == "manytoone": |
| 75 | return "ForeignKey" |
| 76 | elif f["link_type"] == "onetoone": |
| 77 | return "OneToOneField" |
| 78 | elif f["link_type"] == "manytomany": |
| 79 | if f["dst_port"]: |
| 80 | return "ManyToManyField" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 81 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 82 | return "GenericRelation" |
| 83 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 84 | |
| 85 | def map_xproto_to_django(f): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 86 | allowed_keys = [ |
| 87 | "help_text", |
| 88 | "default", |
| 89 | "max_length", |
| 90 | "modifier", |
| 91 | "blank", |
| 92 | "choices", |
| 93 | "db_index", |
| 94 | "null", |
| 95 | "editable", |
| 96 | "on_delete", |
| 97 | "verbose_name", |
| 98 | "auto_now_add", |
| 99 | "unique", |
| 100 | "min_value", |
| 101 | "max_value", |
| 102 | ] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 103 | |
Scott Baker | 2e856be | 2019-02-07 09:28:09 -0800 | [diff] [blame^] | 104 | if f.get("link_type") == "manytomany": |
| 105 | # map for fields that do not support null |
| 106 | m = { |
| 107 | "modifier": {"optional": True, "required": False, "_targets": ["blank"]} |
| 108 | } |
| 109 | else: |
| 110 | # map for fields that do support null |
| 111 | # TODO evaluate if setting Null = False for all strings |
| 112 | m = { |
| 113 | "modifier": {"optional": True, "required": False, "_targets": ["null", "blank"]} |
| 114 | } |
| 115 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 116 | out = {} |
| 117 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 118 | for k, v in f["options"].items(): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 119 | if k in allowed_keys: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 120 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 121 | # NOTE this will be used to parse xproto optional/required field prefix |
| 122 | # and apply it to the null and blank fields |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 123 | kv2 = m[k] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 124 | for t in kv2["_targets"]: |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 125 | out[t] = kv2[v] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 126 | except BaseException: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 127 | out[k] = v |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 128 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 129 | return out |
| 130 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 131 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 132 | def xproto_django_link_options_str(field, dport=None): |
Scott Baker | 2e856be | 2019-02-07 09:28:09 -0800 | [diff] [blame^] | 133 | # Note that this function is called for links (ForeignKeys, M2Ms) |
| 134 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 135 | output_dict = map_xproto_to_django(field) |
| 136 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 137 | if dport and (dport == "+" or "+" not in dport): |
| 138 | output_dict["related_name"] = "%r" % dport |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 139 | |
| 140 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 141 | if field["through"]: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 142 | d = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 143 | if isinstance(field["through"], str): |
| 144 | split = field["through"].rsplit(".", 1) |
| 145 | d["name"] = split[-1] |
| 146 | if len(split) == 2: |
| 147 | d["package"] = split[0] |
| 148 | d["fqn"] = "package" + "." + d["name"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 149 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 150 | d["fqn"] = d["name"] |
| 151 | d["package"] = "" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 152 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 153 | d = field["through"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 154 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 155 | if not d["name"].endswith("_" + field["name"]): |
| 156 | output_dict["through"] = "%r" % d["fqn"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 157 | except KeyError: |
| 158 | pass |
| 159 | |
| 160 | return format_options_string(output_dict) |
| 161 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 162 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 163 | def use_native_django_validators(k, v): |
| 164 | |
| 165 | validators_map = { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 166 | "min_value": "MinValueValidator", |
| 167 | "max_value": "MaxValueValidator", |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return "%s(%s)" % (validators_map[k], v) |
| 171 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 172 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 173 | def format_options_string(d): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 174 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 175 | known_validators = ["min_value", "max_value"] |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 176 | validator_lst = [] |
| 177 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 178 | if not d: |
| 179 | return "" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 180 | else: |
| 181 | |
| 182 | lst = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 183 | for k, v in d.items(): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 184 | if k in known_validators: |
| 185 | validator_lst.append(use_native_django_validators(k, v)) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 186 | elif isinstance(v, str) and k == "default" and v.endswith('()"'): |
| 187 | lst.append("%s = %s" % (k, v[1:-3])) |
| 188 | elif isinstance(v, str) and v.startswith('"'): |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 189 | try: |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 190 | # unquote the value if necessary |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 191 | tup = eval(v[1:-1]) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 192 | if isinstance(tup, tuple): |
| 193 | lst.append("%s = %r" % (k, tup)) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 194 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 195 | lst.append("%s = %s" % (k, v)) |
| 196 | except BaseException: |
| 197 | lst.append("%s = %s" % (k, v)) |
| 198 | elif isinstance(v, bool): |
| 199 | lst.append("%s = %r" % (k, bool(v))) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 200 | else: |
| 201 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 202 | lst.append("%s = %r" % (k, int(v))) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 203 | except ValueError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 204 | lst.append("%s = %s" % (k, v)) |
| 205 | validator_string = "validators=[%s]" % ", ".join(validator_lst) |
| 206 | option_string = ", ".join(lst) |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 207 | if len(validator_lst) == 0: |
| 208 | return option_string |
| 209 | elif len(lst) == 0: |
| 210 | return validator_string |
| 211 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 212 | return option_string + ", " + validator_string |
| 213 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 214 | |
| 215 | def xproto_django_options_str(field, dport=None): |
Scott Baker | 2e856be | 2019-02-07 09:28:09 -0800 | [diff] [blame^] | 216 | # This function is called for non-links (Strings, Ints, Booleans, ...) |
| 217 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 218 | output_dict = map_xproto_to_django(field) |
| 219 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 220 | if dport == "_": |
| 221 | dport = "+" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 222 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 223 | if dport and (dport == "+" or "+" not in dport): |
| 224 | output_dict["related_name"] = "%r" % dport |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 225 | |
| 226 | return format_options_string(output_dict) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 227 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 228 | |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 229 | def xproto_camel_to_underscore(name): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 230 | return re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) |
| 231 | |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 232 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 233 | def xproto_validations(options): |
| 234 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 235 | return [ |
| 236 | map(str.strip, validation.split(":")) |
| 237 | for validation in unquote(options["validators"]).split(",") |
| 238 | ] |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 239 | except KeyError: |
| 240 | return [] |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 241 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 242 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 243 | def xproto_optioned_fields_to_list(fields, option, val): |
| 244 | """ |
| 245 | List all the field that have a particural option |
| 246 | :param fields: (list) an array of message fields |
| 247 | :param option: (string) the option to look for |
| 248 | :param val: (any) the value of the option |
| 249 | :return: list of strings, field names where option is set |
| 250 | """ |
| 251 | |
| 252 | optioned_fields = [] |
| 253 | for f in fields: |
| 254 | option_names = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 255 | for k, v in f["options"].items(): |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 256 | option_names.append(k) |
| 257 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 258 | if option in option_names and f["options"][option] == val: |
| 259 | optioned_fields.append(f["name"]) |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 260 | |
| 261 | return optioned_fields |
| 262 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 263 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 264 | # TODO |
| 265 | # - in modeldefs add info about this fields |
| 266 | # - update the gui to have this fields as readonly |