Commits

Christian Krebs committed 254de69

versioned the message maps

Comments (0)

Files changed (6)

              }
     return ptypes[field.type]
 
+class TemplateEnv(object):
+    def __init__(self, lookup, out_dir, js_test_framework, console_logger_tutorial):
+        self.lookup = lookup
+        self.out_dir = out_dir
+        self.js_test_framework = js_test_framework
+        self.console_logger_tutorial = console_logger_tutorial
+
 def const_id(field, name=""): 
     return dashed_name(field.message and name or field.name, '_').upper()
 
     outfile.close()
     print "Wrote service-interface %s to '%s'" % (service.name, fname)
 
-
-def generate_service_implementation(service, lookup, out_dir, js_test_framework, console_logger_tutorial):
-    if "version" not in service.options:
-        raise Exception("Option 'version' is not set on service %s" % service.name)
-    version = service.options["version"].value
-    fname = os.path.join(
-                out_dir, 
-                LIB_DIR,
-                "%s_%s.js" % (
-                    dashed_name(service.name, dash="_"), 
-                    version.replace('.', '_')
-                    )
-                )
-    outfile = open(fname, "wb")
+def generate_service_implementation(service, template_env):
+    version = service.options["version"].value.replace('.', '_')
+    service_name = dashed_name(service.name, dash="_")
+    fname = "%s_%s.js" % (service_name, version)
+    fpath = os.path.join(template_env.out_dir,LIB_DIR,fname)
+    template = template_env.lookup.get_template("js-service-implementation.mako")
+    outfile = open(fpath, "wb")
     outfile.write(codecs.BOM_UTF8)
-    outfile.write(lookup.get_template('js-service-implementation.mako').render_unicode(
+    outfile.write(template.render_unicode(
         service=service, 
         dashed_name=dashed_name,
-        create_test_framework=js_test_framework,
-        lookup=lookup,
+        create_test_framework=template_env.js_test_framework,
+        lookup=template_env.lookup,
         generate_field_consts=generate_field_consts,
         const_id=const_id,
-        console_logger_tutorial=console_logger_tutorial,
+        console_logger_tutorial=template_env.console_logger_tutorial,
         doc_rot_url=DOC_ROOT_URL
         ).encode('utf-8'))
     outfile.close()
     print "Wrote service-implementation %s to '%s'" % (service.name, fname)
+    
+def generate_message_map(service, template_env):
+    version = service.options["version"].value.replace('.', '_')
+    service_name = dashed_name(service.name, dash="_")
+    fname = "message_map_%s_%s.js" % (service_name, version)
+    fpath = os.path.join(template_env.out_dir, LIB_DIR, "message-maps", fname)
+    template = template_env.lookup.get_template("js-message-map.mako")
+    outfile = open(fpath, "wb")
+    outfile.write(codecs.BOM_UTF8)
+    outfile.write(template.render_unicode(
+        service=service, 
+        dashed_name=dashed_name,
+        create_test_framework=template_env.js_test_framework,
+        lookup=template_env.lookup,
+        generate_field_consts=generate_field_consts,
+        const_id=const_id,
+        console_logger_tutorial=template_env.console_logger_tutorial,
+        doc_rot_url=DOC_ROOT_URL,
+        language_context="js"
+        ).encode('utf-8'))
+    outfile.close()
+    print "Wrote message map %s to '%s'" % (service.name, fname)
 
 def generate_file(services, lookup, template_name, create_test_framework=False, 
             console_logger_tutorial=False, lib_dir="", language_context='js'):
     service_list = []
     tasks =  [
         generate_service_implementation,
+        generate_message_map
     ]
     if not os.path.exists(out_dir):
         os.makedirs(out_dir)
     if not os.path.exists(os.path.join(out_dir, LIB_DIR)):
         os.makedirs(os.path.join(out_dir, LIB_DIR))
+    if not os.path.exists(os.path.join(out_dir, LIB_DIR, "message-maps")):
+        os.makedirs(os.path.join(out_dir, LIB_DIR, "message-maps"))
+    template_env = TemplateEnv(lookup, out_dir, js_test_framework, console_logger_tutorial)
+        
     for path in iterProtoFiles(target.selectServiceFiles(args)):
         manager = PackageManager()
         package = manager.loadFile(path)
         if not package:
             print >>sys.stderr, "No protocol buffer definitions found in file %s" % path
+        
         for service in package.services: # TODO: Should generate for package instead?
+            if "version" not in service.options:
+                raise Exception("Option 'version' is not set on service %s" % service.name)
             service_list.append(service)
             for task in tasks:
-                task(service, lookup, out_dir, js_test_framework, console_logger_tutorial)
+                task(service, template_env)
     dependencies = {"Scope": False,
                     "WindowManager": False}
     for service in service_list:

templates/js/js-build_application.mako

         version = re_version.exec(service.version);
         version = version && version[1] || 0;        
         class_name = get_class_name(service_name); 
+        % if create_test_framework:
+        if(window.message_maps[service_name] && window.message_maps[service_name][version])
+        {
+          window.message_maps[service_name] = window.message_maps[service_name][version];
+        }
+        % endif
         builder = window.app.builders[class_name] && window.app.builders[class_name][version];
         if (builder) 
         {

templates/js/js-client-html.mako

 % for service in services:
   <script src="${lib_dir}/${dashed_name(service.name, dash='_')}_${service.options["version"].value.replace('.', '_')}.js"></script>
 % endfor
+% for service in services:
+  <script src="${lib_dir}/message-maps/message_map_${dashed_name(service.name, dash='_')}_${service.options["version"].value.replace('.', '_')}.js"></script>
+% endfor
 </head>
 <body>
 % if create_test_framework:

templates/js/js-message-map.mako

+<% 
+  INDENT = indent or "  " 
+  SINGLE_COMMENT_TOKEN = language_context and language_context == "js" and "//" or "#"
+
+%>\
+<%def name="print_fields(message, indent_level=0)">\
+[
+% for field in message.fields:
+${(indent_level + 1 ) * INDENT}{
+${(indent_level + 2 ) * INDENT}"name": "${field.name}",
+${(indent_level + 2 ) * INDENT}"q": "${field.q.name}",
+  % if field.message:
+    % if field.message.name == message.name:
+${(indent_level + 2 ) * INDENT}"message": "self" 
+    % else:
+${(indent_level + 2 ) * INDENT}"message": ${print_fields(field.message, indent_level=indent_level + 2)}
+    % endif
+  % endif
+${(indent_level + 1 ) * INDENT}},
+% endfor
+${(indent_level) * INDENT}],\
+</%def>\
+${SINGLE_COMMENT_TOKEN} created with hob 
+
+window.message_maps || (window.message_maps = {});
+window.message_maps["${dashed_name(service.name)}"] || (window.message_maps["${dashed_name(service.name)}"] = {});
+
+window.message_maps["${dashed_name(service.name)}"]["${service.options["version"].value}"] = {
+% for command in service.itercommands():
+  ${command.id}: {
+    "name": "${command.name}",
+    ${SINGLE_COMMENT_TOKEN} command message
+    1: ${print_fields(command.message, indent_level=2)}
+    ${SINGLE_COMMENT_TOKEN} response message
+    2: ${print_fields(command.response, indent_level=2)}
+  },
+% endfor
+% for event in service.iterevents():
+  ${event.id}: {
+    "name": "${event.name}",
+    ${SINGLE_COMMENT_TOKEN} event message
+    3: ${print_fields(event.message, indent_level=2)}
+  },
+% endfor
+}

templates/js/test_framework.js

     response.id = 'message-response';
     service = this._dashed_name(service);
     var command_id = this._event_map[service].indexOf('handle' + command);
-    var definitions = window.command_map[service][command_id][RESPONSE];
+    var definitions = 
+      window.message_maps[service] && 
+      window.message_maps[service][command_id] && 
+      window.message_maps[service][command_id][RESPONSE] || 
+      null;
     if (status != 0) // Use the error structure if we received an error response
         definitions = window.package_map["com.opera.stp"]["Error"];
     response.textContent = 
       "response:\n  status: " + 
       this._status_map[status] + "\n" +
       "  payload: \n" + 
-      (cookies.get('pretty-print-message') == 'true' ?
+      (cookies.get('pretty-print-message') == 'true' && definitions ?
         this._pretty_print_payload(message, definitions, 2) :
         JSON.stringify(message));
   }

templates/py/py-command-map.mako

   },
 }
 
-command_map = {
-% for service in services:
-    "${dashed_name(service.name)}": {
-    % for command in service.itercommands():
-        ${command.id}: {
-            "name": "${command.name}",
-            ${SINGLE_COMMENT_TOKEN} command message
-            1: ${print_fields(command.message, indent_level=3)}
-            ${SINGLE_COMMENT_TOKEN} response message
-            2: ${print_fields(command.response, indent_level=3)}
-        },
-    % endfor
-    % for event in service.iterevents():
-        ${event.id}: {
-            "name": "${event.name}",
-            ${SINGLE_COMMENT_TOKEN} event message
-            3: ${print_fields(event.message, indent_level=3)}
-        },
-    % endfor
-    },
-% endfor
-}
+message_maps = {}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.