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
24 metadata_namelist = ("title", "lineCount", "lastModified", "etag")
25 int_data = ("lineCount")
27 def _empty_generator():
28 for name in metadata_namelist:
34 default_dict = dict([pair for pair in _empty_generator()])
36 def load_idx(bbs_type):
37 """Loads index file of thread
39 return dictionary which key is in metadata_namelist if idx file exist,
40 otherwise return empty dic
42 idxfile_path = misc.get_thread_idx_path(bbs_type)
43 datadic = default_dict.copy()
46 for line in file(idxfile_path):
47 key_equal_value = line.rstrip()
49 index = key_equal_value.index("=")
53 key = key_equal_value[:index]
54 if key in metadata_namelist:
55 value = key_equal_value[index+1:]
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 # create a directory where idx file should belong if does not exist
76 idxfile_path = misc.get_thread_idx_path(bbs_type)
77 basedir = os.path.dirname(idxfile_path)
78 if not os.path.isdir(basedir):
81 f = file(idxfile_path, "w")
82 for name, value in datadic.iteritems():
84 # save metadata only if its name exists in metadata_namelist
85 if name not in metadata_namelist:
88 # no need to save an empty or invalid field
89 if name in int_data and value > 0:
90 f.write(name + "=" + str(value) + "\n")
91 elif name not in int_data and value:
92 f.write(name + "=" + value + "\n")