]> mj.ucw.cz Git - eval.git/blob - t/moe/pipeline.py
1804c5bec732562b718fee667d0b78c4e606a634
[eval.git] / t / moe / pipeline.py
1 #!/usr/bin/env python
2
3 import sys
4 import bisect
5 import imp
6 import moe.log
7
8 class MoePipeline:
9     """Moe pipeline."""
10
11     def __init__(self, name):
12         self.pipe = []
13         self.index = -1
14         self.name = name
15
16     def insert(self, pri, name, fun):
17         triple = (pri,name,fun)
18         pos = bisect.bisect(self.pipe, triple)
19         if pos <= self.index:
20             raise RuntimeError, "MoePipeline.insert cannot alter the past"
21         self.pipe.insert(pos, triple)
22
23     def dump(self, file=sys.stdout):
24         file.write(">>> Pipeline %s\n" % self.name)
25         for pri,name,fun in self.pipe:
26             file.write("%03d %s\n" % (pri,name))
27
28     def run(self, *args):
29         self.index = 0
30         while self.index < len(self.pipe):
31             (pri,name,fun) = self.pipe[self.index]
32             moe.log.verbose(">> Running %s:%s\n" % (self.name,name))
33             fun(*args)
34             self.index += 1
35         self.index = -1
36
37     def add_hook(self, name):
38         modname = "moe.hooks." + name
39         moe.log.verbose(">> Loading hook %s\n" % name)
40         if not sys.modules.has_key(modname):
41             fp, path, desc = imp.find_module(name, ["moe/hooks"])
42             try:
43                 imp.load_module(modname, fp, path, desc)
44             finally:
45                 fp.close()
46         sys.modules[modname].init(self)
47
48     def configure(self, names):
49         for name in names.split():
50             self.add_hook(name)