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_states_dir_path(bbs, board):
51 if not bbs or not board:
52 raise ValueError, "parameter must not be empty"
54 return os.path.join(get_board_dir_path(bbs, board), "states")
56 def get_thread_dat_path(bbs, board, thread):
57 """Returns thread dat file path
66 # if parameter is empty, raise ValueError
67 if not bbs or not board or not thread:
68 raise ValueError, "parameter must not be empty"
70 return os.path.join(get_thread_dat_dir_path(bbs, board), thread + ".dat")
72 def get_board_subjecttxt_url(bbs, board):
73 """Returns subject.txt file url
80 # if parameter is empty, raise ValueError
81 if not bbs or not board:
82 raise ValueError, "parameter must not be empty"
84 return get_board_base_url(bbs, board) + "subject.txt"
86 def get_board_subjecttxt_path(bbs, board):
87 """Returns subject.txt file path
94 # if parameter is empty, raise ValueError
95 if not bbs or not board:
96 raise ValueError, "parameter must not be empty"
98 return os.path.join(get_logs_dir_path(), bbs, board, "subject.txt")
100 def get_thread_states_path(bbs, board, thread):
101 # if parameter is empty, raise ValueError
102 if not bbs or not board or not thread:
103 raise ValueError, "parameter must not be empty"
105 return os.path.join(get_thread_states_dir_path(bbs, board),
108 def get_board_states_path(bbs, board):
109 # if parameter is empty, raise ValueError
110 if not bbs or not board:
111 raise ValueError, "parameter must not be empty"
113 return os.path.join(get_board_dir_path(bbs, board), "board.states")
115 def get_board_idx_path(bbs, board):
116 """Returns board idx file path
123 # if parameter is empty, raise ValueError
124 if not bbs or not board:
125 raise ValueError, "parameter must not be empty"
127 return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
129 def get_board_dir_path(bbs, board):
130 """Returns board dir path
137 # if parameter is empty, raise ValueError
138 if not bbs or not board:
139 raise ValueError, "parameter must not be empty"
141 return os.path.join(get_logs_dir_path(), bbs, board)
143 def get_thread_idx_path(bbs, board, thread):
144 """Returns idx file path of thread
152 Note: if parameter is empty, raise ValueError
155 # if parameter is empty, raise ValueError
156 if not bbs or not board or not thread:
157 raise ValueError, "parameter must not be empty"
159 return os.path.join(get_thread_idx_dir_path(bbs, board), thread + ".idx")
161 def get_board_cache_path(bbs, board):
162 """ Returns .cache file path of board
168 Note: if parameter is empty, raise ValueError
171 # if parameter is empty, raise ValueError
172 if not bbs or not board:
173 raise ValueError, "parameter must not be empty"
175 return os.path.join(get_thread_idx_dir_path(bbs, board), ".cache")
177 def httpdate_to_secs(httpdate):
178 """Returns the seconds since the epoch"""
180 m = REG_EXPR_HTTPDATE.match(httpdate)
182 tm_wday = WDAY_DICT[m.group(1)]
183 tm_mday = int(m.group(2))
184 tm_mon = MON_DICT[m.group(3)]
185 tm_year = int(m.group(4))
186 tm_hour = int(m.group(5))
187 tm_min = int(m.group(6))
188 tm_sec = int(m.group(7))
190 return int(time.mktime(
191 (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,0,-1)) \