OSDN Git Service

31c128737e66aac9d5b12319809d65a864ac5460
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / idxfile.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 os
19 import os.path
20 from misc import get_thread_idx_path
21
22 metadata_namelist = ["title", "lineCount", "lastModified", "etag"]
23
24 def load_idx(bbs, board, thread):
25     """Loads index file of thread
26
27     bbs: bbs id
28
29     board: board id
30
31     thread: thread id
32
33     return dictionary which key is in metadata_namelist if idx file exist,
34     otherwise return empty dic
35     """
36     idxfile_path = get_thread_idx_path(bbs, board, thread)
37     if not os.path.exists(idxfile_path):
38         return {"title":None,"lineCount":0,"lastModified":None,"etag":None}
39
40     f = open(idxfile_path, "r")
41     datadic = {}
42     line = f.readline()
43     while line:
44         for name in metadata_namelist:
45             if line.startswith(name+"="):
46                 value = line[len(name)+1:].rstrip("\n")
47                 if name is "lineCount":
48                     try:
49                         datadic[name] = int(value)
50                     except:
51                         datadic[name] = 0
52                 else:
53                     datadic[name] = value
54                 #print name, datadic[name]
55                 break;
56         line = f.readline()
57     f.close()
58
59     # if datadic does not have key, insert empty entry.
60     for name in metadata_namelist:
61         if name not in datadic:
62             if name is "lineCount":
63                 datadic[name] = 0
64             else:
65                 datadic[name] = ""
66     return datadic
67
68 def save_idx(bbs, board, thread, datadic):
69     """Saves thread metadatas to a index file
70
71     bbs: bbs id
72
73     board: board id
74
75     thread: thread id
76
77     datadic: dictionary which key is in metadata_namelist
78
79     no need to save empty or non-existing metadata
80     """
81
82     # create a directory where idx file should belong if does not exist
83     idxfile_path = get_thread_idx_path(bbs, board, thread)
84     basedir = os.path.dirname(idxfile_path)
85     if not os.path.isdir(basedir):
86         os.makedirs(basedir)
87
88     f = open(idxfile_path, "w")
89     for name in metadata_namelist:
90         if name in datadic:
91             if name is "lineCount":
92                 if datadic[name] > 0:
93                     f.write(name + "=" + str(datadic[name]) + "\n")
94             else:
95                 if datadic[name]:
96                     f.write(name + "=" + datadic[name] + "\n")
97     f.close()