OSDN Git Service

Reimprement regist and unregist a window to session.
[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_states_dir_path(bbs, board):
51     if not bbs or not board:
52         raise ValueError, "parameter must not be empty"
53
54     return os.path.join(get_board_dir_path(bbs, board), "states")
55
56 def get_thread_dat_path(bbs, board, thread):
57     """Returns thread dat file path
58
59     bbs: bbs id
60
61     board: board id
62
63     thread: thread id
64     """
65
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"
69
70     return os.path.join(get_thread_dat_dir_path(bbs, board), thread + ".dat")
71
72 def get_board_subjecttxt_url(bbs, board):
73     """Returns subject.txt file url
74
75     bbs: bbs id
76
77     board: board id
78     """
79
80     # if parameter is empty, raise ValueError
81     if not bbs or not board:
82         raise ValueError, "parameter must not be empty"
83
84     return get_board_base_url(bbs, board) + "subject.txt"
85
86 def get_board_subjecttxt_path(bbs, board):
87     """Returns subject.txt file path
88
89     bbs: bbs id
90
91     board: board id
92     """
93
94     # if parameter is empty, raise ValueError
95     if not bbs or not board:
96         raise ValueError, "parameter must not be empty"
97
98     return os.path.join(get_logs_dir_path(), bbs, board, "subject.txt")
99
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"
104
105     return os.path.join(get_thread_states_dir_path(bbs, board),
106                         thread + ".states")
107
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"
112
113     return os.path.join(get_board_dir_path(bbs, board), "board.states")
114
115 def get_board_idx_path(bbs, board):
116     """Returns board idx file path
117
118     bbs: bbs id
119
120     board: board id
121     """
122
123     # if parameter is empty, raise ValueError
124     if not bbs or not board:
125         raise ValueError, "parameter must not be empty"
126
127     return os.path.join(get_logs_dir_path(), bbs, board, "subject.idx")
128
129 def get_board_dir_path(bbs, board):
130     """Returns board dir path
131
132     bbs: bbs ID
133
134     board: board ID
135     """
136
137     # if parameter is empty, raise ValueError
138     if not bbs or not board:
139         raise ValueError, "parameter must not be empty"
140
141     return os.path.join(get_logs_dir_path(), bbs, board)
142
143 def get_thread_idx_path(bbs, board, thread):
144     """Returns idx file path of thread
145
146     bbs: bbs ID
147
148     board: board ID
149
150     thread: thread ID
151
152     Note: if parameter is empty, raise ValueError
153     """
154
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"
158
159     return os.path.join(get_thread_idx_dir_path(bbs, board), thread + ".idx")
160
161 def get_board_cache_path(bbs, board):
162     """ Returns .cache file path of board
163
164     bbs: bbs ID
165
166     board: board ID
167
168     Note: if parameter is empty, raise ValueError
169     """
170
171     # if parameter is empty, raise ValueError
172     if not bbs or not board:
173         raise ValueError, "parameter must not be empty"
174
175     return os.path.join(get_thread_idx_dir_path(bbs, board), ".cache")
176
177 def httpdate_to_secs(httpdate):
178     """Returns the seconds since the epoch"""
179
180     m = REG_EXPR_HTTPDATE.match(httpdate)
181     if m:
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))
189
190         return int(time.mktime(
191             (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,0,-1)) \
192             - time.timezone)
193     else:
194         raise ValueError