OSDN Git Service

Move ThreadInvoker and FileWrap to misc.py
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / board_data.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 threading
19 import gobject
20 import os.path
21 import glob
22
23 import subjecttxtfile
24 import cachefile
25 import idxfile
26 import misc
27
28 BOARD_DATA_INVALID_VALUE = 0
29
30 def merge_new_thread(oldlist, newlist, id, title, res):
31     num = len(newlist) + 1
32     if id in oldlist:
33         item = oldlist.pop(id)
34         item["num"] = num
35         item["title"] = title
36         item["res"] = res
37         newlist[id] = item
38     else:
39         newlist[id] = {"num": num, "title": title,
40                         "res": res, "lineCount": BOARD_DATA_INVALID_VALUE,
41                         "lastModified": ""}
42
43 def merge_cached_thread(oldlist, newlist):
44     for id, value in oldlist.iteritems():
45         if value["lineCount"] != BOARD_DATA_INVALID_VALUE:
46             value["num"] = BOARD_DATA_INVALID_VALUE
47             value["res"] = BOARD_DATA_INVALID_VALUE
48             newlist[id] = value
49
50 def merge_local_subjecttxt(bbs, board, datalist):
51     oldlist = datalist.copy()
52     datalist.clear()
53
54     h = lambda id,title,res: merge_new_thread(oldlist,datalist,id,title,res)
55     subjecttxtfile.load_subjecttxt(bbs, board, h)
56     merge_cached_thread(oldlist, datalist)
57
58 def merge_subjecttxt(subjecttxt, datalist):
59     oldlist = datalist.copy()
60     datalist.clear()
61
62     h = lambda id,title,res: merge_new_thread(oldlist,datalist,id,title,res)
63     subjecttxtfile.analyze_subjecttxt(subjecttxt, h)
64     merge_cached_thread(oldlist, datalist)
65
66 def load_idxfiles(bbs, board):
67     datalist = {}
68
69     def on_load_record(id, metadata_dic):
70         idxfile_path = misc.get_thread_idx_path(bbs, board, id)
71         if os.path.exists(idxfile_path):
72             datalist[id] = metadata_dic
73
74     print "load_cache"
75     cachefile.load_cache(bbs, board, on_load_record)
76     print "load_idx"
77     load_modified_idxfiles(bbs, board, datalist)
78     print "save_cache"
79     cachefile.save_cache(bbs, board, datalist)
80
81     return datalist
82
83 def load_modified_idxfiles(bbs, board, datalist):
84     basedir = misc.get_thread_idx_dir_path(bbs, board)
85     if os.path.isdir(basedir):
86         for idxfile_path in glob.glob(os.path.join(basedir, "*.idx")):
87             thread_id, ext = os.path.splitext(os.path.basename(idxfile_path))
88             idxlastModified = os.path.getmtime(idxfile_path)
89             if thread_id not in datalist:
90                 print "new"
91                 dic = idxfile.load_idx(bbs, board, thread_id)
92                 #dic.pop("etag")
93                 dic["idxlastModified"] = idxlastModified
94                 datalist[thread_id] = dic
95             elif idxlastModified > datalist[thread_id]["idxlastModified"]:
96                 print "modified"
97                 datalist[thread_id]["idxlastModified"] = idxlastModified
98                 dic = idxfile.load_idx(bbs, board, thread_id)
99                 for name in idxfile.metadata_namelist:
100                     datalist[thread_id][name] = dic[name]
101
102
103 class LoadLocal(threading.Thread):
104     def __init__(self, bbs, board, on_end):
105         super(LoadLocal, self).__init__()
106         self.bbs = bbs
107         self.board = board
108         self.on_end = on_end
109
110     def run(self):
111         datalist = load_idxfiles(self.bbs, self.board)
112         merge_local_subjecttxt(self.bbs, self.board, datalist)
113         lastmod = subjecttxtfile.load_board_idx(self.bbs, self.board)
114         gobject.idle_add(self.on_end, datalist, lastmod)
115
116
117 class GetRemote(threading.Thread):
118     def __init__(self, bbs, board, uri, on_end):
119         super(GetRemote, self).__init__()
120         self.bbs = bbs
121         self.board = board
122         self.uri = uri
123         self.on_end = on_end
124
125     def run(self):
126         print "start get subject.txt"
127         subjecttxt, lastmod = subjecttxtfile.get_subjecttxt(
128             self.bbs, self.board, self.uri)
129         datalist = load_idxfiles(self.bbs, self.board)
130         merge_subjecttxt(subjecttxt, datalist)
131         gobject.idle_add(self.on_end, datalist, lastmod)