OSDN Git Service

Replace PangoLayout with ResLayout.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / cachefile.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import itertools
19
20 import misc
21
22 metadata_namelist = ("title", "lineCount", "lastModified", "idxlastModified")
23 recorded_data = ("id", "title", "lineCount", "lastModified", "idxlastModified")
24 int_data = ("lineCount", "idxlastModified")
25
26 def _empty_generator():
27     for name in metadata_namelist:
28         if name in int_data:
29             yield name, 0
30         else:
31             yield name, ""
32
33 default_dict = dict([pair for pair in _empty_generator()])
34
35 def _tabbed_to_dict(tabbed):
36
37     def adjust_type(key, value):
38         if key in int_data:
39             try:
40                 value = int(value)
41             except:
42                 value = 0
43         return key, value
44
45     iterable = misc.tabbed_to_dict_generator(tabbed)
46     iterable = misc.unpack_ifilter(lambda k, v: k in recorded_data, iterable)
47     iterable = itertools.starmap(adjust_type, iterable)
48
49     dic = default_dict.copy()
50     for key, value in iterable:
51         dic[key] = value
52
53     return dic
54
55 def _dict_to_tabbed(dic):
56
57     def validate(name, value):
58         if name in int_data and value > 0:
59             return True
60         elif name not in int_data and value:
61             return True
62         return False
63         
64     iterable = dic.iteritems()
65
66     # save metadata only if its name exists in metadata_namelist
67     iterable = misc.unpack_ifilter(lambda n,v: n in metadata_namelist,iterable)
68
69     # no need to save an empty or invalid field
70     iterable = misc.unpack_ifilter(validate, iterable)
71
72     iterable = itertools.starmap(lambda n,v: "%s=%s\t" % (n, str(v)), iterable)
73
74     line = ""
75     for tabbed in iterable:
76         line += tabbed
77     return line
78
79 def do_formatted_to_dict(formatted):
80     dic = _tabbed_to_dict(formatted)
81     if "id" in dic and dic["id"]:
82         return dic
83     
84 def formatted_to_dict(iterable):
85     iterable = itertools.imap(do_formatted_to_dict, iterable)
86     iterable = itertools.ifilter(None, iterable)
87     return iterable
88
89 def do_dict_to_formatted(thread_id, dic):
90     return "id=%s\t%s\n" % (thread_id, _dict_to_tabbed(dic))
91
92 def dict_to_formatted(iterable):
93     return itertools.starmap(do_dict_to_formatted, iterable)