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