view pyikriam/lazy/www/work/fetch.py @ 318:e5e25d4a2c2f

merged
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 12 Jan 2009 22:26:48 +0800
parents 60c4b4b78a01
children
line wrap: on
line source

import urllib2
import urllib
import cookielib
import os

class Fetcher:
    
    opener = None
    
    working_product = None
    
    """
    A Semi Production Decoration for content fetching.
    
    handles content restriving.
    
    >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
    >>> o.get().working_product.content
    'It works!!\\n'
    """
    def __init__(self, working_product):
        self.working_product = working_product
    
    def get(self, **kwds):
        return self.open(kwds)

    def post(self, **kwds):
        return self.open(kwds)
    
    def open(self, data = {} ):
        """
        send datas via http post method.
        
        >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
        >>> o.post({'a':'b'}).working_product.content
        'It works!!\\n'
        """        
        res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data))
        return res.read()    

    def refer(self, refer_url):
        """
        refer getter/setter.

        >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
        >>> o.refer('http://www.example.com')        
        """
        raise NotImplementedError

    def retry(self, count = 0, intval = 0, timeout = 0):
        """
        retry to fetch the content.

        >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
        >>> o.retry(4)        
        """        
        raise NotImplementedError
    
class Retry:
    
    """
     A Fetcher Decoration for retry goal.
     
     
    """
    def __init__(self, fetcher):
        raise NotImplementedError
    
if __name__ == '__main__':
    import doctest
    doctest.testmod()