annotate pyikriam/lazy/www/work/fetch.py @ 246:60c4b4b78a01

code clean
author "Hisn Yi, Chen <ossug.hychen@gmail.com>"
date Mon, 01 Dec 2008 00:25:07 +0800
parents a4c364888197
children
rev   line source
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
1 import urllib2
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
2 import urllib
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
3 import cookielib
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
4 import os
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
5
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
6 class Fetcher:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
7
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
8 opener = None
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
9
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
10 working_product = None
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
11
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
12 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
13 A Semi Production Decoration for content fetching.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
14
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
15 handles content restriving.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
16
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
17 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
18 >>> o.get().working_product.content
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
19 'It works!!\\n'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
20 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
21 def __init__(self, working_product):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
22 self.working_product = working_product
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
23
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
24 def get(self, **kwds):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
25 return self.open(kwds)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
26
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
27 def post(self, **kwds):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
28 return self.open(kwds)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
29
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
30 def open(self, data = {} ):
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
31 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
32 send datas via http post method.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
33
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
34 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
35 >>> o.post({'a':'b'}).working_product.content
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
36 'It works!!\\n'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
37 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
38 res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data))
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
39 return res.read()
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
40
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
41 def refer(self, refer_url):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
42 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
43 refer getter/setter.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
44
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
45 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
46 >>> o.refer('http://www.example.com')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
47 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
48 raise NotImplementedError
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
49
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
50 def retry(self, count = 0, intval = 0, timeout = 0):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
51 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
52 retry to fetch the content.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
53
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
54 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
55 >>> o.retry(4)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
56 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
57 raise NotImplementedError
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
58
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
59 class Retry:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
60
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
61 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
62 A Fetcher Decoration for retry goal.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
63
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
64
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
65 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
66 def __init__(self, fetcher):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
67 raise NotImplementedError
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
68
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
69 if __name__ == '__main__':
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
70 import doctest
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
71 doctest.testmod()