OSDN Git Service

Notify thread idx updated to board window.
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / misc.py
index d8375c9..5d74bd0 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")
 
 def get_board_base_url(bbs, board):
     """
-    None: this function uses brdlist.get function, so brdlist.read function
+    Note: this function uses brdlist.get function, so brdlist.init function
     should have been called before this function is called
     """
     # if parameter is empty, raise ValueError
@@ -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