OSDN Git Service

step two, modify script.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / 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
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}
28
29 def get_logs_dir_path():
30     return os.path.join(config.get_config_dir_path(), "logs")
31
32 def get_thread_dat_dir_path(bbs, board):
33     """Returns dir path for saving thread dat file"""
34
35     # if parameter is empty, raise ValueError
36     if not bbs or not board:
37         raise ValueError, "parameter must not be empty"
38
39     return os.path.join(get_board_dir_path(bbs, board), "dat")
40
41 def get_thread_idx_dir_path(bbs, board):
42     """Returns dir path for saving thread index file"""
43
44     # if parameter is empty, raise ValueError
45     if not bbs or not board:
46         raise ValueError, "parameter must not be empty"
47
48     return os.path.join(get_board_dir_path(bbs, board), "idx")
49
50 def get_thread_dat_path(bbs, board, thread):
51     """Returns thread dat file path
52
53     bbs: bbs id
54
55     board: board id
56
57     thread: thread id
58     """
59
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"
63
64     return os.path.join(get_thread_dat_dir_path(bbs, board), thread + ".dat")
65
66 def get_board_subjecttxt_url(bbs, board):
67     """Returns subject.txt file url
68
69     bbs: bbs id
70
71     board: board id
72     """
73
74     # if parameter is empty, raise ValueError
75     if not bbs or not board:
76         raise ValueError, "parameter must not be empty"
77
78     return get_board_base_url(bbs, board) + "subject.txt"
79
80 def get_board_subjecttxt_path(bbs, board):
81     """Returns subject.txt file path
82
83     bbs: bbs id
84
85     board: board id
86     """
87
88     # if parameter is empty, raise ValueError
89     if not bbs or not board:
90         raise ValueError, "parameter must not be empty"
91
92     return os.path.join(get_logs_dir_path(), bbs, board, "subject.txt")
93
94 def get_board_idx_path(bbs, board):
95     """Returns board idx file path
96
97     bbs: bbs id
98
99     board: board id
100     """
101
102     # if parameter is empty, raise ValueError
103     if not bbs or not board:
104         raise ValueError, "parameter must not be empty"
105
106     return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
107
108 def get_board_dir_path(bbs, board):
109     """Returns board dir path
110
111     bbs: bbs ID
112
113     board: board ID
114     """
115
116     # if parameter is empty, raise ValueError
117     if not bbs or not board:
118         raise ValueError, "parameter must not be empty"
119
120     return os.path.join(get_logs_dir_path(), bbs, board)
121
122 def get_thread_idx_path(bbs, board, thread):
123     """Returns idx file path of thread
124
125     bbs: bbs ID
126
127     board: board ID
128
129     thread: thread ID
130
131     Note: if parameter is empty, raise ValueError
132     """
133
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"
137
138     return os.path.join(get_thread_idx_dir_path(bbs, board), thread + ".idx")
139
140 def get_board_cache_path(bbs, board):
141     """ Returns .cache file path of board
142
143     bbs: bbs ID
144
145     board: board ID
146
147     Note: if parameter is empty, raise ValueError
148     """
149
150     # if parameter is empty, raise ValueError
151     if not bbs or not board:
152         raise ValueError, "parameter must not be empty"
153
154     return os.path.join(get_thread_idx_dir_path(bbs, board), ".cache")
155
156 def httpdate_to_secs(httpdate):
157     """Returns the seconds since the epoch"""
158
159     m = REG_EXPR_HTTPDATE.match(httpdate)
160     if m:
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))
168
169         return int(time.mktime(
170             (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,0,-1)) \
171             - time.timezone)
172     else:
173         raise ValueError