changeset 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 0cfc7a19a4d2
children 3ba3edda6d1e 51a8a81d0bb6
files pyikriam/buildings.py pyikriam/ikariam.py
diffstat 2 files changed, 13 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/pyikriam/buildings.py	Sun Nov 02 04:12:23 2008 +0800
+++ b/pyikriam/buildings.py	Sun Nov 02 09:59:51 2008 +0800
@@ -20,7 +20,7 @@
 
 class townhall(position):
     xpath_patterns = {
-        'level': '/div/div/div[@class=\'buildingLevel\']/text()'
+        'level': 'div[@class=\'buildingLevel\']/text()'
         }
 
     def __init__(self, city_id, idx, baseurl):
@@ -30,7 +30,7 @@
     def _sync(self, page):
         parser = etree.HTMLParser(encoding='utf8')
         page_dom = etree.parse(StringIO(page), parser)
-        xpath_building = '/html/body/div/div'
+        xpath_building = '/html/body/descendant::'
         for name, ptn in self.xpath_patterns.items():
             path = xpath_building + ptn
             value = page_dom.xpath(path)[0]
--- a/pyikriam/ikariam.py	Sun Nov 02 04:12:23 2008 +0800
+++ b/pyikriam/ikariam.py	Sun Nov 02 09:59:51 2008 +0800
@@ -70,13 +70,13 @@
 
 class IkariamCity:
     data_patterns = {
-        'gold': '/div[@id=\'globalResources\']/ul/li/a/span[@id=\'value_gold\']/text()',
-        'inhabitants': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_inhabitants\']/text()',
-        'wood': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wood\']/text()',
-        'wine': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_wine\']/text()',
-        'marble': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_marble\']/text()',
-        'crystal': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_crystal\']/text()',
-        'sulfur': '/div[@id=\'cityResources\']/ul/li/span[@id=\'value_sulfur\']/text()'
+        'gold': 'value_gold',
+        'inhabitants': 'value_inhabitants',
+        'wood': 'value_wood',
+        'wine': 'value_wine',
+        'marble': 'value_marble',
+        'crystal': 'value_crystal',
+        'sulfur': 'value_sulfur'
         }
     def __init__(self, id, core ):
         self.core = core
@@ -88,14 +88,14 @@
         parser = etree.HTMLParser(encoding='utf8')
         page_dom = etree.parse(StringIO(page), parser)
 
-        xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']"
-        for name, path in self.data_patterns.items():
-            xpath = xpath_globalinfo + path
+        xpath_globalinfo = '/descendant::*[@id=\'%s\']/text()'
+        for name, tag_id in self.data_patterns.items():
+            xpath = xpath_globalinfo % (tag_id)
             value = page_dom.xpath(xpath)[0]
             setattr(self, name, value)
             pass
         
-        xpath_mainview = '/html/body/div/div/div[@id=\'mainview\']/ul/li'
+        xpath_mainview = '/descendant::div[@id=\'mainview\']/ul/li'
         pos_doms = page_dom.xpath(xpath_mainview)
         positions = [self._mk_position(pos_dom, idx)
                      for idx, pos_dom in enumerate(pos_doms)]