62
|
1 from lazy.www.work.fetch import Fetcher
|
|
2 from lazy.www.work.find import Finder
|
|
3 from lazy.www.core import SemiProduct
|
|
4 class WorkFlow:
|
|
5
|
|
6 serial_number = 0
|
|
7 working_product = None
|
|
8 worker = None
|
|
9
|
|
10 def __init__(self, worker):
|
|
11 self.set_worker(worker)
|
|
12
|
|
13 def set_worker(self, worker):
|
|
14 self.worker = worker
|
|
15 if self.worker.working_product is None:
|
|
16 self.working_product = SemiProduct()
|
|
17 else:
|
|
18 self.working_product = self.worker.working_product
|
|
19
|
|
20 def get_content(self):
|
|
21 return self.working_product.content
|
|
22
|
|
23 def change_worker(self, new_worker):
|
246
|
24 self.serial_number += 1
|
|
25 self.last_work = self
|
62
|
26 self.worker = new_worker
|
|
27
|
|
28 def is_fetcher(self, obj):
|
246
|
29 if obj.__class__.__name__ == 'Fetcher': return True
|
62
|
30
|
|
31 def get(self, data = {} ):
|
246
|
32 if self.worker.__class__.__name__ != 'FileStorager' and not self.is_fetcher(self.worker) :
|
62
|
33 self.change_worker( Fetcher(self.working_product) )
|
|
34
|
|
35 self.working_product.content = self.worker.get(data)
|
|
36 return self
|
|
37
|
|
38 def post(self, data = {} ):
|
|
39 if not self.is_fetcher(self.worker):
|
|
40 self.change_worker( Fetcher(self.working_product) )
|
|
41
|
|
42 self.working_product.content = self.worker.post(data)
|
|
43 return self
|
|
44
|
|
45 def is_finder(self, obj):
|
246
|
46 if obj.__class__.__name__ == 'Finder': return True
|
|
47
|
|
48 def findall(self, expresses):
|
|
49 if not self.is_finder(self.worker):
|
|
50 self.change_worker( Finder(self.working_product) )
|
|
51
|
|
52 ret = {}
|
|
53 for e in expresses.keys():
|
|
54 try:
|
|
55 ret[e] = self.worker.find(expresses[e])
|
|
56 except:
|
|
57 pass
|
|
58
|
|
59 self.working_product.content = ret
|
|
60
|
|
61 return self
|
62
|
62
|
|
63 def find(self, express):
|
|
64 #if not self.is_finder(self.worker):
|
246
|
65 self.change_worker( Finder(self.working_product) )
|
|
66 self.last_working_product = self.working_product
|
62
|
67 self.working_product.content = self.worker.find(express)
|
|
68
|
246
|
69 return self
|
|
70
|
|
71 def process(self, fn):
|
|
72 self.working_product.content = fn(self.working_product.content)
|
62
|
73 return self |