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
20 from fileinput import FileInput
23 metadata_namelist = ["title", "lineCount", "lastModified", "etag"]
25 def load_idx(bbs_type):
26 """Loads index file of thread
34 return dictionary which key is in metadata_namelist if idx file exist,
35 otherwise return empty dic
37 idxfile_path = misc.get_thread_idx_path(bbs_type)
38 if not os.path.exists(idxfile_path):
39 return {"title":None,"lineCount":0,"lastModified":None,"etag":None}
42 f = FileInput(idxfile_path)
44 for name in metadata_namelist:
45 if line.startswith(name+"="):
46 value = line[len(name)+1:].rstrip("\n")
47 if name is "lineCount":
49 datadic[name] = int(value)
54 #print name, datadic[name]
58 # if datadic does not have key, insert empty entry.
59 for name in metadata_namelist:
60 if name not in datadic:
61 if name is "lineCount":
67 def save_idx(bbs_type, datadic):
68 """Saves thread metadatas to a index file
76 datadic: dictionary which key is in metadata_namelist
78 no need to save empty or non-existing metadata
81 # create a directory where idx file should belong if does not exist
82 idxfile_path = misc.get_thread_idx_path(bbs_type)
83 basedir = os.path.dirname(idxfile_path)
84 if not os.path.isdir(basedir):
87 f = open(idxfile_path, "w")
88 for name in metadata_namelist:
90 if name is "lineCount":
92 f.write(name + "=" + str(datadic[name]) + "\n")
95 f.write(name + "=" + datadic[name] + "\n")