3 '''Loader of item groups'''
9 from core.bits import *
11 # FIXME: Need to implement predicate and requirement stuff and
14 class TaskFactory(object):
15 '''Factory to generate tasks'''
16 def __init__(self, classes, dependency_strategy=core.deptools.DigraphDependencyStrategy):
17 self.classes = classes
18 self.dependency_strategy = dependency_strategy
20 def task_from_requirements(self, requirements):
21 '''This is to create a new task given a set of requirements
24 items = self.dependency_strategy.instantiate_items(self.classes)
25 items = set(items.values()) # TODO: Fix instantiate_items return. this is unintuitive
27 # Define a clear goal node that all existing goals depend on
28 goal = TaskComplete(self.dependency_strategy.find_goal_nodes(items))
30 assert set(self.dependency_strategy.early_iter_all_items(goal)) == items
32 # Filter out things that aren't for this task
33 goal = self.dependency_strategy.filter_dependency_graph(requirements, goal)
34 assert goal.name == 'TaskComplete'
37 items = set(self.dependency_strategy.early_iter_all_items(goal))
38 items = self.dependency_strategy.make_group_dependencies_explicit_for_items(items)
41 # Create task for great justice
42 return Task(items, requirements, goal)
44 class ObjectItemLoader(object):
45 '''Load in items defined by python objects and make a TaskFactory from them'''
48 def taskfactory_from_objects(self, objects):
49 ''''process class items represented by simple python objects and return a TaskFactory'''
51 marsh = core.marshal.ItemConverter()
54 classes.append(marsh.itemdict_to_item_class(o))
56 # Have dependencies refer to the classes they depend on, not just the names
57 # FIXME: This should probably be in the marshaller module
58 class_dict = dict((cls.__name__, cls) for cls in classes)
60 # TODO: Warn more sanely about dependencies on non-existant items
61 cls.depends = tuple(class_dict[c] for c in cls.depends)
62 if issubclass(cls, Group):
63 cls.contains = tuple(class_dict[c] for c in cls.contains)
65 return TaskFactory(classes)
68 def check_sanity(self, objdict):
69 '''check that objdict is actually in the correct form
70 if objdict isn't in the correct form for marshalling, raise
72 name,group = objdict.get('name'),objdict.get('group')
73 if not name and not group:
74 raise ValueError('dict has neither name nor group keys',objdict)
76 raise ValueError('dict has both name and group keys')
78 contains = objdict.get('contains')
80 raise ValueError('group dict has no contains key')
81 if len(contains) == 0:
82 raise ValueError('group contains list is empty')
85 class JSONItemLoader(ObjectItemLoader):
86 '''Load in items defined by some json and make a TaskFactory from them'''
89 def load_item_classes_from_file(cls, f):
90 ''''load json items from a file and return a TaskFactory'''
91 return cls.taskfactory_from_objects(json.load(f))
94 def load_item_classes_from_string(cls, data):
95 ''''load json items from a file and return a TaskFactory'''
96 return cls.taskfactory_from_objects(json.loads(data))