view pyikriam/lazy/www/work/__init__.py @ 374:e551351b18de

updated for timestamp of scores information
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Tue, 14 Apr 2009 17:01:17 +0800
parents 60c4b4b78a01
children
line wrap: on
line source

from lazy.www.work.fetch import Fetcher
from lazy.www.work.find import Finder
from lazy.www.core import SemiProduct
class WorkFlow:
    
    serial_number = 0
    working_product = None
    worker = None
    
    def __init__(self, worker):
        self.set_worker(worker)

    def set_worker(self, worker):
        self.worker = worker
        if self.worker.working_product is None:
            self.working_product = SemiProduct()
        else:
            self.working_product = self.worker.working_product
    
    def get_content(self):
        return self.working_product.content
     
    def change_worker(self, new_worker):
        self.serial_number += 1        
        self.last_work = self
        self.worker = new_worker
        
    def is_fetcher(self, obj):
        if obj.__class__.__name__ == 'Fetcher': return True
    
    def get(self, data = {} ):
        if self.worker.__class__.__name__ != 'FileStorager' and not self.is_fetcher(self.worker) :
            self.change_worker( Fetcher(self.working_product) )
        
        self.working_product.content = self.worker.get(data)
        return self
            
    def post(self, data = {} ):
        if not self.is_fetcher(self.worker):
            self.change_worker( Fetcher(self.working_product) )
        
        self.working_product.content = self.worker.post(data)
        return self
    
    def is_finder(self, obj):
        if obj.__class__.__name__ == 'Finder': return True
    
    def findall(self, expresses):
        if not self.is_finder(self.worker):
            self.change_worker( Finder(self.working_product) )
        
        ret = {}
        for e in expresses.keys():
            try:
                ret[e] = self.worker.find(expresses[e])
            except:
                pass

        self.working_product.content = ret
        
        return self
    
    def find(self, express):
        #if not self.is_finder(self.worker):
        self.change_worker( Finder(self.working_product) )
        self.last_working_product = self.working_product
        self.working_product.content = self.worker.find(express)
        
        return self
    
    def process(self, fn):
        self.working_product.content = fn(self.working_product.content)
        return self