def adjust_uri(self, uri, relativeto): """Called from within a Mako template, avoids adjusting the uri if it looks like a resource specification""" # Don't adjust pkg resource spec names if ':' in uri: return uri return TemplateLookup.adjust_uri(self, uri, relativeto)
def adjust_uri(self, uri, relativeto): """ Adjust the supplied URI given a specific source directory. If the uri begins with ``super:``, relativeize the URI such that it refers to the URI specified in the next-higher template directory in the chain. """ if uri.startswith('super:'): uri = uri[6:] relative_templ = self.get_template(relativeto) from_dir = self.find_dir_for_path(relative_templ.filename) assert from_dir, "can't find what dir calling template is in" start_index = self.directories.index(from_dir) + 1 relpath = os.path.relpath(relative_templ.filename, from_dir) next_dir, srcfile = self.find_dir_for_uri(relpath, start_index) assert next_dir else: next_dir = None if self.super_delimiter in relativeto: relativeto = relativeto.split(self.super_delimiter)[0] uri = TemplateLookup.adjust_uri(self, uri, relativeto) if next_dir: assert self.super_delimiter not in uri new_uri = uri + self.super_delimiter + next_dir return new_uri else: return uri
def adjust_uri(self, uri, relativeto): """Called from within a Mako template, avoids adjusting the uri if it looks like an asset specification""" # Don't adjust asset spec names if ':' in uri: return uri return TemplateLookup.adjust_uri(self, uri, relativeto)
def adjust_uri(self, uri, relativeto): """Called from within a Mako template, avoids adjusting the uri if it looks like an asset specification""" # Don't adjust asset spec names isabs = os.path.isabs(uri) if (not isabs) and (':' in uri): return uri if not(isabs) and ('$' in uri): return uri.replace('$', ':') if relativeto is not None: relativeto = relativeto.replace('$', ':') if not(':' in uri) and (':' in relativeto): if uri.startswith('/'): return uri pkg, relto = relativeto.split(':') _uri = posixpath.join(posixpath.dirname(relto), uri) return '{0}:{1}'.format(pkg, _uri) if not(':' in uri) and not(':' in relativeto): return posixpath.join(posixpath.dirname(relativeto), uri) return TemplateLookup.adjust_uri( self, uri, relativeto, input_encoding='utf-8' )
def adjust_uri(self, uri, relativeto): """Called from within a Mako template, avoids adjusting the uri if it looks like an asset specification""" # Don't adjust asset spec names isabs = os.path.isabs(uri) if (not isabs) and (':' in uri): return uri return TemplateLookup.adjust_uri(self, uri, relativeto)
def adjust_uri(self, uri, relativeto): """Called from within a Mako template, avoids adjusting the uri if it looks like an asset specification""" # Don't adjust asset spec names isabs = os.path.isabs(uri) if (not isabs) and (':' in uri): return uri return TemplateLookup.adjust_uri(self, uri, relativeto)
class SaltMakoTemplateLookup(TemplateCollection): """ Look up Mako template files on Salt master via salt://... URLs. If URL is a relative path(without an URL scheme) then assume it's relative to the directory of the salt file that's doing the lookup(with <%include/> or <%namespace/>). If URL is an absolute path then it's treated as if it has been prefixed with salt://. Examples:: <%include file="templates/sls-parts.mako"/> <%namespace file="salt://lib/templates/utils.mako" import="helper"/> """ def __init__(self, opts, env='base'): self.opts = opts self.env = env if opts['file_client'] == 'local': searchpath = opts['file_roots'][env] else: searchpath = [os.path.join(opts['cachedir'], 'files', env)] self.lookup = TemplateLookup(directories=searchpath) self.file_client = salt.fileclient.get_file_client(self.opts) self.cache = {} def adjust_uri(self, uri, filename): scheme = urlparse.urlparse(uri).scheme if scheme == 'salt': return uri elif scheme: raise ValueError("Unsupported URL scheme(%s) in %s" % \ (scheme, uri)) else: return self.lookup.adjust_uri(uri, filename) def get_template(self, uri): prefix = "salt://" salt_uri = uri if uri.startswith(prefix) else prefix+uri self.cache_file(salt_uri) return self.lookup.get_template(salt_uri[len(prefix):]) def cache_file(self, fpath): if fpath not in self.cache: self.cache[fpath] = \ self.file_client.get_file(fpath, '', True, self.env)
class SaltMakoTemplateLookup(TemplateCollection): """ Look up Mako template files on Salt master via salt://... URLs. If URL is a relative path(without an URL scheme) then assume it's relative to the directory of the salt file that's doing the lookup(with <%include/> or <%namespace/>). If URL is an absolute path then it's treated as if it has been prefixed with salt://. Examples:: <%include file="templates/sls-parts.mako"/> <%namespace file="salt://lib/templates/utils.mako" import="helper"/> """ def __init__(self, opts, env='base'): self.opts = opts self.env = env if opts['file_client'] == 'local': searchpath = opts['file_roots'][env] else: searchpath = [os.path.join(opts['cachedir'], 'files', env)] self.lookup = TemplateLookup(directories=searchpath) self.file_client = salt.fileclient.get_file_client(self.opts) self.cache = {} def adjust_uri(self, uri, filename): scheme = urlparse.urlparse(uri).scheme if scheme == 'salt': return uri elif scheme: raise ValueError("Unsupported URL scheme(%s) in %s" % \ (scheme, uri)) else: return self.lookup.adjust_uri(uri, filename) def get_template(self, uri): prefix = "salt://" salt_uri = uri if uri.startswith(prefix) else prefix+uri self.cache_file(salt_uri) return self.lookup.get_template(salt_uri[len(prefix):]) def cache_file(self, fpath): if fpath not in self.cache: self.cache[fpath] = \ self.file_client.get_file(fpath, '', True, self.env)
def adjust_uri(self, uri, relativeto): """Called from within a Mako template, avoids adjusting the uri if it looks like an asset specification""" # Don't adjust asset spec names isabs = os.path.isabs(uri) if (not isabs) and (':' in uri): return uri if not (isabs) and ('$' in uri): return uri.replace('$', ':') if relativeto is not None: relativeto = relativeto.replace('$', ':') if not (':' in uri) and (':' in relativeto): pkg, relto = relativeto.split(':') _uri = posixpath.join(posixpath.dirname(relto), uri) return '{0}:{1}'.format(pkg, _uri) if not (':' in uri) and not (':' in relativeto): return posixpath.join(posixpath.dirname(relativeto), uri) return TemplateLookup.adjust_uri(self, uri, relativeto)
def adjust_uri(self, uri, relativeto): """Called from within a Mako template, avoids adjusting the uri if it looks like an asset specification""" # Don't adjust asset spec names isabs = os.path.isabs(uri) if (not isabs) and (":" in uri): return uri if not (isabs) and ("$" in uri): return uri.replace("$", ":") if relativeto is not None: relativeto = relativeto.replace("$", ":") if not (":" in uri) and (":" in relativeto): if uri.startswith("/"): return uri pkg, relto = relativeto.split(":") _uri = posixpath.join(posixpath.dirname(relto), uri) return "{0}:{1}".format(pkg, _uri) if not (":" in uri) and not (":" in relativeto): return posixpath.join(posixpath.dirname(relativeto), uri) return TemplateLookup.adjust_uri(self, uri, relativeto)
class SaltMakoTemplateLookup(TemplateCollection): """ Look up Mako template files using file:// or salt:// URLs with <%include/> or <%namespace/>. (1) Look up mako template files on local file system via files://... URL. Make sure mako template file is present locally on minion beforehand. Examples: <%include file="file:///etc/salt/lib/templates/sls-parts.mako"/> <%namespace file="file:///etc/salt/lib/templates/utils.mako" import="helper"/> (2) Look up mako template files on Salt master via salt://... URL. If URL is a relative path (without an URL scheme) then assume it's relative to the directory of the salt file that's doing the lookup. If URL is an absolute path then it's treated as if it has been prefixed with salt://. Examples:: <%include file="templates/sls-parts.mako"/> <%include file="salt://lib/templates/sls-parts.mako"/> <%include file="/lib/templates/sls-parts.mako"/> ##-- treated as salt:// <%namespace file="templates/utils.mako"/> <%namespace file="salt://lib/templates/utils.mako" import="helper"/> <%namespace file="/lib/templates/utils.mako" import="helper"/> ##-- treated as salt:// """ def __init__(self, opts, saltenv='base', pillar_rend=False): self.opts = opts self.saltenv = saltenv self._file_client = None self.pillar_rend = pillar_rend self.lookup = TemplateLookup(directories='/') self.cache = {} def file_client(self): ''' Setup and return file_client ''' if not self._file_client: self._file_client = salt.fileclient.get_file_client( self.opts, self.pillar_rend) return self._file_client def adjust_uri(self, uri, filename): scheme = urlparse(uri).scheme if scheme in ('salt', 'file'): return uri elif scheme: raise ValueError( 'Unsupported URL scheme({0}) in {1}'.format( scheme, uri ) ) return self.lookup.adjust_uri(uri, filename) def get_template(self, uri, relativeto=None): if uri.startswith("file://"): proto = "file://" searchpath = "/" salt_uri = uri else: proto = "salt://" if self.opts['file_client'] == 'local': searchpath = self.opts['file_roots'][self.saltenv] else: searchpath = [os.path.join(self.opts['cachedir'], 'files', self.saltenv)] salt_uri = uri if uri.startswith(proto) else salt.utils.url.create(uri) self.cache_file(salt_uri) self.lookup = TemplateLookup(directories=searchpath) return self.lookup.get_template(salt_uri[len(proto):]) def cache_file(self, fpath): if fpath not in self.cache: self.cache[fpath] = self.file_client().get_file(fpath, '', True, self.saltenv)
class SaltMakoTemplateLookup(TemplateCollection): """ Look up Mako template files using file:// or salt:// URLs with <%include/> or <%namespace/>. (1) Look up mako template files on local file system via files://... URL. Make sure mako template file is present locally on minion beforehand. Examples: <%include file="file:///etc/salt/lib/templates/sls-parts.mako"/> <%namespace file="file:///etc/salt/lib/templates/utils.mako" import="helper"/> (2) Look up mako template files on Salt master via salt://... URL. If URL is a relative path (without an URL scheme) then assume it's relative to the directory of the salt file that's doing the lookup. If URL is an absolute path then it's treated as if it has been prefixed with salt://. Examples:: <%include file="templates/sls-parts.mako"/> <%include file="salt://lib/templates/sls-parts.mako"/> <%include file="/lib/templates/sls-parts.mako"/> ##-- treated as salt:// <%namespace file="templates/utils.mako"/> <%namespace file="salt://lib/templates/utils.mako" import="helper"/> <%namespace file="/lib/templates/utils.mako" import="helper"/> ##-- treated as salt:// """ def __init__(self, opts, saltenv='base', env=None): if env is not None: salt.utils.warn_until( 'Boron', 'Passing a salt environment should be done using \'saltenv\' ' 'not \'env\'. This functionality will be removed in Salt ' 'Boron.' ) # Backwards compatibility saltenv = env self.opts = opts self.saltenv = saltenv self.file_client = salt.fileclient.get_file_client(self.opts) self.lookup = TemplateLookup(directories='/') self.cache = {} def adjust_uri(self, uri, filename): scheme = urlparse.urlparse(uri).scheme if scheme in ('salt', 'file'): return uri elif scheme: raise ValueError( 'Unsupported URL scheme({0}) in {1}'.format( scheme, uri ) ) return self.lookup.adjust_uri(uri, filename) def get_template(self, uri): if uri.startswith("file://"): prefix = "file://" searchpath = "/" salt_uri = uri else: prefix = "salt://" if self.opts['file_client'] == 'local': searchpath = self.opts['file_roots'][self.saltenv] else: searchpath = [os.path.join(self.opts['cachedir'], 'files', self.saltenv)] salt_uri = uri if uri.startswith(prefix) else (prefix + uri) self.cache_file(salt_uri) self.lookup = TemplateLookup(directories=searchpath) return self.lookup.get_template(salt_uri[len(prefix):]) def cache_file(self, fpath): if fpath not in self.cache: self.cache[fpath] = self.file_client.get_file(fpath, '', True, self.saltenv)
def adjust_uri(self, uri, relativeto): if uri.startswith('db:'): return uri return TemplateLookup.adjust_uri(self, uri, relativeto)
class SaltMakoTemplateLookup(TemplateCollection): """ Look up Mako template files using file:// or salt:// URLs with <%include/> or <%namespace/>. (1) Look up mako template files on local file system via files://... URL. Make sure mako template file is present locally on minion beforehand. Examples: <%include file="file:///etc/salt/lib/templates/sls-parts.mako"/> <%namespace file="file:///etc/salt/lib/templates/utils.mako" import="helper"/> (2) Look up mako template files on Salt master via salt://... URL. If URL is a relative path (without an URL scheme) then assume it's relative to the directory of the salt file that's doing the lookup. If URL is an absolute path then it's treated as if it has been prefixed with salt://. Examples:: <%include file="templates/sls-parts.mako"/> <%include file="salt://lib/templates/sls-parts.mako"/> <%include file="/lib/templates/sls-parts.mako"/> ##-- treated as salt:// <%namespace file="templates/utils.mako"/> <%namespace file="salt://lib/templates/utils.mako" import="helper"/> <%namespace file="/lib/templates/utils.mako" import="helper"/> ##-- treated as salt:// """ def __init__(self, opts, saltenv="base", pillar_rend=False): self.opts = opts self.saltenv = saltenv self._file_client = None self.pillar_rend = pillar_rend self.lookup = TemplateLookup(directories="/") self.cache = {} def file_client(self): """ Setup and return file_client """ if not self._file_client: self._file_client = salt.fileclient.get_file_client( self.opts, self.pillar_rend) return self._file_client def adjust_uri(self, uri, filename): scheme = urllib.parse.urlparse(uri).scheme if scheme in ("salt", "file"): return uri elif scheme: raise ValueError("Unsupported URL scheme({}) in {}".format( scheme, uri)) return self.lookup.adjust_uri(uri, filename) def get_template(self, uri, relativeto=None): if uri.startswith("file://"): proto = "file://" searchpath = "/" salt_uri = uri else: proto = "salt://" if self.opts["file_client"] == "local": searchpath = self.opts["file_roots"][self.saltenv] else: searchpath = [ os.path.join(self.opts["cachedir"], "files", self.saltenv) ] salt_uri = uri if uri.startswith( proto) else salt.utils.url.create(uri) self.cache_file(salt_uri) self.lookup = TemplateLookup(directories=searchpath) return self.lookup.get_template(salt_uri[len(proto):]) def cache_file(self, fpath): if fpath not in self.cache: self.cache[fpath] = self.file_client().get_file( fpath, "", True, self.saltenv)
def adjust_uri(self, uri, relativeto): if uri.startswith('db:'): return uri return TemplateLookup.adjust_uri(self, uri, relativeto)