OSDN Git Service

Do not display 0
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / datfile.py
index e4bf2eb..4c41173 100644 (file)
@@ -18,6 +18,7 @@
 import re
 import os.path
 import codecs
 import re
 import os.path
 import codecs
+import fileinput
 
 import misc
 
 
 import misc
 
@@ -25,6 +26,32 @@ REG_EXPR_TITLE = re.compile(".*<>.*<>.*<>.*<>(.*)")
 REG_EXPR_ELEM = re.compile( \
     "(?P<name>.*)<>(?P<mail>.*)<>(?P<date>.*)<>(?P<msg>.*)<>")
 
 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
 
 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:
 
     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
 
     """
     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()