diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2013-06-07 11:28:57 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2013-06-07 11:28:57 -0400 |
commit | 85d13294c7570cc32e75c0413924672e72ff3f60 (patch) | |
tree | 5b2241897befc7159ea1eef16dfe53e34c0e0b38 | |
parent | 71147a87b082b453d2d6f6a2815ce6b3f5605179 (diff) | |
download | ipsutils-85d13294c7570cc32e75c0413924672e72ff3f60.tar.gz |
Add doctrings and object instance checks
-rw-r--r-- | ipsutils/task.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/ipsutils/task.py b/ipsutils/task.py index 2c1ccec..aa0166c 100644 --- a/ipsutils/task.py +++ b/ipsutils/task.py @@ -19,14 +19,36 @@ class TaskException(Exception): class InternalTaskException(Exception): pass +class TaskControllerException(Exception): + pass + class Controller(object): def __init__(self): + """Simple FILO execution stack of task objects + + Example: + from ipsutils import tasks + + #Create a new controller + controller = task.Controller() + #Add a tasks + controller.task(task.Task(name='Some task')) + controller.task(task.Task(name='Some other task')) + #Execute tasks + controller.do_tasks() + + Output: + + Running task: Some task + + Running task: Some other task + """ self.stack = [] def task(self, t): """ t: Task object """ + if not isinstance(t, Task): + raise TaskControllerException("'{}' is not an instance of Task".format(type(t))) self.stack.append(t) def do_tasks(self, atexit=None): @@ -53,6 +75,14 @@ class Controller(object): class Task(object): def __init__(self, *args, **kwargs): + """Task engine base class. + + Keyword arguments: + name = Description of the task + func = Function reference + cls = An instance of the calling class (if any) + + """ self.name = '' if 'name' in kwargs: self.name = kwargs['name'] @@ -64,6 +94,8 @@ class Task(object): if 'cls' in kwargs: self.cls = kwargs['cls'] + if not isinstance(self.cls, object): + raise TaskException("'{}' is not an instance of a class".format(type(self.cls))) else: self.cls = object() @@ -81,6 +113,8 @@ class Task(object): class Internal(Task): def __init__(self, *args, **kwargs): + """Implements an internal task denoted by a slightly different output. + """ super(Internal, self).__init__(self, *args, **kwargs) def run(self): |