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