view pyikriam/sync_utils.py @ 208:a2bb38589172

refined debug file name
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 03 Nov 2008 20:07:05 +0800
parents 015ac84d038c
children
line wrap: on
line source

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

import re as _re

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

_reo_tv = _re.compile(u'(([0-9]+)\u6642)? ?(([0-9]+)\u5206)? ?(([0-9]+)\u79d2)?')
## \brief Translate timeval in Chinese text format to integer seconds.
#
def ikariam_zh_timeval(tv_str):
    tmo = _reo_tv.match(tv_str)
    if not tmo:
        raise SyntaxError, \
            '%s is an invalid time interval string' % (repr(tv_str))
    tv = 0

    value = tmo.group(2)        # hour
    if value:
        tv = tv + int(value) * 3600
        pass
    
    value = tmo.group(4)        # minute
    if value:
        tv = tv + int(value) * 60
        pass
    
    value = tmo.group(6)        # second
    if value:
        tv = tv + int(value)
        pass

    return tv