view pyikriam/sync_utils.py @ 177:6adad3bcca78

Refactory to functions for sychronizing object attributes with pages. - We add more building types. - All of them need to sync attributes with content of Ikariam's online page. - These functions are refactoried to functions.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 02 Nov 2008 15:14:51 +0800
parents
children bf4ddf5bffb9
line wrap: on
line source

## \file
# \brief Sync information of objects with DOM trees of respective pages.
#

def sync_tagclass(obj, patterns, page_dom):
    xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()'
    for name, clzname in patterns.items():
        path = xpath_building % (clzname)
        value = float(page_dom.xpath(path)[0])
        setattr(obj, name, value)
        pass
    pass

def sync_tagvalue(obj, patterns, page_dom):
    xpath_value = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'value\']/text()'
    for name, clzname in patterns.items():
        path = xpath_value % (clzname)
        value = float(page_dom.xpath(path)[0])
        setattr(obj, name, value)
        pass
    pass

def sync_tagcount(obj, patterns, page_dom):
    xpath_count = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'count\']/text()'
    for name, clzname in patterns.items():
        path = xpath_count % (clzname)
        value = int(page_dom.xpath(path)[0])
        setattr(obj, name, value)
        pass
    pass

def sync_tagclass_start_appear(obj, patterns, page_dom):
    xpath_appear = '/html/body/descendant::*[starts-with(@class,\'%s\')]'
    for name, clzname in patterns.items():
        path = xpath_appear % (clzname)
        cnt = len(page_dom.xpath(path))
        if cnt != 0:
            setattr(obj, name, True)
        else:
            setattr(obj, name, False)
            pass
        pass
    pass