X-Git-Url: http://git.sourceforge.jp/view?p=fukui-no-namari%2Ffukui-no-namari.git;a=blobdiff_plain;f=src%2FHage1%2Fdatfile.py;h=4c411737b6cc10d8e2926a1dc254b49ec672472c;hp=e4bf2eb254266663797f7f3f361ce84013b18951;hb=8acb0434a170be0ffbd8ebd93538933c4006dfde;hpb=c834573324d9b1441489deae16ba309665103d7c diff --git a/src/Hage1/datfile.py b/src/Hage1/datfile.py index e4bf2eb..4c41173 100644 --- a/src/Hage1/datfile.py +++ b/src/Hage1/datfile.py @@ -18,6 +18,7 @@ import re import os.path import codecs +import fileinput import misc @@ -25,6 +26,32 @@ REG_EXPR_TITLE = re.compile(".*<>.*<>.*<>.*<>(.*)") REG_EXPR_ELEM = re.compile( \ "(?P.*)<>(?P.*)<>(?P.*)<>(?P.*)<>") +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 @@ -79,21 +106,39 @@ def load_dat(bbs, board, thread, func): 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()