1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
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.
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.
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
25 REG_EXPR_TITLE = re.compile(".*<>.*<>.*<>.*<>(.*)")
26 REG_EXPR_ELEM = re.compile( \
27 "(?P<name>.*)<>(?P<mail>.*)<>(?P<date>.*)<>(?P<msg>.*)<>")
29 def get_dat_file_size(bbs, board, thread):
30 """Returns size of dat file"""
31 dat_path = misc.get_thread_dat_path(bbs, board, thread)
32 if not os.path.exists(dat_path):
35 return os.path.getsize(dat_path)
37 def get_dat_line_count(bbs, board, thread):
38 """Returns the number of line of a dat file specified by bbs, board
47 dat_path = misc.get_thread_dat_path(bbs, board, thread)
48 if not os.path.exists(dat_path):
51 f = fileinput.FileInput(dat_path)
55 def do_get_title_from_dat(line):
57 m = REG_EXPR_TITLE.match(line.decode("cp932", "replace"))
62 def get_title_from_dat(bbs, board, thread):
63 """Returns thread title in dat file
71 If failed, return None
73 dat_path = misc.get_thread_dat_path(bbs, board, thread)
74 if not os.path.exists(dat_path):
77 f = open(dat_path, "r")
80 return do_get_title_from_dat(line)
84 def split_line_to_elems(line, func):
85 """Splits a line to elements and invokes func
87 line: represents one res
89 func: is invoked after splitting
90 format of user functon is:
91 def some_func(name, mail, date, msg):
92 where parameters represent corresponding elements of the res
94 m = REG_EXPR_ELEM.match(line)
96 name = m.group("name")
97 mail = m.group("mail")
98 date = m.group("date")
100 func(name, mail, date, msg)
102 def load_dat(bbs, board, thread, func):
103 """Loads entire dat and invokes func per one res
111 func: is invoked per one res
112 format of user function is:
114 where line is raw body of the res
116 dat_path = misc.get_thread_dat_path(bbs, board, thread)
117 if not os.path.exists(dat_path):
120 f = fileinput.FileInput(dat_path)
125 def load_dat_partly(bbs, board, thread, func, resnum):
127 similar to load_dat, but load_dat_partly does not load entire dat.
135 func: invoked per one res
137 resnum: load downward resnum
139 dat_path = misc.get_thread_dat_path(bbs, board, thread)
140 if not os.path.exists(dat_path):
143 f = fileinput.FileInput(dat_path)