OSDN Git Service

Add one function for thread and modified some function for thread. datfile.load_dat...
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 15 Aug 2006 02:42:36 +0000 (11:42 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 15 Aug 2006 02:42:36 +0000 (11:42 +0900)
src/Hage1/datfile.py
src/Hage1/misc.py

index e4bf2eb..4c41173 100644 (file)
@@ -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<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
 
@@ -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()
index ab8269e..d8375c9 100644 (file)
@@ -34,6 +34,15 @@ def get_board_base_url(bbs, board):
 
     return "http://" + brdlist.get(bbs, board, "host") + "/" + board + "/"
 
+def get_thread_dat_url(bbs, board, thread):
+    """Returns thread dat url"""
+
+    # if parameter is empty, raise ValueError
+    if not bbs or not board or not thread:
+        raise ValueError, "parameter must not be empty"
+
+    return get_board_base_url(bbs, board) + "dat/" + thread + ".dat"
+
 def get_thread_dat_path(bbs, board, thread):
     """Returns thread dat file path