annotate pyikriam/lazy/www/work/find.py @ 175:9f248c8460ce

Simplize xpath patterns. - Use 'descendant' aix name simplize patterns.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 09:59:51 +0800
parents a4c364888197
children 60c4b4b78a01
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 parser = etree.HTMLParser(encoding=self.encoding)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
14 self.dom_tree = etree.parse(StringIO(self.working_prodcut.content), parser)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
15
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
16 def find(self, express , callback = None):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
17 xpath = self.dom_tree.xpath(express)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
18
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
19 if callback is None:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
20 ret = xpath
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
21 else:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
22 ret = self.callback(xpath)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
23 return ret