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 |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import os |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 18 | import jinja2 |
| 19 | import plyxproto.parser as plyxproto |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | import yaml |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 21 | from colorama import Fore |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 22 | import sys |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 23 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 24 | from . import jinja2_extensions |
| 25 | from .proto2xproto import Proto2XProto |
| 26 | from .xos2jinja import XOS2Jinja |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 27 | from .validator import XProtoValidator |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 28 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | loader = jinja2.PackageLoader(__name__, "templates") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 30 | env = jinja2.Environment(loader=loader) |
| 31 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 32 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 33 | class XOSProcessorArgs: |
| 34 | """ Helper class for use cases that want to call XOSProcessor directly, rather than executing xosgenx from the |
| 35 | command line. |
| 36 | """ |
| 37 | |
| 38 | default_rev = False |
| 39 | default_output = None |
| 40 | default_attic = None |
| 41 | default_kvpairs = None |
| 42 | default_write_to_file = None |
| 43 | default_dest_file = None |
| 44 | default_dest_extension = None |
| 45 | default_target = None |
| 46 | default_checkers = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | default_verbosity = ( |
| 48 | 0 |
| 49 | ) # Higher numbers = more verbosity, lower numbers = less verbosity |
| 50 | default_include_models = ( |
| 51 | [] |
| 52 | ) # If neither include_models nor include_apps is specified, then all models will |
| 53 | default_include_apps = [] # be included. |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 54 | default_strict_validation = False |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 55 | default_lint = False |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 56 | |
| 57 | def __init__(self, **kwargs): |
| 58 | # set defaults |
| 59 | self.rev = XOSProcessorArgs.default_rev |
| 60 | self.output = XOSProcessorArgs.default_output |
| 61 | self.attic = XOSProcessorArgs.default_attic |
| 62 | self.kvpairs = XOSProcessorArgs.default_kvpairs |
| 63 | self.verbosity = XOSProcessorArgs.default_verbosity |
| 64 | self.write_to_file = XOSProcessorArgs.default_write_to_file |
| 65 | self.default_dest_file = XOSProcessorArgs.default_dest_file |
| 66 | self.default_dest_extension = XOSProcessorArgs.default_dest_extension |
| 67 | self.default_target = XOSProcessorArgs.default_target |
| 68 | self.default_checkers = XOSProcessorArgs.default_target |
| 69 | self.include_models = XOSProcessorArgs.default_include_models |
| 70 | self.include_apps = XOSProcessorArgs.default_include_apps |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 71 | self.strict_validation = XOSProcessorArgs.default_strict_validation |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 72 | self.lint = XOSProcessorArgs.default_lint |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 73 | |
| 74 | # override defaults with kwargs |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 75 | for (k, v) in kwargs.items(): |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 76 | setattr(self, k, v) |
| 77 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 78 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 79 | class XOSProcessor: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 80 | @staticmethod |
| 81 | def _read_input_from_files(files): |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 82 | """ Read the files and return the combined text read. |
| 83 | |
| 84 | Also returns a list of (line_number, filename) tuples that tell which |
| 85 | starting line corresponds to each file. |
| 86 | """ |
| 87 | line_map = [] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 88 | input = "" |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 89 | for fname in files: |
| 90 | with open(fname) as infile: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 91 | line_map.append((len(input.split("\n")), fname)) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 92 | input += infile.read() |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 93 | return (input, line_map) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 94 | |
| 95 | @staticmethod |
| 96 | def _attach_parser(ast, args): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 97 | if hasattr(args, "rev") and args.rev: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 98 | v = Proto2XProto() |
| 99 | ast.accept(v) |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 100 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 101 | v = XOS2Jinja(args) |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 102 | ast.accept(v) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 103 | return v |
| 104 | |
| 105 | @staticmethod |
| 106 | def _get_template(target): |
| 107 | if not os.path.isabs(target): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 108 | return os.path.abspath( |
| 109 | os.path.dirname(os.path.realpath(__file__)) + "/targets/" + target |
| 110 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 111 | return target |
| 112 | |
| 113 | @staticmethod |
| 114 | def _file_exists(attic): |
| 115 | # NOTE this method can be used in the jinja template |
| 116 | def file_exists2(name): |
| 117 | if attic is not None: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 118 | path = attic + "/" + name |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 119 | else: |
| 120 | path = name |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 121 | return os.path.exists(path) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 122 | |
| 123 | return file_exists2 |
| 124 | |
| 125 | @staticmethod |
| 126 | def _include_file(attic): |
| 127 | # NOTE this method can be used in the jinja template |
| 128 | def include_file2(name): |
| 129 | if attic is not None: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 130 | path = attic + "/" + name |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 131 | else: |
| 132 | path = name |
| 133 | return open(path).read() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 134 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 135 | return include_file2 |
| 136 | |
| 137 | @staticmethod |
| 138 | def _load_jinja2_extensions(os_template_env, attic): |
| 139 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 140 | os_template_env.globals["include_file"] = XOSProcessor._include_file( |
| 141 | attic |
| 142 | ) # Generates a function |
| 143 | os_template_env.globals["file_exists"] = XOSProcessor._file_exists( |
| 144 | attic |
| 145 | ) # Generates a function |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 146 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 147 | os_template_env.filters["yaml"] = yaml.dump |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 148 | for f in dir(jinja2_extensions): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 149 | if f.startswith("xproto"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 150 | os_template_env.globals[f] = getattr(jinja2_extensions, f) |
| 151 | return os_template_env |
| 152 | |
| 153 | @staticmethod |
| 154 | def _add_context(args): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 155 | if not hasattr(args, "kv") or not args.kv: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 156 | return |
| 157 | try: |
| 158 | context = {} |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 159 | for s in args.kv.split(","): |
| 160 | k, val = s.split(":") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 161 | context[k] = val |
| 162 | return context |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 163 | except Exception as e: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 164 | print(e) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 165 | |
| 166 | @staticmethod |
| 167 | def _write_single_file(rendered, dir, dest_file, quiet): |
| 168 | |
| 169 | file_name = "%s/%s" % (dir, dest_file) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 170 | file = open(file_name, "w") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 171 | file.write(rendered) |
| 172 | file.close() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 173 | if not quiet: |
| 174 | print("Saved: %s" % file_name) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 175 | |
| 176 | @staticmethod |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 177 | def _write_file_per_model(rendered, dir, suffix, quiet): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 178 | for m in rendered: |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 179 | file_name = "%s/%s%s" % (dir, m.lower(), suffix) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 180 | if not rendered[m]: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 181 | if not quiet: |
| 182 | print("Not saving %s as it is empty" % file_name) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 183 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 184 | file = open(file_name, "w") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 185 | file.write(rendered[m]) |
| 186 | file.close() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 187 | if not quiet: |
| 188 | print("Saved: %s" % file_name) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 189 | |
| 190 | @staticmethod |
| 191 | def _write_split_target(rendered, dir, quiet): |
| 192 | |
| 193 | lines = rendered.splitlines() |
| 194 | current_buffer = [] |
| 195 | for l in lines: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 196 | if l.startswith("+++"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 197 | |
| 198 | if dir: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 199 | path = dir + "/" + l[4:].lower() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 200 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 201 | fil = open(path, "w") |
| 202 | buf = "\n".join(current_buffer) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 203 | |
| 204 | obuf = buf |
| 205 | |
| 206 | fil.write(obuf) |
| 207 | fil.close() |
| 208 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 209 | if not quiet: |
| 210 | print("Save file to: %s" % path) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 211 | |
| 212 | current_buffer = [] |
| 213 | else: |
| 214 | current_buffer.append(l) |
| 215 | |
| 216 | @staticmethod |
| 217 | def _find_message_by_model_name(messages, model): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 218 | return next((x for x in messages if x["name"] == model), None) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 219 | |
| 220 | @staticmethod |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 221 | def _find_last_nonempty_line(text, pointer): |
| 222 | ne_pointer = pointer |
| 223 | found = False |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 224 | while ne_pointer != 0 and not found: |
| 225 | ne_pointer = text[: (ne_pointer - 1)].rfind("\n") |
| 226 | if ne_pointer < 0: |
| 227 | ne_pointer = 0 |
| 228 | if text[ne_pointer - 1] != "\n": |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 229 | found = True |
| 230 | |
| 231 | return ne_pointer |
| 232 | |
| 233 | @staticmethod |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 234 | def process(args, operator=None): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 235 | # Setting defaults |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 236 | if not hasattr(args, "attic"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 237 | args.attic = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 238 | if not hasattr(args, "write_to_file"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 239 | args.write_to_file = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 240 | if not hasattr(args, "dest_file"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 241 | args.dest_file = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 242 | if not hasattr(args, "dest_extension"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 243 | args.dest_extension = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 244 | if not hasattr(args, "output"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 245 | args.output = None |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 246 | if not hasattr(args, "quiet"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 247 | args.quiet = True |
| 248 | |
| 249 | # Validating |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 250 | if args.write_to_file == "single" and args.dest_file is None: |
| 251 | raise Exception( |
| 252 | "[XosGenX] write_to_file option is specified as 'single' but no dest_file is provided" |
| 253 | ) |
| 254 | if args.write_to_file == "model" and (args.dest_extension is None): |
| 255 | raise Exception( |
| 256 | "[XosGenX] write_to_file option is specified as 'model' but no dest_extension is provided" |
| 257 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 258 | |
| 259 | if args.output is not None and not os.path.isabs(args.output): |
Scott Baker | 63c27ba | 2019-03-01 16:06:15 -0800 | [diff] [blame] | 260 | raise Exception("[XosGenX] The output dir (%s) must be an absolute path!" % args.output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 261 | if args.output is not None and not os.path.isdir(args.output): |
Scott Baker | 63c27ba | 2019-03-01 16:06:15 -0800 | [diff] [blame] | 262 | raise Exception("[XosGenX] The output dir (%s) must be a directory!" % args.output) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 263 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 264 | if hasattr(args, "files"): |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 265 | (inputs, line_map) = XOSProcessor._read_input_from_files(args.files) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 266 | elif hasattr(args, "inputs"): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 267 | inputs = args.inputs |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 268 | line_map = [] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 269 | else: |
| 270 | raise Exception("[XosGenX] No inputs provided!") |
| 271 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 272 | context = XOSProcessor._add_context(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 273 | |
| 274 | parser = plyxproto.ProtobufAnalyzer() |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 275 | try: |
| 276 | ast = parser.parse_string(inputs, debug=0) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 277 | except plyxproto.ParsingError as e: |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 278 | line, start, end = e.error_range |
| 279 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 280 | ptr = XOSProcessor._find_last_nonempty_line(inputs, start) |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 281 | |
| 282 | if start == 0: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 283 | beginning = "" |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 284 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 285 | beginning = inputs[ptr: start - 1] |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 286 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 287 | line_end_char = inputs[start + end:].find("\n") |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 288 | line_end = inputs[line_end_char] |
| 289 | |
| 290 | if e.message: |
| 291 | error = e.message |
| 292 | else: |
| 293 | error = "xproto parsing error" |
| 294 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 295 | print(error + "\n" + Fore.YELLOW + "Line %d:" % line + Fore.WHITE) |
| 296 | print( |
| 297 | beginning |
| 298 | + Fore.YELLOW |
| 299 | + inputs[start - 1: start + end] |
| 300 | + Fore.WHITE |
| 301 | + line_end |
| 302 | ) |
Sapan Bhatia | 85b7101 | 2018-01-12 12:11:19 -0500 | [diff] [blame] | 303 | exit(1) |
| 304 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 305 | v = XOSProcessor._attach_parser(ast, args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 306 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 307 | if args.include_models or args.include_apps: |
| 308 | for message in v.messages: |
| 309 | message["is_included"] = False |
| 310 | if message["name"] in args.include_models: |
| 311 | message["is_included"] = True |
| 312 | else: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 313 | app_label = ( |
| 314 | message.get("options", {}) |
| 315 | .get("app_label") |
| 316 | .strip('"') |
| 317 | ) |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 318 | if app_label in args.include_apps: |
| 319 | message["is_included"] = True |
| 320 | else: |
| 321 | for message in v.messages: |
| 322 | message["is_included"] = True |
| 323 | |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 324 | validator = XProtoValidator(v.models, line_map) |
| 325 | validator.validate() |
| 326 | if validator.errors: |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 327 | if args.strict_validation or (args.verbosity >= 0): |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 328 | validator.print_errors() |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 329 | fatal_errors = [x for x in validator.errors if x["severity"] == "ERROR"] |
| 330 | if fatal_errors and args.strict_validation: |
Scott Baker | 7ae3a8f | 2019-03-05 16:24:14 -0800 | [diff] [blame] | 331 | sys.exit(-1) |
| 332 | |
Scott Baker | 08d1040 | 2019-04-08 16:19:59 -0700 | [diff] [blame] | 333 | if args.lint: |
| 334 | return "" |
| 335 | |
| 336 | if not operator: |
| 337 | operator = args.target |
| 338 | template_path = XOSProcessor._get_template(operator) |
| 339 | else: |
| 340 | template_path = operator |
| 341 | |
| 342 | [template_folder, template_name] = os.path.split(template_path) |
| 343 | os_template_loader = jinja2.FileSystemLoader(searchpath=[template_folder]) |
| 344 | os_template_env = jinja2.Environment(loader=os_template_loader) |
| 345 | os_template_env = XOSProcessor._load_jinja2_extensions( |
| 346 | os_template_env, args.attic |
| 347 | ) |
| 348 | template = os_template_env.get_template(template_name) |
| 349 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 350 | if args.output is not None and args.write_to_file == "model": |
| 351 | rendered = {} |
| 352 | for i, model in enumerate(v.models): |
| 353 | models = {} |
| 354 | models[model] = v.models[model] |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 355 | messages = [XOSProcessor._find_message_by_model_name(v.messages, model)] |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 356 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 357 | rendered[model] = template.render( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 358 | { |
| 359 | "proto": { |
| 360 | "message_table": models, |
| 361 | "messages": messages, |
| 362 | "policies": v.policies, |
| 363 | "message_names": [m["name"] for m in v.messages], |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 364 | }, |
| 365 | "context": context, |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 366 | "options": v.options, |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 367 | } |
| 368 | ) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 369 | if str(v.options.get("legacy", "false")).strip('"').lower() == "true": |
Scott Baker | a33ccb0 | 2018-01-26 13:03:28 -0800 | [diff] [blame] | 370 | suffix = "_decl." + args.dest_extension |
| 371 | else: |
| 372 | suffix = "." + args.dest_extension |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 373 | XOSProcessor._write_file_per_model( |
| 374 | rendered, args.output, suffix, args.quiet |
| 375 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 376 | else: |
| 377 | rendered = template.render( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 378 | { |
| 379 | "proto": { |
| 380 | "message_table": v.models, |
| 381 | "messages": v.messages, |
| 382 | "policies": v.policies, |
| 383 | "message_names": [m["name"] for m in v.messages], |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 384 | }, |
| 385 | "context": context, |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 386 | "options": v.options, |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 387 | } |
| 388 | ) |
| 389 | if args.output is not None and args.write_to_file == "target": |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 390 | XOSProcessor._write_split_target(rendered, args.output, args.quiet) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 391 | elif args.output is not None and args.write_to_file == "single": |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 392 | XOSProcessor._write_single_file( |
| 393 | rendered, args.output, args.dest_file, args.quiet |
| 394 | ) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 395 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 396 | return rendered |