OSDN Git Service

Reduce printing messages.
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / misc.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
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.
8 #
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.
13 #
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
17
18 import os.path
19 import re
20 import time
21
22 import config
23 import brdlist
24
25 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")
26 WDAY_DICT = {"Mon":0, "Tue":1, "Wed":2, "Thu":3, "Fri":4, "Sat":5, "Sun":6}
27 MON_DICT = {"Jan":1, "Feb":2, "Mar":3, "Apr":4, "May":5, "Jun":6, "Jul":7,
28             "Aug":8, "Sep":9, "Oct":10, "Nov":11, "Dec":12}
29
30 def get_logs_dir_path():
31     return os.path.join(config.get_config_dir_path(), "logs")
32
33 def get_board_base_url(bbs, board):
34     """
35     Note: this function uses brdlist.get function, so brdlist.init function
36     should have been called before this function is called
37     """
38     # if parameter is empty, raise ValueError
39     if not bbs or not board:
40         raise ValueError, "parameter must not be empty"
41
42     return "http://" + brdlist.get(bbs, board, "host") + "/" + board + "/"
43
44 def get_thread_dat_url(bbs, board, thread):
45     """Returns thread dat url"""
46
47     # if parameter is empty, raise ValueError
48     if not bbs or not board or not thread:
49         raise ValueError, "parameter must not be empty"
50
51     return get_board_base_url(bbs, board) + "dat/" + thread + ".dat"
52
53 def get_thread_dat_path(bbs, board, thread):
54     """Returns thread dat file path
55
56     bbs: bbs id
57
58     board: board id
59
60     thread: thread id
61     """
62
63     # if parameter is empty, raise ValueError
64     if not bbs or not board or not thread:
65         raise ValueError, "parameter must not be empty"
66
67     return os.path.join(get_board_dir_path(bbs, board), thread + ".dat")
68
69 def get_board_subjecttxt_url(bbs, board):
70     """Returns subject.txt file url
71
72     bbs: bbs id
73
74     board: board id
75     """
76
77     # if parameter is empty, raise ValueError
78     if not bbs or not board:
79         raise ValueError, "parameter must not be empty"
80
81     return get_board_base_url(bbs, board) + "subject.txt"
82
83 def get_board_subjecttxt_path(bbs, board):
84     """Returns subject.txt file path
85
86     bbs: bbs id
87
88     board: board id
89     """
90
91     # if parameter is empty, raise ValueError
92     if not bbs or not board:
93         raise ValueError, "parameter must not be empty"
94
95     return os.path.join(get_logs_dir_path(), bbs, board, "subject.txt")
96
97 def get_board_idx_path(bbs, board):
98     """Returns board idx file path
99
100     bbs: bbs id
101
102     board: board id
103     """
104
105     # if parameter is empty, raise ValueError
106     if not bbs or not board:
107         raise ValueError, "parameter must not be empty"
108
109     return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
110
111 def get_board_dir_path(bbs, board):
112     """Returns board dir path
113
114     bbs: bbs ID
115
116     board: board ID
117     """
118
119     # if parameter is empty, raise ValueError
120     if not bbs or not board:
121         raise ValueError, "parameter must not be empty"
122
123     return os.path.join(get_logs_dir_path(), bbs, board)
124
125 def get_thread_idx_path(bbs, board, thread):
126     """Returns idx file path of thread
127
128     bbs: bbs ID
129
130     board: board ID
131
132     thread: thread ID
133
134     Note: if parameter is empty, raise ValueError
135     """
136
137     # if parameter is empty, raise ValueError
138     if not bbs or not board or not thread:
139         raise ValueError, "parameter must not be empty"
140
141     return os.path.join(get_logs_dir_path(), bbs, board, thread + ".idx")
142
143 def get_board_cache_path(bbs, board):
144     """ Returns .cache file path of board
145
146     bbs: bbs ID
147
148     board: board ID
149
150     Note: if parameter is empty, raise ValueError
151     """
152
153     # if parameter is empty, raise ValueError
154     if not bbs or not board:
155         raise ValueError, "parameter must not be empty"
156
157     return os.path.join(get_logs_dir_path(), bbs, board, ".cache")
158
159 def httpdate_to_secs(httpdate):
160     """Returns the seconds since the epoch"""
161
162     m = REG_EXPR_HTTPDATE.match(httpdate)
163     if m:
164         tm_wday = WDAY_DICT[m.group(1)]
165         tm_mday = int(m.group(2))
166         tm_mon = MON_DICT[m.group(3)]
167         tm_year = int(m.group(4))
168         tm_hour = int(m.group(5))
169         tm_min = int(m.group(6))
170         tm_sec = int(m.group(7))
171
172         return int(time.mktime(
173             (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,0,-1)) \
174             - time.timezone)
175     else:
176         raise ValueError