from .. import log
+SUBMODS = ['thread', 'pbs']
+
+
class Job (object):
"""Job request structure for `JobManager`.
def _receive_job(self):
raise NotImplementedError
+
+
+class IsSubclass (object):
+ """A safe subclass comparator.
+
+ Examples
+ --------
+
+ >>> class A (object):
+ ... pass
+ >>> class B (A):
+ ... pass
+ >>> C = 5
+ >>> is_subclass = IsSubclass(A)
+ >>> is_subclass(A)
+ True
+ >>> is_subclass = IsSubclass(A, blacklist=[A])
+ >>> is_subclass(A)
+ False
+ >>> is_subclass(B)
+ True
+ >>> is_subclass(C)
+ False
+ """
+ def __init__(self, base_class, blacklist=None):
+ self.base_class = base_class
+ if blacklist == None:
+ blacklist = []
+ self.blacklist = blacklist
+ def __call__(self, other):
+ try:
+ subclass = issubclass(other, self.base_class)
+ except TypeError:
+ return False
+ if other in self.blacklist:
+ return False
+ return subclass
+
+
+def get_manager(submod):
+ """
+ >>> get_manager('thread')
+ <class 'pysawsim.manager.thread.ThreadManager'>
+ >>> get_manager('wookie')
+ Traceback (most recent call last):
+ ...
+ AttributeError: 'module' object has no attribute 'wookie'
+ """
+ this_mod = __import__(__name__, fromlist=[submod])
+ sub_mod = getattr(this_mod, submod)
+ class_selector = IsSubclass(base_class=JobManager, blacklist=[JobManager])
+ for x_name in dir(sub_mod):
+ x = getattr(sub_mod, x_name)
+ if class_selector(x) == True:
+ return x
+ raise ValueError('no JobManager found in %s' % sub_mod.__name__)