changeset 170:7df753a99926

merged
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 02:23:13 +0800
parents a60cf4d1a6dd (current diff) 8f699a9da6c0 (diff)
children 2efa901871e5
files
diffstat 7 files changed, 301 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/overall.yaml	Sun Nov 02 02:21:05 2008 +0800
+++ b/overall.yaml	Sun Nov 02 02:23:13 2008 +0800
@@ -22,15 +22,12 @@
      # 發明 (兵工廠)
      - is_invention_researched:
         0: research_knowledge
-     # 異國文化
+     # 異國文化, 建造博物館
      - is_culturalexchange_researched:
-        0: research_seafaring
+        0: research_knowledge
      # 希臘火, 建造噴火船
      - is_greekfire_researched:
         0: research_seafaring
-     # 文化交流, 建造博物館
-     - is_culturalexchange_researched:
-        0: research_knowledge
         
    1: run_defence
    # we are being attacked!!!!!!!!!!!!!
--- a/pyikriam/__init__.py	Sun Nov 02 02:21:05 2008 +0800
+++ b/pyikriam/__init__.py	Sun Nov 02 02:23:13 2008 +0800
@@ -1,54 +1,3 @@
-from lazy.www import c
-from lconf import LoadConfigfile
-import cookielib
-import os
-import urllib2
-import urllib
-class Ikariam:
-
-    cities = {}
-
-    def __init__(self):
-        self.COOKIEFILE = '/tmp/ikariam.lwp'
-	self.confdata=LoadConfigfile().cd
-        self.baseurl='http://'+self.confdata['server']
-        self.cj = cookielib.LWPCookieJar()
-        if os.path.isfile(self.COOKIEFILE):
-            self.cj.load(self.COOKIEFILE)
- 
-        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
-        opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.1.12pre) Gecko/20071220 BonEcho/2.0.0.12pre')]
-        urllib2.install_opener(opener)
-
-        self.login()
+from ikariam import Ikariam
 
-    def login(self):     
-	print "login to %s...." % self.confdata['server']
-        params = {"universe":self.confdata['server'], \
-        "name":self.confdata['user'], \
-        "password":self.confdata['pass']}
-        ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
-        self.cj.save(self.COOKIEFILE)
-        
-    def logout(self):
-	print "logut from %s...." % self.confdata['server']
-        c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
-        os.remove(self.COOKIEFILE)
-        
-    def city(self, id):
-	return self.cities.get(id, IkariamCity(id=id, core=self) )
-    
-class IkariamCity:
-    
-    def __init__(self, id, core ):
-        self.core = core
-        self.id = id
-        self.params = {'view':'city','id':id}
-        
-    def sync(self):
-	print "pull datas of the city %s" % self.id
-        xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul"
-
-        xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()"
-        self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0]
-        
+__all__ = ('Ikariam',)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/buildings.py	Sun Nov 02 02:23:13 2008 +0800
@@ -0,0 +1,45 @@
+from lazy.www import c
+from lxml import etree
+from StringIO import StringIO
+
+class position(object):
+    def __init__(self, build_type, city_id, idx, baseurl):
+        self._baseurl = baseurl + '/index.php'
+        self.build_type = build_type
+        self.city_id = city_id
+        self.idx = idx
+        self._params = {'view': 'buildingGround',
+                        'id': city_id,
+                        'position': idx}
+        pass
+
+    def get_page(self):
+        page = c(self._baseurl).get(self._params).get_content()
+        return page
+    pass
+
+class townhall(position):
+    xpath_patterns = {
+        'level': '/div/div/div[@class=\'buildingLevel\']/text()'
+        }
+
+    def __init__(self, city_id, idx, baseurl):
+        super(townhall, self).__init__('townhall', city_id, idx, baseurl)
+        pass
+
+    def _sync(self, page):
+        parser = etree.HTMLParser(encoding='utf8')
+        page_dom = etree.parse(StringIO(page), parser)
+        xpath_building = '/html/body/div/div'
+        for name, ptn in self.xpath_patterns.items():
+            path = xpath_building + ptn
+            value = page_dom.xpath(path)[0]
+            setattr(self, name, value)
+            pass
+        pass
+
+    def sync(self):
+        page = self.get_page()
+        self._sync(page)
+        pass
+    pass
--- a/pyikriam/example.py	Sun Nov 02 02:21:05 2008 +0800
+++ b/pyikriam/example.py	Sun Nov 02 02:23:13 2008 +0800
@@ -1,6 +1,27 @@
-from __init__ import Ikariam
+import sys
+from ikariam import Ikariam
+
+if len(sys.argv) != 2:
+    print >> sys.stderr, 'Usage: %s <city id>' % (sys.argv[0])
+    sys.exit(1)
+    pass
+
+city_id = int(sys.argv[1]) # 117261
 
 i = Ikariam()
-city = i.city(117261)
+city = i.city(city_id)
+
+print "pull datas of the city %s" % (city_id)
 city.sync()
-print 'gold is'+city.gold
+
+print 'gold is ' + city.gold
+print 'inhabitants is ' + city.inhabitants
+print 'wood is ' + city.wood
+print 'wine is ' + city.wine
+print 'marble is ' + city.marble
+print 'crystal is ' + city.crystal
+print 'sulfur is ' + city.sulfur
+print 'positions ' + repr(city.positions)
+
+city.positions[0].sync()
+print 'positions[0] level is ' + str(city.positions[0].level)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/ikariam.py	Sun Nov 02 02:23:13 2008 +0800
@@ -0,0 +1,117 @@
+from lazy.www import c
+from lconf import LoadConfigfile
+import cookielib
+import os
+import urllib2
+import urllib
+from utils import dyna_prog, decorator
+from lxml import etree
+from StringIO import StringIO
+
+class fake_moz(object):
+    __metaclass__ = decorator
+
+    def __init__(self):
+        super(fake_moz, self).__init__()
+        cookie_jar = cookielib.LWPCookieJar()
+        cookie_proc = urllib2.HTTPCookieProcessor(cookie_jar)
+        opener = urllib2.build_opener(cookie_proc)
+        opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.1.12pre) Gecko/20071220 BonEcho/2.0.0.12pre')]
+        fake_moz.set_backend(self, opener)
+        self.cookie_jar = cookie_jar
+        pass
+    pass
+
+
+class Ikariam:
+
+    cities = {}
+    COOKIEFILE = '/tmp/ikariam.lwp'
+
+    def __init__(self):
+        browser = fake_moz()
+        self.browser = browser
+        self._cookie_jar = browser.cookie_jar
+
+        if os.path.isfile(self.COOKIEFILE):
+            self._cookie_jar.load(self.COOKIEFILE)
+            pass
+ 
+        urllib2.install_opener(browser)
+
+	self.confdata=LoadConfigfile().cd
+        self.baseurl='http://'+self.confdata['server']
+
+        self.login()
+        pass
+
+    def login(self):     
+	print "login to %s...." % self.confdata['server']
+        params = {"universe":self.confdata['server'], \
+        "name":self.confdata['user'], \
+        "password":self.confdata['pass']}
+        ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
+        self._cookie_jar.save(self.COOKIEFILE)
+        pass
+
+    def logout(self):
+	print "logut from %s...." % self.confdata['server']
+        c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
+        os.remove(self.COOKIEFILE)
+        pass
+    
+    ##
+    # \note We can cache data with decorator 'dynamic programming'.
+    #
+    @dyna_prog
+    def city(self, id):
+        return IkariamCity(id=id, core=self)
+    pass
+
+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()'
+        }
+    def __init__(self, id, core ):
+        self.core = core
+        self.id = id
+        self.params = {'view':'city','id':id}
+        
+    def sync(self):
+        page = c(self.core.baseurl).get(self.params).get_content()
+        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
+            value = page_dom.xpath(xpath)[0]
+            setattr(self, name, value)
+            pass
+        
+        xpath_mainview = '/html/body/div/div/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)]
+        self.positions = positions
+        pass
+
+    def _mk_position(self, pos_dom, idx):
+        import buildings
+
+        build_type = pos_dom.get('class').split()[-1].lower()
+        if hasattr(buildings, build_type):
+            clz = getattr(buildings, build_type)
+            if issubclass(clz, buildings.position):
+                building = clz(self.id, idx, self.core.baseurl)
+                return building
+            pass
+        return build_type
+    pass
+
--- a/pyikriam/lconf.py	Sun Nov 02 02:21:05 2008 +0800
+++ b/pyikriam/lconf.py	Sun Nov 02 02:23:13 2008 +0800
@@ -1,15 +1,33 @@
-import os,string
+import os, string
+import re
+
+_user_home = os.environ['HOME']
+dfl_profile = os.path.join(_user_home, '.eagleeye.pm')
+_reo_assign = re.compile('^\\$::([_a-zA-Z][_a-zA-Z0-9]+) *= *([\'"][^"]*[\'"]).*$')
+
+def _real_load_conf(conf_o):
+    confs = [_reo_assign.match(line)
+             for line in conf_o
+             if line.startswith('$::')]
+    cd = dict([(mo.group(1), eval(mo.group(2)))
+               for mo in confs if mo])
+    return cd
+
 class LoadConfigfile(object):
-    def __init__(self):
-	profile = os.environ["HOME"]+'/.eagleeye.pm'
-	self.cd={}
-	if os.path.isfile(profile):
-	    print "Loading Config file."
-	    cfile=open(profile,'r')
-	    for line in cfile.xreadlines():
-	    	if line[0:3]=='$::':
-		   con=string.split(line[3:-2])
-		   self.cd[con[0]]=con[2][1:-1]	   
-	else:
-	    print "File don't exist."
+    def __init__(self, profile=dfl_profile):
+        prof_o = open(profile, 'r')
+        self.cd = _real_load_conf(prof_o)
+        pass
+    pass
 
+if __name__ == '__main__':
+    from StringIO import StringIO
+    conf = '''
+$::user = "alsfjsf"; #lsfjslf
+$::server = "fkljalfasf"; # sfjslf
+$::pass = "lsfjslfsf";
+'''
+    conf_o = StringIO(conf)
+    cd = _real_load_conf(conf_o)
+    print cd
+    pass
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/utils.py	Sun Nov 02 02:23:13 2008 +0800
@@ -0,0 +1,80 @@
+## \brief decorator is a class decorate another of another object.
+#
+# Access of attributes/methods that can not be found in an instance
+# of a class of this meta-class, decorator, are delegated to the backend
+# of the instance.
+#
+# A backend object always passed as first parameter when
+# instantiate an instance of a class with decorator as meta-class.
+# For example,
+# \code
+# class foo(object):
+#     __metaclass__ = decorator
+#
+# backend.data = 3
+# obj = foo(backend)
+# print obj.data
+# backend.data = 4
+# print obj.data
+# \endcode
+# it print out 3 and 4 respectively.
+# 
+class decorator(type):
+    def __init__(clz, name, base, dict):
+        super(decorator, clz).__init__(name, base, dict)
+        clz.__getattr__ = decorator.__dele_getattr
+        pass
+
+    @staticmethod
+    def set_backend(obj, backend):
+        obj.__backend = backend
+        pass
+
+    @staticmethod
+    def __dele_getattr(obj, name):
+        return getattr(obj.__backend, name)
+    pass
+
+
+## \brief Decorator to make functions or methods dynamic programming.
+#
+# dyna_prog result of functions or methods with their arguments as key.
+# It supposes result of a function always the same if the same arguments
+# are passed.  It cache result of cached function to avoid really calling
+# function every time.
+class dyna_prog(object):
+    class functor(object):
+        def __init__(self, instance, method):
+            self._method = method
+            self._instance = instance
+            self._cache = {}
+            pass
+
+        def __call__(self, *args):
+            try:
+                return self._cache[args]
+            except KeyError:
+                pass
+            instance = self._instance
+            result = self._method(instance, *args)
+            return self._cache.setdefault(args, result)
+
+        def clear(self):
+            self._cache.clear()
+        pass
+
+    def __init__(self, method):
+        super(dyna_prog, self).__init__()
+        self._method = method
+        self._functors = {}
+        pass
+    
+    def __get__(self, instance, owner):
+        try:
+            return self._functors[instance]
+        except KeyError:
+            pass
+        functor_o = dyna_prog.functor(instance, self._method)
+        return self._functors.setdefault(instance, functor_o)
+    pass
+