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 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 104 | # TODO evaluate if setting Null = False for all strings |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 105 | m = { |
| 106 | "modifier": {"optional": True, "required": False, "_targets": ["null", "blank"]} |
| 107 | } |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 108 | out = {} |
| 109 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 110 | for k, v in f["options"].items(): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 111 | if k in allowed_keys: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 112 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 113 | # NOTE this will be used to parse xproto optional/required field prefix |
| 114 | # and apply it to the null and blank fields |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 115 | kv2 = m[k] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 116 | for t in kv2["_targets"]: |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 117 | out[t] = kv2[v] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 118 | except BaseException: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 119 | out[k] = v |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 120 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 121 | return out |
| 122 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 123 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 124 | def xproto_django_link_options_str(field, dport=None): |
| 125 | output_dict = map_xproto_to_django(field) |
| 126 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 127 | if dport and (dport == "+" or "+" not in dport): |
| 128 | output_dict["related_name"] = "%r" % dport |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 129 | |
| 130 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 131 | if field["through"]: |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 132 | d = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 133 | if isinstance(field["through"], str): |
| 134 | split = field["through"].rsplit(".", 1) |
| 135 | d["name"] = split[-1] |
| 136 | if len(split) == 2: |
| 137 | d["package"] = split[0] |
| 138 | d["fqn"] = "package" + "." + d["name"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 139 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 140 | d["fqn"] = d["name"] |
| 141 | d["package"] = "" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 142 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 143 | d = field["through"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 144 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 145 | if not d["name"].endswith("_" + field["name"]): |
| 146 | output_dict["through"] = "%r" % d["fqn"] |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 147 | except KeyError: |
| 148 | pass |
| 149 | |
| 150 | return format_options_string(output_dict) |
| 151 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 152 | |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 153 | def use_native_django_validators(k, v): |
| 154 | |
| 155 | validators_map = { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 156 | "min_value": "MinValueValidator", |
| 157 | "max_value": "MaxValueValidator", |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return "%s(%s)" % (validators_map[k], v) |
| 161 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 162 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 163 | def format_options_string(d): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 164 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 165 | known_validators = ["min_value", "max_value"] |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 166 | validator_lst = [] |
| 167 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 168 | if not d: |
| 169 | return "" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 170 | else: |
| 171 | |
| 172 | lst = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 173 | for k, v in d.items(): |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 174 | if k in known_validators: |
| 175 | validator_lst.append(use_native_django_validators(k, v)) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 176 | elif isinstance(v, str) and k == "default" and v.endswith('()"'): |
| 177 | lst.append("%s = %s" % (k, v[1:-3])) |
| 178 | elif isinstance(v, str) and v.startswith('"'): |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 179 | try: |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 180 | # unquote the value if necessary |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 181 | tup = eval(v[1:-1]) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 182 | if isinstance(tup, tuple): |
| 183 | lst.append("%s = %r" % (k, tup)) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 184 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 185 | lst.append("%s = %s" % (k, v)) |
| 186 | except BaseException: |
| 187 | lst.append("%s = %s" % (k, v)) |
| 188 | elif isinstance(v, bool): |
| 189 | lst.append("%s = %r" % (k, bool(v))) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 190 | else: |
| 191 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 192 | lst.append("%s = %r" % (k, int(v))) |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 193 | except ValueError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 194 | lst.append("%s = %s" % (k, v)) |
| 195 | validator_string = "validators=[%s]" % ", ".join(validator_lst) |
| 196 | option_string = ", ".join(lst) |
Matteo Scandolo | 61a9f20 | 2018-08-01 08:58:13 -0400 | [diff] [blame] | 197 | if len(validator_lst) == 0: |
| 198 | return option_string |
| 199 | elif len(lst) == 0: |
| 200 | return validator_string |
| 201 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 202 | return option_string + ", " + validator_string |
| 203 | |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 204 | |
| 205 | def xproto_django_options_str(field, dport=None): |
| 206 | output_dict = map_xproto_to_django(field) |
| 207 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 208 | if dport == "_": |
| 209 | dport = "+" |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 210 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 211 | if dport and (dport == "+" or "+" not in dport): |
| 212 | output_dict["related_name"] = "%r" % dport |
Sapan Bhatia | d8e4a23 | 2017-07-12 21:20:06 -0400 | [diff] [blame] | 213 | |
| 214 | return format_options_string(output_dict) |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 215 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 216 | |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 217 | def xproto_camel_to_underscore(name): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 218 | return re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name) |
| 219 | |
Sapan Bhatia | 1e02177 | 2017-08-19 02:15:48 -0400 | [diff] [blame] | 220 | |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 221 | def xproto_validations(options): |
| 222 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 223 | return [ |
| 224 | map(str.strip, validation.split(":")) |
| 225 | for validation in unquote(options["validators"]).split(",") |
| 226 | ] |
Sapan Bhatia | 5ea307d | 2017-07-19 00:13:21 -0400 | [diff] [blame] | 227 | except KeyError: |
| 228 | return [] |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 229 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 230 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 231 | def xproto_optioned_fields_to_list(fields, option, val): |
| 232 | """ |
| 233 | List all the field that have a particural option |
| 234 | :param fields: (list) an array of message fields |
| 235 | :param option: (string) the option to look for |
| 236 | :param val: (any) the value of the option |
| 237 | :return: list of strings, field names where option is set |
| 238 | """ |
| 239 | |
| 240 | optioned_fields = [] |
| 241 | for f in fields: |
| 242 | option_names = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 243 | for k, v in f["options"].items(): |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 244 | option_names.append(k) |
| 245 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 246 | if option in option_names and f["options"][option] == val: |
| 247 | optioned_fields.append(f["name"]) |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 248 | |
| 249 | return optioned_fields |
| 250 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 251 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 252 | # TODO |
| 253 | # - in modeldefs add info about this fields |
| 254 | # - update the gui to have this fields as readonly |