Commits

Brent Tubbs committed 20bff4d

added template abstractions

Comments (0)

Files changed (2)

 def _upload_config_template(template, dest, context):
     #first try to load the template from the local cfg_templates dir.
     #if it's not there then try loading from pkg_resources
-    localpath=os.path.join(env.local_root, 'cfg_templates', template)
-    pkgpath=pkg_resources.resource_filename('silk', 'cfg_templates/%s' % template)
-    if os.path.isfile(localpath):
-        upload_template(localpath, dest, context=context, use_sudo=True)
-        sudo('chmod +r %s' % dest)
-    elif os.path.isfile(pkgpath):
-        upload_template(pkgpath, dest, context=context, use_sudo=True)
-        sudo('chmod +r %s' % dest)
-    else:
-        sys.exit("Template not found: %s" % template)
+    path = silk.lib.get_template_path('template')
+    upload_template(path, dest, context=context, use_sudo=True)
 
 def _get_nginx_static_snippet(url_path, local_path):
     return """
 import copy
 import os
 import sys
+import pkg_resources
 
 def get_gunicorn_cmd(site_config, bin_dir=''):
     gconfig = copy.copy(site_config['gunicorn'])
         config.update(get_role_config(role))
         return config
 
+def get_template_path(template, site_root=None):
+    """
+    Returns path of template from site cfg_templates dir, if found there, else
+    returns template path from silk's cfg_templates dir.
+    """
+    if site_root:
+        localpath=os.path.join(site_root, 'cfg_templates', template)
+        if os.path.isfile(localpath):
+            return localpath
+    pkgpath=pkg_resources.resource_filename('silk', 'cfg_templates/%s' % template)
+    if os.path.isfile(pkgpath):
+        return pkgpath
+    else:
+        raise Exception("Template not found: %s" % template)
+
+def get_rendered_template(template_name, context):
+    """
+    Returns text of named template, with keyword substitutions pulled from
+    'context'
+    """
+    template_path = get_template_path(template_name)
+    txt = open(template_path, 'r').read()
+    return txt % context
+
 def get_role():
     try:
         #if '-R rolename' found in sys.argv, use that