1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
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.
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.
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
25 metadata_namelist = ("title", "lineCount", "lastModified", "etag")
26 int_data = ("lineCount")
28 def _empty_generator():
29 for name in metadata_namelist:
35 default_dict = dict([pair for pair in _empty_generator()])
37 def load_idx(bbs_type):
38 """Loads index file of thread
40 return dictionary which key is in metadata_namelist if idx file exist,
41 otherwise return empty dic
43 idxfile_path = misc.get_thread_idx_path(bbs_type)
44 datadic = default_dict.copy()
46 def adjust_type(name, value):
55 iterable = itertools.imap(lambda l: l.rstrip(), file(idxfile_path))
56 iterable = itertools.imap(misc.split_key_and_value, iterable)
57 iterable = itertools.ifilter(None, iterable)
58 iterable = misc.unpack_ifilter(
59 lambda name, value: name in metadata_namelist, iterable)
60 iterable = itertools.starmap(adjust_type, iterable)
61 for name, value in iterable: datadic[name] = value
67 def save_idx(bbs_type, datadic):
68 """Saves thread metadatas to a index file
70 datadic: dictionary which key is in metadata_namelist
72 no need to save empty or non-existing metadata
75 def validate(name, value):
76 if name in int_data and value > 0:
78 elif name not in int_data and value:
82 iterable = datadic.iteritems()
84 # save metadata only if its name exists in metadata_namelist
85 iterable = misc.unpack_ifilter(
86 lambda name, value: name in metadata_namelist, iterable)
88 # no need to save an empty or invalid field
89 iterable = misc.unpack_ifilter(validate, iterable)
92 iterable = itertools.starmap(lambda n,v: "%s=%s\n" % (n, str(v)), iterable)
95 idx_file = misc.FileWrap(misc.get_thread_idx_path(bbs_type), "w")
97 idx_file.writelines(iterable)