1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 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")
25 WDAY_DICT = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
26 MON_DICT = {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7,
27 "Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12}
29 def get_logs_dir_path():
30 return os.path.join(config.get_config_dir_path(), "logs")
32 def get_thread_dat_dir_path(bbs, board):
33 """Returns dir path for saving thread dat file"""
35 # if parameter is empty, raise ValueError
36 if not bbs or not board:
37 raise ValueError, "parameter must not be empty"
39 return os.path.join(get_board_dir_path(bbs, board), "dat")
41 def get_thread_idx_dir_path(bbs, board):
42 """Returns dir path for saving thread index file"""
44 # if parameter is empty, raise ValueError
45 if not bbs or not board:
46 raise ValueError, "parameter must not be empty"
48 return os.path.join(get_board_dir_path(bbs, board), "idx")
50 def get_thread_dat_path(bbs, board, thread):
51 """Returns thread dat file path
60 # if parameter is empty, raise ValueError
61 if not bbs or not board or not thread:
62 raise ValueError, "parameter must not be empty"
64 return os.path.join(get_thread_dat_dir_path(bbs, board), thread + ".dat")
66 def get_board_subjecttxt_url(bbs, board):
67 """Returns subject.txt file url
74 # if parameter is empty, raise ValueError
75 if not bbs or not board:
76 raise ValueError, "parameter must not be empty"
78 return get_board_base_url(bbs, board) + "subject.txt"
80 def get_board_subjecttxt_path(bbs, board):
81 """Returns subject.txt file path
88 # if parameter is empty, raise ValueError
89 if not bbs or not board:
90 raise ValueError, "parameter must not be empty"
92 return os.path.join(get_logs_dir_path(), bbs, board, "subject.txt")
94 def get_board_idx_path(bbs, board):
95 """Returns board idx file path
102 # if parameter is empty, raise ValueError
103 if not bbs or not board:
104 raise ValueError, "parameter must not be empty"
106 return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
108 def get_board_dir_path(bbs, board):
109 """Returns board dir path
116 # if parameter is empty, raise ValueError
117 if not bbs or not board:
118 raise ValueError, "parameter must not be empty"
120 return os.path.join(get_logs_dir_path(), bbs, board)
122 def get_thread_idx_path(bbs, board, thread):
123 """Returns idx file path of thread
131 Note: if parameter is empty, raise ValueError
134 # if parameter is empty, raise ValueError
135 if not bbs or not board or not thread:
136 raise ValueError, "parameter must not be empty"
138 return os.path.join(get_thread_idx_dir_path(bbs, board), thread + ".idx")
140 def get_board_cache_path(bbs, board):
141 """ Returns .cache file path of board
147 Note: if parameter is empty, raise ValueError
150 # if parameter is empty, raise ValueError
151 if not bbs or not board:
152 raise ValueError, "parameter must not be empty"
154 return os.path.join(get_thread_idx_dir_path(bbs, board), ".cache")
156 def httpdate_to_secs(httpdate):
157 """Returns the seconds since the epoch"""
159 m = REG_EXPR_HTTPDATE.match(httpdate)
161 tm_wday = WDAY_DICT[m.group(1)]
162 tm_mday = int(m.group(2))
163 tm_mon = MON_DICT[m.group(3)]
164 tm_year = int(m.group(4))
165 tm_hour = int(m.group(5))
166 tm_min = int(m.group(6))
167 tm_sec = int(m.group(7))
169 return int(time.mktime(
170 (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,0,-1)) \