OSDN Git Service

Multithreads are abandoned. Alternatly, The asyncore substitutes.(#16776)
[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 import itertools
22
23 import misc
24
25 metadata_namelist = ("title", "lineCount", "lastModified", "etag")
26 int_data = ("lineCount")
27
28 def _empty_generator():
29     for name in metadata_namelist:
30         if name in int_data:
31             yield name, 0
32         else:
33             yield name, ""
34
35 default_dict = dict([pair for pair in _empty_generator()])
36
37 def load_idx(bbs_type):
38     """Loads index file of thread
39
40     return dictionary which key is in metadata_namelist if idx file exist,
41     otherwise return empty dic
42     """
43     idxfile_path = misc.get_thread_idx_path(bbs_type)
44     datadic = default_dict.copy()
45
46     def adjust_type(name, value):
47         if name in int_data:
48             try:
49                 value = int(value)
50             except:
51                 value = 0
52         return name, value
53
54     try:
55         iterable = itertools.imap(lambda l: l.rstrip(), file(idxfile_path))
56         iterable = itertools.imap(misc.split_key_and_value, iterable)
57         iterable = itertools.ifilter(None, iterable)
58         iterable = misc.unpack_ifilter(
59             lambda name, value: name in metadata_namelist, iterable)
60         iterable = itertools.starmap(adjust_type, iterable)
61         for name, value in iterable: datadic[name] = value
62     except IOError:
63         pass
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     def validate(name, value):
76         if name in int_data and value > 0:
77             return True
78         elif name not in int_data and value:
79             return True
80         return False
81
82     iterable = datadic.iteritems()
83
84     # save metadata only if its name exists in metadata_namelist
85     iterable = misc.unpack_ifilter(
86         lambda name, value: name in metadata_namelist, iterable)
87
88     # no need to save an empty or invalid field
89     iterable = misc.unpack_ifilter(validate, iterable)
90
91     # format
92     iterable = itertools.starmap(lambda n,v: "%s=%s\n" % (n, str(v)), iterable)
93
94     # write
95     idx_file = misc.FileWrap(misc.get_thread_idx_path(bbs_type), "w")
96     try:
97         idx_file.writelines(iterable)
98     except IOError:
99         traceback.print_exc()