OSDN Git Service

Add average column
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Thu, 17 Aug 2006 10:20:10 +0000 (19:20 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Thu, 17 Aug 2006 10:20:10 +0000 (19:20 +0900)
src/Hage1/board_data.py
src/Hage1/board_window.py
src/Hage1/misc.py
src/Hage1/subjecttxtfile.py

index cbfb4c1..cb24ce8 100644 (file)
@@ -110,7 +110,8 @@ class LoadLocal(threading.Thread):
     def run(self):
         datalist = load_idxfiles(self.bbs, self.board)
         merge_local_subjecttxt(self.bbs, self.board, datalist)
-        gobject.idle_add(self.on_end, datalist)
+        lastmod = subjecttxtfile.load_board_idx(self.bbs, self.board)
+        gobject.idle_add(self.on_end, datalist, lastmod)
 
 
 class GetRemote(threading.Thread):
@@ -122,7 +123,8 @@ class GetRemote(threading.Thread):
 
     def run(self):
         print "start get subject.txt"
-        subjecttxt = subjecttxtfile.get_subjecttxt(self.bbs, self.board)
+        subjecttxt, lastmod = \
+                    subjecttxtfile.get_subjecttxt(self.bbs, self.board)
         datalist = load_idxfiles(self.bbs, self.board)
         merge_subjecttxt(subjecttxt, datalist)
-        gobject.idle_add(self.on_end, datalist)
+        gobject.idle_add(self.on_end, datalist, lastmod)
index d67b8d3..bbe380d 100644 (file)
@@ -24,6 +24,7 @@ import time
 
 import board_data
 import thread_window
+import misc
 
 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                          "..", "data")
@@ -130,13 +131,27 @@ class WinWrap:
                 self.popupmenu.popup(None, None, None, event.button, time)
             return 1
 
-    def update_datastore(self, datalist):
+    def update_datastore(self, datalist, lastmod):
         print "reflesh datastore"
 
+        try:
+            lastmod = misc.httpdate_to_secs(lastmod)
+        except:
+            lastmod = 0
+
         time_start = time.time()
         list_list = []
         for id, dic in datalist.iteritems():
             dic["id"] = id
+
+            # average
+            if lastmod == 0 or dic["num"] == 0:
+                dic["average"] = 0
+            else:
+                res = dic["res"]
+                start = int(id)
+                dic["average"] = int(res * 60 * 60 * 24 / (lastmod - start))
+
             list_list.append(dic)
 
         model = self.treeview.get_model()
@@ -155,8 +170,9 @@ class WinWrap:
 
 
 class ThreadListModel(gtk.GenericTreeModel):
-    column_types = (str, int, str, int, int, str)
-    column_names = ["id", "num", "title", "res", "lineCount", "lastModified"]
+    column_types = (str, int, str, int, int, str, int)
+    column_names = ["id", "num", "title", "res", "lineCount",
+                    "lastModified", "average"]
 
     def __init__(self, list, sort_column_name="num", sort_reverse=False):
         gtk.GenericTreeModel.__init__(self)
index d8375c9..536f089 100644 (file)
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 import os.path
+import re
+import time
 
 import config
 import brdlist
 
+REG_EXPR_HTTPDATE = re.compile("((?:Mon)|(?:Tue)|(?:Wed)|(?:Thu)|(?:Fri)|(?:Sat)|(?:Sun)), (\d{2}) ((?:Jan)|(?:Feb)|(?:Mar)|(?:Apr)|(?:May)|(?:Jun)|(?:Jul)|(?:Aug)|(?:Sep)|(?:Oct)|(?:Nov)|(?:Dec)) (\d{4}) (\d{2}):(\d{2}):(\d{2}) GMT")
+WDAY_DICT = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
+MON_DICT = {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7,
+            "Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12}
+
 def get_logs_dir_path():
     return os.path.join(config.get_config_dir_path(), "logs")
 
@@ -87,6 +94,20 @@ def get_board_subjecttxt_path(bbs, board):
 
     return os.path.join(get_logs_dir_path(), bbs, board, "subject.txt")
 
+def get_board_idx_path(bbs, board):
+    """Returns board idx file path
+
+    bbs: bbs id
+
+    board: board id
+    """
+
+    # if parameter is empty, raise ValueError
+    if not bbs or not board:
+        raise ValueError, "parameter must not be empty"
+
+    return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
+
 def get_board_dir_path(bbs, board):
     """Returns board dir path
 
@@ -134,3 +155,22 @@ def get_board_cache_path(bbs, board):
         raise ValueError, "parameter must not be empty"
 
     return os.path.join(get_logs_dir_path(), bbs, board, ".cache")
+
+def httpdate_to_secs(httpdate):
+    """Returns the seconds since the epoch"""
+
+    m = REG_EXPR_HTTPDATE.match(httpdate)
+    if m:
+        tm_wday = WDAY_DICT[m.group(1)]
+        tm_mday = int(m.group(2))
+        tm_mon = MON_DICT[m.group(3)]
+        tm_year = int(m.group(4))
+        tm_hour = int(m.group(5))
+        tm_min = int(m.group(6))
+        tm_sec = int(m.group(7))
+
+        return int(time.mktime(
+            (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,0,-1)) \
+            - time.timezone)
+    else:
+        raise ValueError
index 745525d..c2bc58d 100644 (file)
@@ -21,6 +21,7 @@ import codecs
 import urllib
 from misc import get_board_subjecttxt_path
 from misc import get_board_subjecttxt_url
+from misc import get_board_idx_path
 
 REG_EXPR = re.compile("(?P<id>.*).dat<>(?P<title>.*)\((?P<res>\d*)\)")
 
@@ -54,6 +55,7 @@ def load_subjecttxt(bbs, board, func):
 def get_subjecttxt(bbs, board):
     u = urllib.urlopen(get_board_subjecttxt_url(bbs, board))
     subjecttxt_encoded = u.read()
+    info = u.info()
 
     subjecttxt_path = get_board_subjecttxt_path(bbs, board)
     
@@ -65,4 +67,32 @@ def get_subjecttxt(bbs, board):
     f.write(subjecttxt_encoded)
     f.close()
 
-    return subjecttxt_encoded.decode("cp932", "replace")
+    lastmod = None
+    if "Last-Modified" in info:
+        lastmod = info["Last-Modified"]
+        save_board_idx(bbs, board, lastmod)
+
+    return subjecttxt_encoded.decode("cp932", "replace"), lastmod
+
+def load_board_idx(bbs, board):
+    lastmod = ""
+    boardidxfile = get_board_idx_path(bbs, board)
+    if os.path.exists(boardidxfile):
+        for line in file(boardidxfile):
+            if line.startswith("lastModified="):
+                lastmod = line[len("lastModified="):].rstrip("\n")
+                break
+    return lastmod
+
+def save_board_idx(bbs, board, lastmod):
+    if not lastmod:
+        return
+
+    boardidx_path = get_board_idx_path(bbs, board)
+    basedir = os.path.dirname(boardidx_path)
+    if not os.path.isdir(basedir):
+        os.makedirs(basedir)
+
+    f = file(boardidx_path, "w")
+    f.write("lastModified=" + lastmod + "\n")
+    f.close()