annotate pyikriam/lazy/www/work/find.py @ 308:f6f56a47f383

implemented is_tavern_enough
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 04 Jan 2009 23:49:41 +0800
parents 60c4b4b78a01
children
rev   line source
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
1 from lxml import etree
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
2 from cStringIO import StringIO
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
3
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
4 class Finder:
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 dom_tree = None
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
7 xpath = None
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 def __init__(self, working_product):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
10 self.working_prodcut = working_product
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 self.encoding = 'utf8'
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 find(self, express , callback = None):
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
15
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
16 if self.dom_tree is None: self.set_dom_tree(self.working_prodcut.content)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
17
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
18 xpath = self.dom_tree.xpath(express)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
19
246
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
20 if callback: return self.callback(xpath)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
21 return xpath
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
22
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
23 def set_dom_tree(self, content):
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
24 stream = StringIO(content)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
25
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
26 parser = etree.HTMLParser(encoding=self.encoding)
60c4b4b78a01 code clean
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents: 62
diff changeset
27 self.dom_tree = etree.parse(stream, parser)