view intentmademanifest/actions.py @ 19:74ada5cb1cb8 default tip

more tools
author Jeff Hammel <k0scist@gmail.com>
date Fri, 04 Dec 2015 12:55:31 -0800
parents c7585bd93680
children
line wrap: on
line source

#!/usr/bin/env python

"""
``intentmademanifest.actions`` is a dependency resolver for python instance
methods
"""

# TODO: generalize, if possible.  For instance, we don't care about
# parameter space; should we? how?

class Actions(object):

    @classmethod
    def requires(cls, method, *requirements):
        """
        Require all dependencies to be run before invoking the method.
        - requirements: method names
        """
        # TODO: as is, the intent is to run all of the requirements
        # and then invoke ``method``
        # alternatively, e.g. via subclass, one could err out if the 
        # requirements have not been run

class ActionsCLI(object):
    """command line handler for an actions-based class"""
    def __init__(self):
        raise NotImplementedError("TODO")

requires = Actions.requires

if __name__ == '__main__':
    # TODO: -> test

    class ActionsExample(object):
        """example class for illustration of ``intentmademanifest.actions``"""

        def foo(self):
            self.attr = 1

        @requires('foo')
        def bar(self):
            self.attr *= 2

        def fleem(self):
            self.another_attr = 3

        @requires('bar', 'fleem')
        def result(self):
            return self.attr * self.another_attr

    instance = ActionsExample()
    assert instance.result() == 6