Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def limit_iterable(iterable, limit_or_engine):
if isinstance(limit_or_engine, int):
max_count = limit_or_engine
else:
max_count = get_max_collection_size(limit_or_engine)
if isinstance(iterable, (SequenceType, MappingType, SetType)):
if 0 <= max_count < len(iterable):
raise exceptions.CollectionTooLargeException(max_count)
return iterable
def limiting_iterator():
for i, t in enumerate(iterable):
if 0 <= max_count <= i:
raise exceptions.CollectionTooLargeException(max_count)
yield t
return limiting_iterator()
def __call__(self, receiver, context, engine):
if not context.collect_functions('#finalize'):
context = context.create_child_context()
context.register_function(lambda x: x, name='#finalize')
try:
return super(Statement, self).__call__(receiver, context, engine)
except exceptions.WrappedException as e:
six.reraise(type(e.wrapped), e.wrapped, sys.exc_info()[2])
def raise_ambiguous():
if receiver is utils.NO_VALUE:
raise exceptions.AmbiguousFunctionException(name)
else:
raise exceptions.AmbiguousMethodException(name, receiver)
function_name=self.function.func_name)
if self.context_owner_param_name and param.own_context:
raise exceptions.DuplicateContextOwnerDecoratorException(
function_name=self.function.func_name)
self.param_definitions[param.name] = param
if param.is_context:
self.is_context_aware = True
self.context_param_name = param.name
if param.is_self is None:
if param.name in self._arg_spec.args:
param.is_self = self._arg_spec.args.index(
param.name) == 0 and param.name == 'self'
if param.is_self:
self.self_param_name = param.name
if param.lazy:
raise exceptions.YaqlException("Self parameter cannot be lazy")
return args, {}
pos_args = []
kw_args = {}
for t in args:
if isinstance(t, expressions.MappingRuleExpression):
param_name = t.source
if isinstance(param_name, expressions.KeywordConstant):
param_name = param_name.value
else:
raise exceptions.MappingTranslationException()
kw_args[param_name] = t.destination
else:
pos_args.append(t)
for key, value in six.iteritems(kwargs):
if key in kw_args:
raise exceptions.MappingTranslationException()
else:
kw_args[key] = value
return tuple(pos_args), kw_args
def limit_memory_usage(quota_or_engine, *args):
if isinstance(quota_or_engine, int):
quota = quota_or_engine
else:
quota = get_memory_quota(quota_or_engine)
if quota <= 0:
return
total = 0
for t in args:
total += t[0] * sys.getsizeof(t[1], 0)
if total > quota:
raise exceptions.MemoryQuotaExceededException()
def p_error(p):
if p:
raise exceptions.YaqlGrammarException(
p.lexer.lexdata, p.value, p.lexpos)
else:
raise exceptions.YaqlGrammarException(None, None, None)
def register_param_constraint(self, param):
if param.name not in self._arg_spec.args \
and self._arg_spec.varargs != param.name:
raise exceptions.NoParameterFoundException(
function_name=self.function.func_name,
param_name=param.name)
if param.name in self.param_definitions:
raise exceptions.DuplicateParameterDecoratorException(
function_name=self.function.func_name,
param_name=param.name)
if self.is_context_aware and param.is_context:
raise exceptions.DuplicateContextDecoratorException(
function_name=self.function.func_name)
if self.context_owner_param_name and param.own_context:
raise exceptions.DuplicateContextOwnerDecoratorException(
function_name=self.function.func_name)
self.param_definitions[param.name] = param
if param.is_context:
self.is_context_aware = True
self.context_param_name = param.name
if param.is_self is None:
if param.name in self._arg_spec.args:
param.is_self = self._arg_spec.args.index(
param.name) == 0 and param.name == 'self'
if param.is_self:
self.self_param_name = param.name
if param.lazy:
raise exceptions.YaqlException("Self parameter cannot be lazy")
@staticmethod
def match(expr):
if not isinstance(expr, six.string_types):
return False
if re.match('^[\s\w\d.:]*$', expr):
return False
try:
YAQL(expr)
return True
except yaql_exc.YaqlGrammarException:
return False
except yaql_exc.YaqlLexicalException:
return False
def validate(self, value):
if self.constant_only:
if not isinstance(value,
yaql.language.expressions.Constant.Callable):
raise exceptions.YaqlExecutionException(
"Parameter {0} has to be a constant".format(self.name))
if self.function_only:
if not isinstance(value,
yaql.language.expressions.Function.Callable):
raise exceptions.YaqlExecutionException(
"Parameter {0} has to be a function".format(self.name))
if not self.lazy:
try:
res = value()
except Exception:
raise exceptions.YaqlExecutionException(
"Unable to evaluate parameter {0}".format(self.name),
sys.exc_info())
else:
res = value
context = value.yaql_context
self.validate_value(res)
return res, context