Skip to content

Commit

Permalink
Issue #93: Use THEN to execute (send method action)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmo committed Oct 23, 2018
1 parent 64e04ac commit 533a81b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
13 changes: 10 additions & 3 deletions amazon_dash/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@


class Action:
def __init__(self, use, params=None, condition=None, template=None):
def __init__(self, use, config=None, params=None, condition=None, template=None):
self.use = use
self.params = params
self.condition = condition.lower() if condition else condition
self.template = template
self.config = config

def is_first_run_action(self):
return self.condition not in ALL_COMPLETE_CONDITIONS

def evaluate_condition(self, value):
return self.condition == value or (value in ACTION_STATUSES and self.condition == COMPLETE_CONDITION)

def send(self, **params):
new_params = self.params.copy()
new_params.update(params)
self.config.templates.use(self.use).render(**new_params).send()


class Actions:
def __init__(self, actions):
self._actions: List[Action] = [Action(**action) for action in actions]
def __init__(self, actions, config):
self._actions: List[Action] = [Action(config=config, **action) for action in actions]
self.config = config

def get_first_run_actions(self):
return filter(lambda x: x.is_first_run_action(), self._actions)
Expand Down
3 changes: 2 additions & 1 deletion amazon_dash/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ def read(self):
self.templates = then.templates(LoadTemplates(self.file))
devices = LoadConfig(self.file, 'devices')
devices = {device['mac']: device for device in devices.data} if isinstance(devices.data, list) else devices
devices = {key: Device(key, value.get('name'), value.get('actions', [])) for key, value in devices.items()}
devices = {key: Device(key, value.get('name'), value.get('actions', []), self)
for key, value in devices.items()}
self.devices = devices
pass
# try:
Expand Down
14 changes: 8 additions & 6 deletions amazon_dash/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def __init__(self, src, name, actions, config=None):
:param str src: Mac address
:param data: device data
"""
config = config or {}
config = config
actions = actions or []

if isinstance(src, Device):
src = src.src
self.src = src.lower()
self.name = name or self.src
self.actions = Actions(actions)
self.actions = Actions(actions, config)
self.config = config

def execute(self, root_allowed=False):
Expand All @@ -59,7 +59,7 @@ def execute(self, root_allowed=False):
success = True
results = []
for action in self.actions.get_first_run_actions():
result = self.execute_action(action)
result = self.execute_action(action, {'mac': self.src})
success = result.status
results.append(result)
if not success and self.on_error == 'fail':
Expand All @@ -69,11 +69,13 @@ def execute(self, root_allowed=False):
self.execute_action(action)
return results

def execute_action(self, action):
def execute_action(self, action, params=None):
result = Result()
params = params or {}
try:
result.message = action.send()
except Exception as e:
result.message = action.send(**params)
# except Exception as e: # TODO: disabled temporally
except ImportError as e:
result.message = 'Error executing the device {} in action {}: {}'.format(self.name, action, e)
result.exception = e
else:
Expand Down

0 comments on commit 533a81b

Please sign in to comment.