changeset 166:f7fd2738b9b4

Move Ikariam out of __init__.py to ikariam.py.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 00:59:00 +0800
parents 4f06f72365fa
children 6ab01f709650
files pyikriam/__init__.py pyikriam/example.py pyikriam/ikariam.py
diffstat 3 files changed, 107 insertions(+), 104 deletions(-) [+]
line wrap: on
line diff
--- a/pyikriam/__init__.py	Sun Nov 02 00:31:03 2008 +0800
+++ b/pyikriam/__init__.py	Sun Nov 02 00:59:00 2008 +0800
@@ -1,104 +1,3 @@
-from lazy.www import c
-from lconf import LoadConfigfile
-import cookielib
-import os
-import urllib2
-import urllib
-from utils import dyna_prog, decorator
-
-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
+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._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):
-        from lxml import etree
-        from StringIO import StringIO
-
-        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 = [pos_dom.get('class').split()[-1] for pos_dom in pos_doms]
-        self.positions = positions
-        pass
-    pass
+__all__ = ('Ikariam')
--- a/pyikriam/example.py	Sun Nov 02 00:31:03 2008 +0800
+++ b/pyikriam/example.py	Sun Nov 02 00:59:00 2008 +0800
@@ -1,5 +1,5 @@
 import sys
-from __init__ import Ikariam
+from ikariam import Ikariam
 
 if len(sys.argv) != 2:
     print >> sys.stderr, 'Usage: %s <city id>' % (sys.argv[0])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/ikariam.py	Sun Nov 02 00:59:00 2008 +0800
@@ -0,0 +1,104 @@
+from lazy.www import c
+from lconf import LoadConfigfile
+import cookielib
+import os
+import urllib2
+import urllib
+from utils import dyna_prog, decorator
+
+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):
+        from lxml import etree
+        from StringIO import StringIO
+
+        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 = [pos_dom.get('class').split()[-1] for pos_dom in pos_doms]
+        self.positions = positions
+        pass
+    pass