OSDN Git Service

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