import threading
import config
+from BbsType import bbs_type_exception
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 _check_thread(bbs_type):
+ """if bbs_type is not thread, raise BbsTypeError"""
+
+ if not bbs_type.is_thread():
+ raise bbs_type_exception.BbsTypeError, \
+ "the bbs_type does not represent thread: " + bbs_type.uri
+
def get_logs_dir_path():
return os.path.join(config.get_config_dir_path(), "logs")
-def get_thread_dat_dir_path(bbs, board):
+def get_thread_dat_dir_path(bbs_type):
"""Returns dir path for saving thread dat file"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "dat")
- return os.path.join(get_board_dir_path(bbs, board), "dat")
-
-def get_thread_idx_dir_path(bbs, board):
+def get_thread_idx_dir_path(bbs_type):
"""Returns dir path for saving thread index file"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_board_dir_path(bbs, board), "idx")
-
-def get_thread_states_dir_path(bbs, board):
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_board_dir_path(bbs, board), "states")
-
-def get_thread_dat_path(bbs, board, thread):
- """Returns thread dat file path
-
- bbs: bbs id
-
- board: board id
-
- thread: thread id
- """
-
- # if parameter is empty, raise ValueError
- if not bbs or not board or not thread:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_thread_dat_dir_path(bbs, board), thread + ".dat")
-
-def get_board_subjecttxt_url(bbs, board):
- """Returns subject.txt file url
-
- 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 get_board_base_url(bbs, board) + "subject.txt"
-
-def get_board_subjecttxt_path(bbs, board):
- """Returns subject.txt 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.txt")
-
-def get_thread_states_path(bbs, board, thread):
- # if parameter is empty, raise ValueError
- if not bbs or not board or not thread:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_thread_states_dir_path(bbs, board),
- thread + ".states")
-
-def get_board_states_path(bbs, board):
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
-
- return os.path.join(get_board_dir_path(bbs, board), "board.states")
-
-def get_board_idx_path(bbs, board):
- """Returns board idx file path
-
- bbs: bbs id
+ return os.path.join(get_board_dir_path(bbs_type), "idx")
- board: board id
- """
+def get_thread_states_dir_path(bbs_type):
+ """Returns dir path for saving thread states file"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "states")
- return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
+def get_thread_dat_path(bbs_type):
+ """Returns thread dat file path"""
-def get_board_dir_path(bbs, board):
- """Returns board dir path
+ _check_thread(bbs_type)
- bbs: bbs ID
+ return os.path.join(get_thread_dat_dir_path(bbs_type),
+ bbs_type.thread + ".dat")
- board: board ID
- """
+def get_board_subjecttxt_path(bbs_type):
+ """Returns subject.txt file path"""
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "subject.txt")
- return os.path.join(get_logs_dir_path(), bbs, board)
+def get_thread_states_path(bbs_type):
+ """Returns thread states file path"""
-def get_thread_idx_path(bbs, board, thread):
- """Returns idx file path of thread
+ _check_thread(bbs_type)
- bbs: bbs ID
+ return os.path.join(get_thread_states_dir_path(bbs_type),
+ bbs_type.thread + ".states")
- board: board ID
+def get_board_states_path(bbs_type):
+ """Returns board states file path"""
- thread: thread ID
+ return os.path.join(get_board_dir_path(bbs_type), "board.states")
- Note: if parameter is empty, raise ValueError
- """
+def get_board_idx_path(bbs_type):
+ """Returns board idx file path"""
- # if parameter is empty, raise ValueError
- if not bbs or not board or not thread:
- raise ValueError, "parameter must not be empty"
+ return os.path.join(get_board_dir_path(bbs_type), "subject.idx")
- return os.path.join(get_thread_idx_dir_path(bbs, board), thread + ".idx")
+def get_board_dir_path(bbs_type):
+ """Returns board dir path"""
-def get_board_cache_path(bbs, board):
- """ Returns .cache file path of board
+ return os.path.join(get_logs_dir_path(), bbs_type.get_board_dir_path())
- bbs: bbs ID
+def get_thread_idx_path(bbs_type):
+ """Returns idx file path of thread"""
- board: board ID
+ _check_thread(bbs_type)
- Note: if parameter is empty, raise ValueError
- """
+ return os.path.join(get_thread_idx_dir_path(bbs_type),
+ bbs_type.thread + ".idx")
- # if parameter is empty, raise ValueError
- if not bbs or not board:
- raise ValueError, "parameter must not be empty"
+def get_board_cache_path(bbs_type):
+ """Returns .cache file path of board"""
- return os.path.join(get_thread_idx_dir_path(bbs, board), ".cache")
+ return os.path.join(get_thread_idx_dir_path(bbs_type), ".cache")
def httpdate_to_secs(httpdate):
"""Returns the seconds since the epoch"""