Mercurial > eagle-eye
annotate pyikriam/sync_utils.py @ 246:60c4b4b78a01
code clean
author | "Hisn Yi, Chen <ossug.hychen@gmail.com>" |
---|---|
date | Mon, 01 Dec 2008 00:25:07 +0800 |
parents | 015ac84d038c |
children |
rev | line source |
---|---|
177
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
1 ## \file |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
2 # \brief Sync information of objects with DOM trees of respective pages. |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 # |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
4 |
188
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
5 import re as _re |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
6 |
177
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
7 def sync_tagclass(obj, patterns, page_dom): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
8 xpath_building = '/html/body/descendant::*[@class=\'%s\']/text()' |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
9 for name, clzname in patterns.items(): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
10 path = xpath_building % (clzname) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
11 value = float(page_dom.xpath(path)[0]) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 setattr(obj, name, value) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
15 |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
16 def sync_tagvalue(obj, patterns, page_dom): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
17 xpath_value = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'value\']/text()' |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
18 for name, clzname in patterns.items(): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
19 path = xpath_value % (clzname) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
20 value = float(page_dom.xpath(path)[0]) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
21 setattr(obj, name, value) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
22 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
23 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
24 |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
25 def sync_tagcount(obj, patterns, page_dom): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
26 xpath_count = '/html/body/descendant::*[starts-with(@class,\'%s\')]/descendant::*[@class=\'count\']/text()' |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
27 for name, clzname in patterns.items(): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
28 path = xpath_count % (clzname) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
29 value = int(page_dom.xpath(path)[0]) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
30 setattr(obj, name, value) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
31 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
32 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
33 |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
34 def sync_tagclass_start_appear(obj, patterns, page_dom): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
35 xpath_appear = '/html/body/descendant::*[starts-with(@class,\'%s\')]' |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
36 for name, clzname in patterns.items(): |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
37 path = xpath_appear % (clzname) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
38 cnt = len(page_dom.xpath(path)) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
39 if cnt != 0: |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
40 setattr(obj, name, True) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
41 else: |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
42 setattr(obj, name, False) |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
43 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
44 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
45 pass |
6adad3bcca78
Refactory to functions for sychronizing object attributes with pages.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
46 |
188
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
47 _reo_tv = _re.compile(u'(([0-9]+)\u6642)? ?(([0-9]+)\u5206)? ?(([0-9]+)\u79d2)?') |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
48 ## \brief Translate timeval in Chinese text format to integer seconds. |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
49 # |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
50 def ikariam_zh_timeval(tv_str): |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
51 tmo = _reo_tv.match(tv_str) |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
52 if not tmo: |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
53 raise SyntaxError, \ |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
54 '%s is an invalid time interval string' % (repr(tv_str)) |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
55 tv = 0 |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
56 |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
57 value = tmo.group(2) # hour |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
58 if value: |
194
015ac84d038c
Fix hour-2-seconds translation.
Thinker K.F. Li <thinker@branda.to>
parents:
188
diff
changeset
|
59 tv = tv + int(value) * 3600 |
188
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
60 pass |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
61 |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
62 value = tmo.group(4) # minute |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
63 if value: |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
64 tv = tv + int(value) * 60 |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
65 pass |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
66 |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
67 value = tmo.group(6) # second |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
68 if value: |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
69 tv = tv + int(value) |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
70 pass |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
71 |
bf4ddf5bffb9
Extracts information about resources required to upgrade a building.
Thinker K.F. Li <thinker@branda.to>
parents:
177
diff
changeset
|
72 return tv |