Jan Borsodi avatar Jan Borsodi committed 6afbda6

Added sys.prefix/templates as a lookup path for templates and fixed template paths in js command.

Comments (0)

Files changed (2)

 from hob.proto import ServiceManager, iterProtoFiles  # fixme: presuambly redundant. runeh
 from hob.utils import dashed_name, DIST_ROOT, tempdir, strquote
 from hob.cmd import ProgramError
+from hob.template import Generator
 from mako.template import Template
 from mako.lookup import TemplateLookup
 
     fname = "%s_%s.js" % (service_name, version)
     short_fpath = os.path.join(service_name == "scope" and LIB_DIR or DOC_DIR, fname)
     fpath = os.path.join(template_env.out_dir, short_fpath)
-    template = template_env.lookup.get_template("js-service-implementation.mako")
+    template = template_env.lookup.get_template("js/js-service-implementation.mako")
     text = template.render_unicode(
         service=service,
         dashed_name=dashed_name,
         comments=["/** \n", " * ", " */"],
         ):
     from hob import proto
-    return lookup.get_template('js-message-definition.mako').render_unicode(
+    return lookup.get_template('js/js-message-definition.mako').render_unicode(
         message=message,
         protoHTMLClass=protoHTMLClass,
         classes=classes,
         )
 
 def generate_message_classes(service, message, lookup, create_test_framework=False):
-    return lookup.get_template('js-message-classes.mako').render_unicode(
+    return lookup.get_template('js/js-message-classes.mako').render_unicode(
         service=service,
         message=message,
         proto=proto,
         indent='  ',
         ):
     from hob import proto
-    return lookup.get_template('js-field-consts.mako').render_unicode(
+    return lookup.get_template('js/js-field-consts.mako').render_unicode(
         message=message,
         create_test_framework=create_test_framework,
         dashed_name=dashed_name,
         )
 
 def generate_services(target, args, out_dir="js-out", js_test_framework=False, console_logger_tutorial=False):
-    lookup = TemplateLookup(
-        directories=[
-            os.path.join(DIST_ROOT, 'templates'),
-            os.path.join(DIST_ROOT, 'templates', 'js'),
-            os.path.join(DIST_ROOT, 'templates', 'py'),
-            ],
-        module_directory=tempdir(["mako", "js"]),
-        default_filters=['decode.utf8'],
-        )
+    gen = Generator(subdir="js", lookup_args=dict(default_filters=['decode.utf8']))
+    lookup = gen.lookup
+    # lookup = TemplateLookup(
+        # directories=[
+            # os.path.join(DIST_ROOT, 'templates'),
+            # os.path.join(DIST_ROOT, 'templates', 'js'),
+            # os.path.join(DIST_ROOT, 'templates', 'py'),
+            # ],
+        # module_directory=tempdir(["mako", "js"]),
+        # default_filters=['decode.utf8'],
+        # )
     service_list = []
     tasks = [generate_service_implementation]
     if not os.path.exists(out_dir):
         required = [k for k, v in dependencies.iteritems() if not v]
         raise ProgramError("The following services must be included for the generated code to work: %s" % ", ".join(required))
     sources = [
-        ('client.js', 'js-client.mako', ''),
-        ('client.html', 'js-client-html.mako', ''),
-        ('service_base.js', 'js-service-base.mako', LIB_DIR),
-        ('http_interface.js', 'js-http-interface.mako', LIB_DIR),
-        ('stp_0_wrapper.js', 'js-stp-0-wrapper.mako', LIB_DIR),
-        ('build_application.js', 'js-build_application.mako', ''),
+        ('client.js', 'js/js-client.mako', ''),
+        ('client.html', 'js/js-client-html.mako', ''),
+        ('service_base.js', 'js/js-service-base.mako', LIB_DIR),
+        ('http_interface.js', 'js/js-http-interface.mako', LIB_DIR),
+        ('stp_0_wrapper.js', 'js/js-stp-0-wrapper.mako', LIB_DIR),
+        ('build_application.js', 'js/js-build_application.mako', ''),
         ]
     if js_test_framework:
         sources += [
             ('simpleconsolelogger.js', ''),
             ]
     for file_name, repo in sources:
-        short_source_path = os.path.join('templates', 'js', file_name)
+        short_source_path = gen.findFile(os.path.join('js', file_name))
         source_path = os.path.join(DIST_ROOT, short_source_path)
         out_path = os.path.join(out_dir, repo)
         out_file = os.path.join(out_path, os.path.basename(source_path))
 import os
+import sys
 from hob.utils import tempdir
 from hob.proto import Package, Service, Message, Field, Enum
 from hob import proto
 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 class Generator(object):
-    def __init__(self, level=4, lookup_args={}):
+    def __init__(self, level=4, subdir="proto", lookup_args={}):
         self.level = level
-        self.lookup = TemplateLookup(directories=[os.path.join(root, 'templates')],
-                                     module_directory=tempdir(["mako", "proto"]),
+        self.directories = [os.path.join(root, 'templates'),
+                            os.path.join(sys.prefix, 'templates')]
+        self.lookup = TemplateLookup(directories=self.directories,
+                                     module_directory=tempdir(["mako", subdir]),
                                      **lookup_args)
 
+    def findFile(self, relpath):
+        for path in self.directories:
+            path = os.path.join(path, relpath)
+            if os.path.exists(path):
+                return path
+        raise IOError("Could not find file %s" % relpath)
+
     def generate(self, template, indent, template_data):
         t = self.lookup.get_template(template)
         template_data["proto"] = proto
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.