Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.s = EvalWithCompoundTypes()
def setUp(self):
self.s = EvalWithCompoundTypes()
def test_use_func(self):
self.s = EvalWithCompoundTypes(functions={"map": map, "str": str})
self.t('list(map(str, [-1, 0, 1]))', ['-1', '0', '1'])
with self.assertRaises(NameNotDefined):
self.s.eval('list(map(bad, [-1, 0, 1]))')
with self.assertRaises(FunctionNotDefined):
self.s.eval('dir(str)')
with self.assertRaises(FeatureNotAvailable):
self.s.eval('str.__dict__')
self.s = EvalWithCompoundTypes(functions={"dir": dir, "str": str})
self.t('dir(str)', dir(str))
:param ctx: The Context.
:return: The string, with snippets replaced.
"""
tempargs = shlex.split(args)
snippets = await get_servsnippets(ctx)
snippets.update(await get_snippets(ctx))
for index, arg in enumerate(tempargs): # parse snippets
snippet_value = snippets.get(arg)
if snippet_value:
tempargs[index] = snippet_value
elif ' ' in arg:
tempargs[index] = shlex.quote(arg)
return " ".join(tempargs)
class ScriptingEvaluator(EvalWithCompoundTypes):
def __init__(self, operators=None, functions=None, names=None):
super(ScriptingEvaluator, self).__init__(operators, functions, names)
self.nodes.update({
ast.JoinedStr: self._eval_joinedstr, # f-string
ast.FormattedValue: self._eval_formattedvalue, # things in f-strings
ast.ListComp: self._eval_listcomp,
ast.SetComp: self._eval_setcomp,
ast.DictComp: self._eval_dictcomp,
ast.comprehension: self._eval_comprehension
})
self.assign_nodes = {
ast.Name: self._assign_name,
ast.Tuple: self._assign_tuple,
ast.Subscript: self._assign_subscript
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script to perform one time initialization on a new AWS Account to be use for TheBoss"""
import argparse
import sys
import os
import alter_path
from lib import aws
from lib import configuration
from lib import console
try:
import simpleeval
eval = simpleeval.EvalWithCompoundTypes(functions={'range': range}).eval
except ImportError:
console.warning("Library 'simpleeval' not available, using the default python implementation")
class SubscriptionList(object):
def __init__(self, bosslet_config, topic):
self.bosslet_config = bosslet_config
self.client = bosslet_config.session.client('sns')
self.topic = topic
self.arn = self.to_arn(topic)
def to_arn(self, topic):
return 'arn:aws:sns:{}:{}:{}'.format(self.bosslet_config.REGION,
self.bosslet_config.ACCOUNT_ID,
topic)
def create(self):
# SimpleEval and simple_eval NOT WORK with compound types
try:
print(simple_eval('[1, 2, 3, 4]'))
except Exception as e:
print(e) # Sorry, List is not available in this evaluator
try:
my_eval = SimpleEval()
print(my_eval.eval('[1, 2, 3, 4]'))
except Exception as e:
print(e) # Sorry, List is not available in this evaluator
print()
# Compound Types
my_compound_types_eval = EvalWithCompoundTypes()
my_compound_types_eval.functions['len'] = len
# list
print(my_compound_types_eval.eval('[1, 2, 3, 4]')) # [1, 2, 3, 4]
print(my_compound_types_eval.eval('[1, 2] + [3, 4]')) # [1, 2, 3, 4]
print(my_compound_types_eval.eval('len([1, 2, 3, 4])')) # 4
print(my_compound_types_eval.eval('[1, 2, 1, 3, 4].count(1)')) # 2
print(my_compound_types_eval.eval('list("1234")')) # ['1', '2', '3', '4']
print()
# dict
print(my_compound_types_eval.eval('{"a": 1, "b": 999}')) # {'a': 1, 'b': 999}
print(my_compound_types_eval.eval('{"a": 1, "b": 999}["b"]')) # 999
print(my_compound_types_eval.eval('{"a": 1, "b": 999}.items()')) # dict_items([('a', 1), ('b', 999)])
print(my_compound_types_eval.eval('len({"a": 1, "b": 999})')) # 2
print(my_compound_types_eval.eval('dict([("a", 1), ("b", 999)])')) # {'a': 1, 'b': 999}
def prepare_evaluator(self):
"""
Setup evaluator engine
:return: Prepare evaluator engine
"""
simpleeval_params = self.params.get('simpleeval', {})
# update simpleeval safety options
for k, v in simpleeval_params.get('options', {}).items():
setattr(simpleeval, k.upper(), v)
evaluator = EvalWithCompoundTypes()
# self._evals_functions should mirror self.fns
# TODO: Make a test to ensure proper mirroring
evaluator.functions = self.fns
evaluator.names = self.names
# set the operators
if simpleeval_params.get('operators'):
evaluator.operators = simpleeval_params.get('operators')
return evaluator
'str': fn_str,
'float': fn_float,
'int': fn_int,
'has': fn_has,
'next': fn_list_next,
'join': fn_list_join,
'trim': fn_str_trim,
'extend': fn_extend,
'update': fn_update,
'encrypt': fn_encrypt_names(names2),
'decrypt': fn_decrypt_names(names2),
'inc': fn_inc_names(names2),
'linspace': fn_linspace,
'arange': fn_arange
})}
s = EvalWithCompoundTypes(names=names2, functions=fns)
try:
v = s.eval(expr=expr)
except Exception as ex:
v = expr
errors.append([expr, ex])
v = filter_value(v)
return v