view intentmademanifest/actions.py @ 18:4fd7fb12b782

TWO AWESOME TOOLS!
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 22 Aug 2013 13:08:36 -0700
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