annotate pyikriam/lazy/www/__init__.py @ 367:d9d3cdff27c6

handling field not found error
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 01 Mar 2009 22:14:05 +0800
parents 60c4b4b78a01
children
rev   line source
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
2 #
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
3 # Copyright 2008 Hsin Yi, Chen
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
4 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
5 [Note] the project is not available yet.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
6
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
7 A web page fetcing tool chain that has a JQuery-like selector and supports chain working.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
8
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
9 Here is an exmaple can show the the main idea, To restrive a content you want
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
10 in a div box in a web page, and then post and restrive next content in the other
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
11 web page with the param you just maked from the content in first restriving.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
12 finally, storage the production.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
13
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
14 def func(s):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
15 return {'msg':s}
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 try:
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
18 c("http://example.tw/").get().find("////ul/text()") \
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
19 .build_param( func ).post_to("http://example2.com") \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
20 .save_as('hellow.html')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
21 except:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
22 pass
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
23
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
24 more complex example
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
25
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
26 try:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
27 c("http://example.tw/").retry(4, '5m').get() \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
28 .find("#id > div"). \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
29 .build_param( func ).post_to("http://example2.com") \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
30 .save_as('hellow.html') \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
31 .end().find("#id2 > img").download('pretty-%s.jpg'). \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
32 tar_and_zip("pretty_girl.tar.gz")
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
33 except NotFound:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
34 print "the web page is not found."
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
35 except NoPermissionTosave:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
36 print "the files can not be save with incorrect permission."
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
37 else:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
38 print "unknow error."
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
39 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
40 from lazy.www.work import WorkFlow
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
41 from lazy.www.work.fetch import Fetcher
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
42 from lazy.www.work.storage import FileStorager
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
43 from lazy.www.core import SemiProduct
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
44 import os
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
45 import sys
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
46 import re
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
47
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
48 def parse_scheme(scheme):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
49 try:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
50 return re.findall("(\w+):\/\/(.*\/?)",scheme)[0]
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
51 except:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
52 sys.stdout.write("the scheme is not supported.")
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
53 sys.exit()
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
54
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
55 def c(scheme):
62
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 connect to a web apge
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 >>> c('http://localhost:8080').get().worker.working_product.content
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
60 'It works!!\\n'
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 >>> c('http://localhost:8080').get().find('//text()')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
63 'It works!!\\n'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
64 """
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
65 target_type, path = parse_scheme(scheme)
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
66
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
67 #@todo: SemiProduct Factory.
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
68 if 'file' == target_type:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
69 s= SemiProduct(source=path)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
70 worker = FileStorager(s)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
71 else:
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
72 s= SemiProduct(source=scheme)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
73 worker = Fetcher(s)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
74 return WorkFlow(worker)
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
75
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
76 if __name__ == '__main__':
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
77 import doctest
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
78 doctest.testmod()