def call_test(self, name, value, args=None, kwargs=None): """Invokes a test on a value the same way the compiler does it. .. versionadded:: 2.7 """ func = self.tests.get(name) if func is None: raise TemplateRuntimeError('no test named %r' % name) return func(value, *(args or ()), **(kwargs or {}))
def fail_for_missing_callable(string, name): msg = string % name if isinstance(name, Undefined): try: name._fail_with_undefined_error() except Exception as e: msg = '%s (%s; did you forget to quote the callable name?)' % (msg, e) raise TemplateRuntimeError(msg)
def normalize_iter(value): if isinstance(value, (list, tuple)): if isinstance(value[0], str): xmlval = value else: xmlval = [] elif isinstance(value, dict): xmlval = list(value.items()) else: raise TemplateRuntimeError( 'Value is not a dict or list. Cannot render as XML') return xmlval
def filter_path_reftypes(spec): rv = set() for p in spec['paths']: for method in spec['paths'][p]: if 'parameters' in spec['paths'][p][method]: for param in spec['paths'][p][method]['parameters']: if param['in'] == 'body': if 'schema' in param: schema = param['schema'] if 'type' in schema and schema['type'] == 'array': if '$ref' in schema['items']: rv.add(filter_reftype(schema['items']['$ref'])) else: raise TemplateRuntimeError("Arrays as parameter types are only supported "+ "for referenced types (%s)" % param) elif '$ref' in schema: rv.add(filter_reftype(schema['$ref'])) else: raise TemplateRuntimeError("Only referenced schemas are supported " "as parameter types (%s %s %s)" % (param, method, param['name'])) for code in spec['paths'][p][method]['responses']: if 'content' in spec['paths'][p][method]['responses'][code]: content = spec['paths'][p][method]['responses'][code]['content'] if 'application/json' in content: schema = content['application/json']['schema'] if 'type' in schema and schema['type'] == 'array': if '$ref' in schema['items']: rv.add(filter_reftype(schema['items']['$ref'])) else: raise TemplateRuntimeError("Arrays as response types are only supported "+ "for referenced types (%s)" % p) elif '$ref' in schema: rv.add(filter_reftype(schema['$ref'])) else: raise TemplateRuntimeError("Only referenced schemas are supported " "as response types (%s %s %s)" % (p, method, code)) return sorted(rv)
def filter_path_args(op, only_in=''): rv = [] if 'parameters' in op: for p in op['parameters']: if 'schema' not in p: raise TemplateRuntimeError("Parameter requires schema " + "(%s of %s)" % (p.name, op.operationId)) required = ('required' in p and p['required']) if p['in'] in ['path', 'query'] and (only_in == '' or p['in'] == only_in): if 'type' in p['schema'] \ and p['schema']['type'] in ['integer', 'number', 'string', 'boolean']: rv.append({ "name": p['name'], "type": p['schema']['type'], "required": required }) else: raise TemplateRuntimeError( "Only primitive types are supported " + "for path args (%s of %s)" % (p.name, op.operationId)) elif p['in'] == 'body' and (only_in == '' or p['in'] == only_in): if '$ref' in p['schema']: rv.append({ "name": p['name'], "type": filter_reftype(p['schema']['$ref']), "required": required }) else: # We can do better here, generating appropriate objects rv.append({ "name": p['name'], "type": "any", "required": required }) return rv
def load_yaml(self, value): if isinstance(value, TemplateModule): value = six.text_type(value) try: return salt.utils.data.decode(salt.utils.yaml.safe_load(value)) except salt.utils.yaml.YAMLError as exc: msg = 'Encountered error loading yaml: ' try: # Reported line is off by one, add 1 to correct it line = exc.problem_mark.line + 1 buf = exc.problem_mark.buffer problem = exc.problem except AttributeError: # No context information available in the exception, fall back # to the stringified version of the exception. msg += six.text_type(exc) else: msg += '{0}\n'.format(problem) msg += salt.utils.stringutils.get_context( buf, line, marker=' <======================') raise TemplateRuntimeError(msg) except AttributeError: raise TemplateRuntimeError( 'Unable to load yaml from {0}'.format(value))
def _context_git_source(context, uri, rev='master'): """clone the given ref into the output_dir and return the rev""" if not utils._has_git(): raise TemplateRuntimeError("Context function 'git_source' requires" " gitpython but it could not be imported") _context_check_variable(context, CONTEXT_VAR_PYPI_NAME, 'pypi_name') prj_name = context.vars[CONTEXT_VAR_PYPI_NAME] out_dir = context['output_dir'] if out_dir: src_dir = os.path.join(out_dir, prj_name) sha = utils._git_repo(src_dir, uri, rev) tarname = '-'.join([prj_name, sha]) utils._create_archive(tarname, 'gztar', out_dir, prj_name) return sha return rev
def disable_op(arg): raise TemplateRuntimeError('that operator so does not work')
def disable_op(left, right): raise TemplateRuntimeError('that operator so does not work')
def _raise(self, msg, caller): raise TemplateRuntimeError(msg)
def _raise(self, msg, caller): """Helper callback.""" raise TemplateRuntimeError(msg)
def _raise_error(self, message, caller): raise TemplateRuntimeError(message)
def _raise_error(msg, caller): raise TemplateRuntimeError(msg)
def _context_check_variable(context, var_name, needed_by): """check that the context has a given variable""" if var_name not in context.vars: raise TemplateRuntimeError("Variable '%s' not available in context but" " needed for '%s'" % (var_name, needed_by))