Allow the file_client to be overridden

This commit is contained in:
twangboy 2019-06-21 17:48:27 -06:00
parent f6c592c7a1
commit 369720e660
No known key found for this signature in database
GPG key ID: 93FF3BDEB278C9EB

View file

@ -5,6 +5,7 @@ Jinja loading utils to enable a more powerful backend for jinja templates
# Import python libs
from __future__ import absolute_import, unicode_literals
import atexit
import collections
import logging
import os.path
@ -55,10 +56,17 @@ class SaltCacheLoader(BaseLoader):
and only loaded once per loader instance.
'''
_file_client = None
_cached_client = None
@classmethod
def shutdown(cls):
if cls._cached_client is None:
return
cls._cached_client.destroy()
cls._cached_client = None
def __init__(self, opts, saltenv='base', encoding='utf-8',
pillar_rend=False):
pillar_rend=False, _file_client=None):
self.opts = opts
self.saltenv = saltenv
self.encoding = encoding
@ -72,6 +80,7 @@ class SaltCacheLoader(BaseLoader):
self.searchpath = [os.path.join(opts['cachedir'], 'files', saltenv)]
log.debug('Jinja search path: %s', self.searchpath)
self.cached = []
self._file_client = _file_client
# Instantiate the fileclient
self.file_client()
@ -79,10 +88,15 @@ class SaltCacheLoader(BaseLoader):
'''
Return a file client. Instantiates on first call.
'''
if not SaltCacheLoader._file_client:
SaltCacheLoader._file_client = salt.fileclient.get_file_client(
self.opts, self.pillar_rend)
return SaltCacheLoader._file_client
# If there was no file_client passed to the class, create a cache_client
# and use that. This avoids opening a new file_client every time this
# class is instantiated
if self._file_client is None:
if not SaltCacheLoader._cached_client:
SaltCacheLoader._cached_client = salt.fileclient.get_file_client(
self.opts, self.pillar_rend)
self._file_client = SaltCacheLoader._cached_client
return self._file_client
def cache_file(self, template):
'''
@ -173,6 +187,9 @@ class SaltCacheLoader(BaseLoader):
raise TemplateNotFound(template)
atexit.register(SaltCacheLoader.shutdown)
class PrintableDict(OrderedDict):
'''
Ensures that dict str() and repr() are YAML friendly.