OSDN Git Service

Add README. Decide name, Fukui no Namari.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / cachefile.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
20 import misc
21
22 metadata_namelist = ["title", "lineCount", "lastModified", "idxlastModified"]
23
24 def load_cache(bbs, board, func):
25     """ Loads metadata from .cache file
26
27     loads metadata and invokes handler per one thread
28
29     bbs: bbs id
30
31     board: board id
32
33     func: invoked at loading one thread's metadata
34     should be of the form:
35     def handler_example(id, metadata_dic):
36     where the first argument is thread id and
37     the second metadata dic which key is in metadata_namelist
38     """
39
40     # nothing to do if .cache file does not exist
41     cachefile_path = misc.get_board_cache_path(bbs, board)
42     if not os.path.exists(cachefile_path):
43         return
44
45     f = open(cachefile_path, "r")
46     try:
47         line = f.readline()
48         while line:
49             metadatas = line.split("\t")
50             dic = {}
51             for field in metadatas:
52                 for name in ["id", "title", "lineCount",
53                              "lastModified", "idxlastModified"]:
54                     if field.startswith(name+"="):
55                         value = field[len(name)+1:]
56                         if name is "lineCount" or name is "idxlastModified":
57                             try:
58                                 dic[name] = int(value)
59                             except:
60                                 dic[name] = 0
61                         else:
62                             dic[name] = value
63             # invoke func only if id exists
64             if "id" in dic and dic["id"]:
65                 # if metadata in metadata_namelist does not exist,
66                 # set empty str or 0
67                 for name in metadata_namelist:
68                     if name not in dic:
69                         if name is "lineCount" or name is "idxlastModified":
70                             dic[name] = 0
71                         else:
72                             dic[name] = ""
73                 func(dic["id"], dic)
74             line = f.readline()
75     finally:
76         f.close()
77
78 def save_cache(bbs, board, dic):
79     """ Saves metadata list to .cache file
80
81     bbs: bbs id
82
83     board: board id
84
85     dic: dictionary of thread id and metadata dictionary
86     which key is in metadata_namelist
87     """
88     # nothing to do if dic is empty
89     if not dic:
90         return
91
92     cachefile_path = misc.get_board_cache_path(bbs, board)
93
94     # create a directroy where .cache file is if not exists
95     basedir = os.path.dirname(cachefile_path)
96     if not os.path.isdir(basedir):
97         os.makedirs(basedir)
98
99     f = open(cachefile_path, "w")
100     for id, metadata_list in dic.iteritems():
101         # save only if id is not empty
102         if id:
103             line = "id=" + id + "\t"
104             # save metadata only if exists in metadata_namelist
105             for name in metadata_namelist:
106                 # save metadata only if exists in metadata_list
107                 # and no need to save an empty or invalid field
108                 if name in metadata_list:
109                     if name is "lineCount" or name is "idxlastModified":
110                         if metadata_list[name] > 0:
111                             line += name + "=" + str(metadata_list[name]) + "\t"
112                     else:
113                         if metadata_list[name]:
114                             line += name + "=" + metadata_list[name] + "\t"
115             line += "\n"
116             f.write(line)
117     f.close()