/
condition.py
51 lines (43 loc) · 1.54 KB
/
condition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from hal_configurator.lib.command_base import OperationBase, \
InvalidCommandArgumentsError, ArgumentDescriptor
CONDITION_BUNDLE = 'Incondition'
OperationBase.register_bundle(CONDITION_BUNDLE)
class Condition(OperationBase):
"""Create a condition"""
code = "condition"
def __init__(self, *args, **kwargs):
super(Condition, self).__init__(*args, **kwargs)
self.result = ''
@classmethod
def get_arg_descriptors(cls):
return [
ArgumentDescriptor("Expression", "Python Expression", "text"),
ArgumentDescriptor("Bundle", "Operations Bundle",
"OperationsBundle"),
]
@classmethod
def get_empty_dict(cls):
return{
"Code": cls.get_code(),
"Type": cls.get_name(),
"Arguments": {
"Expression": "\"true\"==\"true\"",
"Bundle": {
"Name": CONDITION_BUNDLE,
"Operations": []
}
}
}
def set_args(self, Expression, Bundle):
self.kwargs["Expression"] = self.expression = Expression
self.kwargs["Bundle"] = self.bundle = Bundle
def run(self):
is_valid, errors = self.validate_args()
if is_valid:
# print self.expression
result = eval(self.expression)
if result:
self.executor.execute_bundle_within_current_scope(self.bundle)
else:
raise InvalidCommandArgumentsError(str(errors))
__plugin__ = Condition