def test_string_io_unicode(self): ret = compat.string_io("test string \xf8") if PY3: expected = "io.StringIO object" else: expected = "StringIO.StringIO instance" self.assertTrue(expected in repr(ret))
def test_string_io(self): ret = compat.string_io("test string") if PY3: expected = "io.StringIO object" else: expected = "cStringIO.StringI object" self.assertTrue(expected in repr(ret))
def test_string_io_unicode(self): ret = compat.string_io(u'test string \xf8') if PY3: expected = 'io.StringIO object' else: expected = 'StringIO.StringIO instance' self.assertTrue(expected in repr(ret))
def test_string_io(self): ret = compat.string_io('test string') if PY3: expected = 'io.StringIO object' else: expected = 'cStringIO.StringI object' self.assertTrue(expected in repr(ret))
def compile_template(template, renderers, default, blacklist, whitelist, saltenv='base', sls='', input_data='', **kwargs): ''' Take the path to a template and return the high data structure derived from the template. ''' # if any error occurs, we return an empty dictionary ret = {} log.debug('compile template: {0}'.format(template)) if 'env' in kwargs: salt.utils.warn_until( 'Oxygen', 'Parameter \'env\' has been detected in the argument list. This ' 'parameter is no longer used and has been replaced by \'saltenv\' ' 'as of Salt Carbon. This warning will be removed in Salt Oxygen.') kwargs.pop('env') if template != ':string:': # Template was specified incorrectly if not isinstance(template, string_types): log.error( 'Template was specified incorrectly: {0}'.format(template)) return ret # Template does not exist if not os.path.isfile(template): log.error('Template does not exist: {0}'.format(template)) return ret # Template is an empty file if salt.utils.is_empty(template): log.warning('Template is an empty file: {0}'.format(template)) return ret with codecs.open(template, encoding=SLS_ENCODING) as ifile: # data input to the first render function in the pipe input_data = ifile.read() if not input_data.strip(): # Template is nothing but whitespace log.error( 'Template is nothing but whitespace: {0}'.format(template)) return ret # Get the list of render funcs in the render pipe line. render_pipe = template_shebang(template, renderers, default, blacklist, whitelist, input_data) input_data = string_io(input_data) for render, argline in render_pipe: # For GPG renderer, input_data can be an OrderedDict (from YAML) or dict (from py renderer). # Repress the error. if not isinstance(input_data, (dict, OrderedDict)): try: input_data.seek(0) except Exception as exp: log.error('error: {0}'.format(exp)) render_kwargs = dict(renderers=renderers, tmplpath=template) render_kwargs.update(kwargs) if argline: render_kwargs['argline'] = argline start = time.time() ret = render(input_data, saltenv, sls, **render_kwargs) log.profile( 'Time (in seconds) to render \'{0}\' using \'{1}\' renderer: {2}'. format(template, render.__module__.split('.')[-1], time.time() - start)) if ret is None: # The file is empty or is being written elsewhere time.sleep(0.01) ret = render(input_data, saltenv, sls, **render_kwargs) input_data = ret if log.isEnabledFor(logging.GARBAGE): # pylint: disable=no-member try: log.debug('Rendered data from file: {0}:\n{1}'.format( template, ret.read())) ret.seek(0) except Exception: # ret is not a StringIO, which means it was rendered using # yaml, mako, or another engine which renders to a data # structure. We don't want to log this, so ignore this # exception. pass return ret
def compile_template(template, renderers, default, saltenv='base', sls='', input_data='', **kwargs): ''' Take the path to a template and return the high data structure derived from the template. ''' # if any error occurs, we return an empty dictionary ret = {} log.debug('compile template: {0}'.format(template)) # We "map" env to the same as saltenv until Boron is out in order to follow the same deprecation path kwargs.setdefault('env', saltenv) salt.utils.warn_until( 'Boron', 'We are only supporting \'env\' in the templating context until Boron comes out. ' 'Once this warning is shown, please remove the above mapping', _dont_call_warnings=True ) if template != ':string:': # Template was specified incorrectly if not isinstance(template, string_types): log.error('Template was specified incorrectly: {0}'.format(template)) return ret # Template does not exist if not os.path.isfile(template): log.error('Template does not exist: {0}'.format(template)) return ret # Template is an empty file if salt.utils.is_empty(template): log.warn('Template is an empty file: {0}'.format(template)) return ret with codecs.open(template, encoding=SLS_ENCODING) as ifile: # data input to the first render function in the pipe input_data = ifile.read() if not input_data.strip(): # Template is nothing but whitespace log.error('Template is nothing but whitespace: {0}'.format(template)) return ret # Get the list of render funcs in the render pipe line. render_pipe = template_shebang(template, renderers, default, input_data) input_data = string_io(input_data) for render, argline in render_pipe: # For GPG renderer, input_data can be an OrderedDict (from YAML) or dict (from py renderer). # Repress the error. if not isinstance(input_data, (dict, OrderedDict)): try: input_data.seek(0) except Exception as exp: log.error('error: {0}'.format(exp)) render_kwargs = dict(renderers=renderers, tmplpath=template) render_kwargs.update(kwargs) if argline: render_kwargs['argline'] = argline start = time.time() ret = render(input_data, saltenv, sls, **render_kwargs) log.profile( 'Time (in seconds) to render \'{0}\' using \'{1}\' renderer: {2}'.format( template, render.__module__.split('.')[-1], time.time() - start ) ) if ret is None: # The file is empty or is being written elsewhere time.sleep(0.01) ret = render(input_data, saltenv, sls, **render_kwargs) input_data = ret if log.isEnabledFor(logging.GARBAGE): # pylint: disable=no-member try: log.debug('Rendered data from file: {0}:\n{1}'.format( template, ret.read())) ret.seek(0) except Exception: # ret is not a StringIO, which means it was rendered using # yaml, mako, or another engine which renders to a data # structure. We don't want to log this, so ignore this # exception. pass return ret
def compile_template(template, renderers, default, saltenv='base', sls='', **kwargs): ''' Take the path to a template and return the high data structure derived from the template. ''' # We "map" env to the same as saltenv until Boron is out in order to follow the same deprecation path kwargs.setdefault('env', saltenv) salt.utils.warn_until( 'Boron', 'We are only supporting \'env\' in the templating context until Boron comes out. ' 'Once this warning is shown, please remove the above mapping', _dont_call_warnings=True) # Template was specified incorrectly if not isinstance(template, string_types): return {} # Template does not exists if not os.path.isfile(template): return {} # Template is an empty file if salt.utils.is_empty(template): return {} # Get the list of render funcs in the render pipe line. render_pipe = template_shebang(template, renderers, default) with codecs.open(template, encoding=SLS_ENCODING) as ifile: # data input to the first render function in the pipe input_data = ifile.read() if not input_data.strip(): # Template is nothing but whitespace return {} input_data = string_io(input_data) for render, argline in render_pipe: try: input_data.seek(0) except Exception: pass render_kwargs = dict(renderers=renderers, tmplpath=template) render_kwargs.update(kwargs) if argline: render_kwargs['argline'] = argline ret = render(input_data, saltenv, sls, **render_kwargs) if ret is None: # The file is empty or is being written elsewhere time.sleep(0.01) ret = render(input_data, saltenv, sls, **render_kwargs) input_data = ret if log.isEnabledFor(logging.GARBAGE): # pylint: disable=no-member try: log.debug('Rendered data from file: {0}:\n{1}'.format( template, ret.read())) ret.seek(0) except Exception: pass return ret
def compile_template(template, renderers, default, saltenv='base', sls='', input_data='', **kwargs): ''' Take the path to a template and return the high data structure derived from the template. ''' # if any error occurs, we return an empty dictionary ret = {} log.debug('compile template: {0}'.format(template)) # We "map" env to the same as saltenv until Boron is out in order to follow the same deprecation path kwargs.setdefault('env', saltenv) salt.utils.warn_until( 'Boron', 'We are only supporting \'env\' in the templating context until Boron comes out. ' 'Once this warning is shown, please remove the above mapping', _dont_call_warnings=True) if template != ':string:': # Template was specified incorrectly if not isinstance(template, string_types): log.error( 'Template was specified incorrectly: {0}'.format(template)) return ret # Template does not exist if not os.path.isfile(template): log.error('Template does not exist: {0}'.format(template)) return ret # Template is an empty file if salt.utils.is_empty(template): log.warn('Template is an empty file: {0}'.format(template)) return ret with codecs.open(template, encoding=SLS_ENCODING) as ifile: # data input to the first render function in the pipe input_data = ifile.read() if not input_data.strip(): # Template is nothing but whitespace log.error( 'Template is nothing but whitespace: {0}'.format(template)) return ret # Get the list of render funcs in the render pipe line. render_pipe = template_shebang(template, renderers, default, input_data) input_data = string_io(input_data) for render, argline in render_pipe: # For GPG renderer, input_data can be an OrderedDict (from YAML) or dict (from py renderer). # Repress the error. if not isinstance(input_data, (dict, OrderedDict)): try: input_data.seek(0) except Exception as exp: log.error('error: {0}'.format(exp)) render_kwargs = dict(renderers=renderers, tmplpath=template) render_kwargs.update(kwargs) if argline: render_kwargs['argline'] = argline start = time.time() ret = render(input_data, saltenv, sls, **render_kwargs) log.profile( 'Time (in seconds) to render \'{0}\' using \'{1}\' renderer: {2}'. format(template, render.__module__.split('.')[-1], time.time() - start)) if ret is None: # The file is empty or is being written elsewhere time.sleep(0.01) ret = render(input_data, saltenv, sls, **render_kwargs) input_data = ret if log.isEnabledFor(logging.GARBAGE): # pylint: disable=no-member try: log.debug('Rendered data from file: {0}:\n{1}'.format( template, ret.read())) ret.seek(0) except Exception: # ret is not a StringIO, which means it was rendered using # yaml, mako, or another engine which renders to a data # structure. We don't want to log this, so ignore this # exception. pass return ret