OSDN Git Service

Now support almost all 2ch-like bbs.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / BbsType / bbs_type_base.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 re
19 import copy
20
21 from bbs_type_exception import BbsTypeError
22
23
24 class BaseType:
25     """Base of bbs type. Do not instantiate directly"""
26
27     bbs_type = "other"
28     _base_reg = None
29     _cgi_reg = None
30
31     def __init__(self, uri):
32         self.uri = uri
33         self.host = None
34         self.board = None
35         self.thread = None
36
37         self._parse_uri(uri)
38
39     def _parse_uri(self, uri):
40
41         m = self._base_reg.match(self.uri)
42         if m:
43             self.host = m.group("host")
44             self.board = m.group("board")
45         else:
46             m = self._cgi_reg.match(self.uri)
47             if m:
48                 self.host = m.group("host")
49                 self.board = m.group("board")
50                 self.thread = m.group("thread")
51             else:
52                 raise BbsTypeError, \
53                       "the uri %s does not represent %s" \
54                       % (self.uri, self.bbs_type)
55
56     def is_board(self):
57         return not self.thread
58
59     def is_thread(self):
60         return self.thread
61
62     def clone_with_thread(self, thread):
63         if not thread:
64             raise ValueError, "parameter must not be empty"
65         another = copy.copy(self)
66         another.thread = thread
67         another.uri = another.get_thread_uri()
68         return another
69
70     def get_uri_base(self):
71         return "http://" + self.host + "/" + self.board + "/"
72
73     def get_subject_txt_uri(self):
74         return self.get_uri_base() + "subject.txt"
75
76     def get_dat_uri(self):
77         if not self.thread:
78             raise BbsTypeError, "not specified thread"
79         return self.get_uri_base() + "dat/" + self.thread + ".dat"
80
81     def get_thread_uri(self):
82         if not self.thread:
83             raise BbsTypeError, "not specified thread"
84         return "http://" + self.host + "/test/read.cgi/" + \
85                   self.board + "/" + self.thread + "/"
86
87     def get_board_dir_path(self):
88         """Returns board dir path from logs dir downward, not full path"""
89
90         return self.bbs_type + "/" + self.host + "/" + self.board