SEBA-419 allow legacy to be specified on a per-model basis;
Deprecate "legacy" in favor of "custom_python"

Change-Id: I44625ff18ee7796996ae25ad700e58a0b36e7398
diff --git a/lib/xos-genx/xosgenx/generator.py b/lib/xos-genx/xosgenx/generator.py
index d1dc99a..278a309 100644
--- a/lib/xos-genx/xosgenx/generator.py
+++ b/lib/xos-genx/xosgenx/generator.py
@@ -174,20 +174,6 @@
             print("Saved: %s" % file_name)
 
     @staticmethod
-    def _write_file_per_model(rendered, dir, suffix, quiet):
-        for m in rendered:
-            file_name = "%s/%s%s" % (dir, m.lower(), suffix)
-            if not rendered[m]:
-                if not quiet:
-                    print("Not saving %s as it is empty" % file_name)
-            else:
-                file = open(file_name, "w")
-                file.write(rendered[m])
-                file.close()
-                if not quiet:
-                    print("Saved: %s" % file_name)
-
-    @staticmethod
     def _write_split_target(rendered, dir, quiet):
 
         lines = rendered.splitlines()
@@ -348,16 +334,17 @@
         template = os_template_env.get_template(template_name)
 
         if args.output is not None and args.write_to_file == "model":
+            # Handle the case where each model is written to a separate python file.
             rendered = {}
+
             for i, model in enumerate(v.models):
-                models = {}
-                models[model] = v.models[model]
+                model_dict = v.models[model]
                 messages = [XOSProcessor._find_message_by_model_name(v.messages, model)]
 
                 rendered[model] = template.render(
                     {
                         "proto": {
-                            "message_table": models,
+                            "message_table": {model: model_dict},
                             "messages": messages,
                             "policies": v.policies,
                             "message_names": [m["name"] for m in v.messages],
@@ -366,14 +353,28 @@
                         "options": v.options,
                     }
                 )
-            if str(v.options.get("legacy", "false")).strip('"').lower() == "true":
-                suffix = "_decl." + args.dest_extension
-            else:
-                suffix = "." + args.dest_extension
-            XOSProcessor._write_file_per_model(
-                rendered, args.output, suffix, args.quiet
-            )
+                if not rendered[model]:
+                    print("Not saving model %s as it is empty" % model, file=sys.stderr)
+                else:
+                    legacy = jinja2_extensions.base.xproto_list_evaluates_true(
+                        [model_dict.get("options", {}).get("custom_python", None),
+                         model_dict.get("options", {}).get("legacy", None),
+                         v.options.get("custom_python", None),
+                         v.options.get("legacy", None)])
+
+                    if legacy:
+                        file_name = "%s/%s_decl.%s" % (args.output, model.lower(), args.dest_extension)
+                    else:
+                        file_name = "%s/%s.%s" % (args.output, model.lower(), args.dest_extension)
+
+                    file = open(file_name, "w")
+                    file.write(rendered[model])
+                    file.close()
+                    if not args.quiet:
+                        print("Saved: %s" % file_name, file=sys.stderr)
         else:
+            # Handle the case where all models are written to the same python file.
+
             rendered = template.render(
                 {
                     "proto": {