Added more actions, and improved action handling. #5

Merged
merged 5 commits into from Oct 26, 2016

Conversation

Projects
None yet
3 participants
Collaborator

petevg commented Oct 20, 2016

Plan can now select from actions that have arguments other than a unit.

Cleaned up TODOs and generally made things better.

@johnsca @bcsaller

Added more actions, and improved action handling.
Plan can now select from actions that have arguments other than a unit.

Cleaned up TODOs and generally made things better.
for obj in objects:
- await func(model, obj, **kwargs)
- self[func.__name__] = wrapped
+ await enforce.runtime_validation(func(
@bcsaller

bcsaller Oct 21, 2016

Contributor

Interesting catch.

matrix/plugins/glitch/actions.py
- self[func.__name__] = wrapped
+ await enforce.runtime_validation(func(
+ rule, model, obj, **kwargs))
+ self[func.__name__] = {'func': wrapped, 'args': func.__annotations__}
@bcsaller

bcsaller Oct 21, 2016

Contributor

There are a number of ways this might be better to do. I am still quite wary of just matching names like we do here (and like I might do a few other places).

I'd suggest (even though it will have the same outcome) that we future proof this a little bit by using inspect.signature. The Signature object it returns should have all the function information we want.

@petevg

petevg Oct 24, 2016

Collaborator

Refactored. Pushing shortly.

context.bus.dispatch(
origin="glitch",
- payload=functools.partial(actionf, model, objects, **action),
+ payload={'action': actionf.__name__, **action},
@bcsaller

bcsaller Oct 21, 2016

Contributor

we might be better off here if this were the inspect.Signature object as well. It should handle things like the qualname vs name handling

@petevg

petevg Oct 24, 2016

Collaborator

Since this is strictly for logging, I think that I'm comfortable with trusting the __name__ for now. If we wind up passing the function through more wrappers and wind up mangling it, we can dig into it with inspect ...

@@ -10,6 +10,49 @@ class InvalidModel(Exception):
pass
+_MODEL_OPS = {
@bcsaller

bcsaller Oct 21, 2016

Contributor

I like this change, I do things we'll want a method for pushing new entries in from plugins. Maybe glitch can use a passed in copy of this in generate plan and all the plugins get called to see if they update that struct.

@petevg

petevg Oct 24, 2016

Collaborator

Passing this as an arg makes a lot of sense. I'd like to hold off on doing so for now, though -- without seeing a plugin that wants to interact with this dict, it's hard to tell where to put it. (Maybe it's a .yaml file that gets read in when we create the context.)

matrix/plugins/glitch/plan.py
- # TODO: get some good default args for each action
- 'selectors': selectors
- }
+ sig = [arg for arg in Actions[action]['args'] if arg in _MODEL_OPS.keys()]
@bcsaller

bcsaller Oct 21, 2016

Contributor

Like I mentioned I'd like something a little more formal than this convention here, but we can leave that as a TODO. I am not sure of the best path forward here yet.

@petevg

petevg Oct 24, 2016

Collaborator

Refactored. Pushing shortly.

tests/glitch/test_actions.py
-class TestAction(unittest.TestCase):
- def test_define(self):
- self.assertFalse('faux_action' in Actions)
+#class TestAction(unittest.TestCase):
@bcsaller

bcsaller Oct 21, 2016

Contributor

There must be something we can test here. :)

@petevg

petevg Oct 24, 2016

Collaborator

Whoops. Forgot to fix these tests. Doing so ...

tests/glitch/test_main.py
@@ -15,28 +15,15 @@ def setUp(self):
self.loop = asyncio.get_event_loop()
self.context = mock.Mock()
self.rule = mock.Mock()
+ self.rule.log.debug = print
@bcsaller

bcsaller Oct 21, 2016

Contributor

These methods don't have the same signature and can break when used in this way. We can add a test.utils that can set up a default handler that does this.

@petevg

petevg Oct 24, 2016

Collaborator

This whole test is really an integration test, and is getting increasingly unwieldy. Replacing it with a matrix test plan.

Let me know what you think of the feedback, After this I think we can merge this. Thanks

petevg added some commits Oct 21, 2016

Added glitch_plan config.
Per a discussion, glitch is a plugin, but we're treating it as a special
plugin for now, and giving it access to the main config. We can come up
with a more general solution for plugin config in the future.
Safer inspection of function args for function sig.
Addressed comments in PR: now use inspect.signature to extract args from
a function, rather than relying on the __annotation__ we get from type
hinting. Produced a net reduction in the lines of code, which means that
it's probably a good change :-)
Fixed up tests.
Replaced test_main.py with a proper matrix test plan. Uncommented and
fixed the actions unit tests.
Glitch now skips actions if a selector returns no objects.
A randomly generated glitch plan may do something like get rid of more
units than are actually in an application. In this case, we don't want
to throw an error -- we just want to log about it and move on.

LGTM, thanks

@johnsca johnsca merged commit 0a682d3 into master Oct 26, 2016

@petevg petevg deleted the feature/glitch-more-actions branch Dec 20, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment