OSDN Git Service

Rearrange. changes are individual, do not effect other. improve performance.
[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 import traceback
21
22 import misc
23
24 metadata_namelist = ("title", "lineCount", "lastModified", "etag")
25 int_data = ("lineCount")
26
27 def _empty_generator():
28     for name in metadata_namelist:
29         if name in int_data:
30             yield name, 0
31         else:
32             yield name, ""
33
34 default_dict = dict([pair for pair in _empty_generator()])
35
36 def load_idx(bbs_type):
37     """Loads index file of thread
38
39     return dictionary which key is in metadata_namelist if idx file exist,
40     otherwise return empty dic
41     """
42     idxfile_path = misc.get_thread_idx_path(bbs_type)
43     datadic = default_dict.copy()
44
45     try:
46         for line in file(idxfile_path):
47             key_equal_value = line.rstrip()
48             try:
49                 index = key_equal_value.index("=")
50             except ValueError:
51                 pass
52             else:
53                 key = key_equal_value[:index]
54                 if key in metadata_namelist:
55                     value = key_equal_value[index+1:]
56                     if key in int_data:
57                         try:
58                             value = int(value)
59                         except:
60                             value = 0
61                     datadic[key] = value
62     except IOError:
63         traceback.print_exc()
64
65     return datadic
66
67 def save_idx(bbs_type, datadic):
68     """Saves thread metadatas to a index file
69
70     datadic: dictionary which key is in metadata_namelist
71
72     no need to save empty or non-existing metadata
73     """
74
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):
79         os.makedirs(basedir)
80
81     f = file(idxfile_path, "w")
82     for name, value in datadic.iteritems():
83         
84         # save metadata only if its name exists in metadata_namelist
85         if name not in metadata_namelist:
86             continue
87
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")
93
94     f.close()