OSDN Git Service

step one, change file name.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / 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 fileinput import FileInput
21 from misc import get_thread_idx_path
22
23 metadata_namelist = ["title", "lineCount", "lastModified", "etag"]
24
25 def load_idx(bbs, board, thread):
26     """Loads index file of thread
27
28     bbs: bbs id
29
30     board: board id
31
32     thread: thread id
33
34     return dictionary which key is in metadata_namelist if idx file exist,
35     otherwise return empty dic
36     """
37     idxfile_path = get_thread_idx_path(bbs, board, thread)
38     if not os.path.exists(idxfile_path):
39         return {"title":None,"lineCount":0,"lastModified":None,"etag":None}
40
41     datadic = {}
42     f = FileInput(idxfile_path)
43     for line in f:
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     f.close()
57
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":
62                 datadic[name] = 0
63             else:
64                 datadic[name] = ""
65     return datadic
66
67 def save_idx(bbs, board, thread, datadic):
68     """Saves thread metadatas to a index file
69
70     bbs: bbs id
71
72     board: board id
73
74     thread: thread id
75
76     datadic: dictionary which key is in metadata_namelist
77
78     no need to save empty or non-existing metadata
79     """
80
81     # create a directory where idx file should belong if does not exist
82     idxfile_path = get_thread_idx_path(bbs, board, thread)
83     basedir = os.path.dirname(idxfile_path)
84     if not os.path.isdir(basedir):
85         os.makedirs(basedir)
86
87     f = open(idxfile_path, "w")
88     for name in metadata_namelist:
89         if name in datadic:
90             if name is "lineCount":
91                 if datadic[name] > 0:
92                     f.write(name + "=" + str(datadic[name]) + "\n")
93             else:
94                 if datadic[name]:
95                     f.write(name + "=" + datadic[name] + "\n")
96     f.close()