import re
import os.path
import codecs
+import fileinput
import misc
REG_EXPR_ELEM = re.compile( \
"(?P<name>.*)<>(?P<mail>.*)<>(?P<date>.*)<>(?P<msg>.*)<>")
+def get_dat_file_size(bbs, board, thread):
+ """Returns size of dat file"""
+ dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ if not os.path.exists(dat_path):
+ return 0
+
+ return os.path.getsize(dat_path)
+
+def get_dat_line_count(bbs, board, thread):
+ """Returns the number of line of a dat file specified by bbs, board
+ and thread
+
+ bbs: bbs id
+
+ board: board id
+
+ thread: thread id
+ """
+ dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ if not os.path.exists(dat_path):
+ return 0
+
+ f = fileinput.FileInput(dat_path)
+ for l in f: -1
+ return f.filelineno()
+
def get_title_from_dat(bbs, board, thread):
"""Returns thread title in dat file
func: is invoked per one res
format of user function is:
- def some_func(num, line):
- where num is the number of the res and line is raw body of the res
+ def some_func(line):
+ where line is raw body of the res
"""
dat_path = misc.get_thread_dat_path(bbs, board, thread)
if not os.path.exists(dat_path):
return
- f = open(dat_path, "r")
- num = 1
- try:
- line = f.readline()
- while line:
- line = line.decode("cp932", "replace")
- func(num, line)
- line = f.readline()
- num += 1
- finally:
- f.close()
+ f = fileinput.FileInput(dat_path)
+ for line in f:
+ func(line)
+ f.close()
+
+def load_dat_partly(bbs, board, thread, func, resnum):
+ """Loads dat partly
+ similar to load_dat, but load_dat_partly does not load entire dat.
+
+ bbs: bbs id
+
+ board: board id
+
+ thread: thread id
+
+ func: invoked per one res
+
+ resnum: load downward resnum
+ """
+ dat_path = misc.get_thread_dat_path(bbs, board, thread)
+ if not os.path.exists(dat_path):
+ return
+
+ f = fileinput.FileInput(dat_path)
+ for line in f:
+ num = f.filelineno()
+ if num >= resnum:
+ func(line)
+ f.close()