OSDN Git Service

Multithreads are abandoned. Alternatly, The asyncore substitutes.(#16776)
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / bookmark_list.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 urllib2
20 import codecs
21 import traceback
22 import os
23 import os.path
24
25 import config
26 from http_sub import HTTPRedirectHandler302, HTTPDebugHandler
27 from bookmark_core import BookmarkFormatError, Bookmark
28 from bookmark_editwindow import BookmarkEditWindow
29
30 bookmark_list = None
31
32 def init(bookmarks_path, bbsmenu_uri):
33     global bookmark_list
34
35     bookmark_list = BookmarkList(bookmarks_path, bbsmenu_uri)
36     bookmark_list.load()
37
38
39 class BookmarkList(object):
40
41     def __init__(self, bookmarks_path, bbsmenu_uri):
42         self.__bookmarks_path = bookmarks_path
43         self.__bbsmenu_uri = bbsmenu_uri
44         self.__bookmark_list = []
45         self.__view = None
46
47     def set_view(self, view):
48         self.__view = view
49
50     def load(self):
51         bookmark_list = []
52         try:
53             for line in file(self.__bookmarks_path):
54                 line = line.rstrip("\n")
55                 try:
56                     bookmark = Bookmark(formatted=line)
57                 except BookmarkFormatError:
58                     traceback.print_exc()
59                 else:
60                     bookmark_list.append(bookmark)
61         except IOError:
62             traceback.print_exc()
63         else:
64             self.__bookmark_list = bookmark_list
65
66     def save(self):
67         try:
68             basedir = os.path.dirname(self.__bookmarks_path)
69             if not os.path.isdir(basedir):
70                 os.makedirs(basedir)
71             f = file(self.__bookmarks_path, "w")
72             for bookmark in self.__bookmark_list:
73                 line = bookmark.tabbed_text() + "\n"
74                 f.write(line)
75         except:
76             traceback.print_exc()
77         else:
78             f.close()
79
80     def update_bbsmenu(self):
81         reg = re.compile(
82             "<A HREF=(?P<uri>http://[^\s]+).*>(?P<name>[^<]+)</A>")
83         category_reg = re.compile(
84             "(?:<BR>){1,2}<B>(?P<category>[^<]+)</B><BR>")
85
86         new_bookmark_list = []
87         bookmark_uri_dict = {}
88         encoding = "cp932"
89
90         current_category = ""
91         opener = urllib2.build_opener(HTTPRedirectHandler302, HTTPDebugHandler)
92         req = urllib2.Request(self.__bbsmenu_uri)
93         req.add_header("User-agent", config.User_Agent)
94         try:
95             for line in opener.open(req):
96                 line = line.decode(encoding, "replace").strip()
97                 if line:
98                     if not current_category:
99                         m = category_reg.match(line)
100                         if m:
101                             current_category = m.group("category")
102                             continue
103                     m = reg.match(line)
104                     if m:
105                         name = m.group("name")
106                         uri = m.group("uri")
107                         category = current_category
108                         if uri and uri in bookmark_uri_dict:
109                             bookmark = bookmark_uri_dict[uri]
110                             if category and \
111                                category not in bookmark.categories:
112                                 bookmark.categories.append(category)
113                         elif name and uri:
114                             categories = ["bbsmenu"]
115                             if category:
116                                 categories.append(category)
117                             bookmark = Bookmark(uri=uri, name=name,
118                                                 categories=categories)
119                             new_bookmark_list.append(bookmark)
120                             bookmark_uri_dict[uri] = bookmark
121                 else:
122                     current_category = ""
123         except:
124             traceback.print_exc()
125         else:
126             self._merge_bbsmenu(new_bookmark_list)
127
128     def _merge_bbsmenu(self, new_bbsmenu_list):
129         old_list = []
130         for bookmark in self.__bookmark_list:
131             if "bbsmenu" not in bookmark.categories:
132                 old_list.append(bookmark)
133         new_bbsmenu_list += old_list
134         self.__bookmark_list = new_bbsmenu_list
135         self.save()
136         category_list = []
137         for bookmark in self.__bookmark_list:
138             for category in bookmark.categories:
139                 if category and category not in category_list:
140                     category_list.append(category)
141
142     def get_category_list(self):
143         categories = []
144         for bookmark in self.__bookmark_list:
145             for category in bookmark.categories:
146                 if category and category not in categories:
147                     # avoid empty and duplicate
148                     categories.append(category)
149         return categories
150
151     def get_bookmark_list_in_category(self, category):
152         bookmarks = []
153         for bookmark in self.__bookmark_list:
154             if category in bookmark.categories:
155                 bookmarks.append(bookmark)
156         return bookmarks
157
158     def get_non_category_bookmark(self):
159         bookmarks = []
160         for bookmark in self.__bookmark_list:
161             have_category = False
162             for category in bookmark.categories:
163                 if category:
164                     have_category = True
165                     break
166             if not have_category:
167                 bookmarks.append(bookmark)
168         return bookmarks
169
170     def add_new_bookmark(self, bookmark):
171         self.__bookmark_list.append(bookmark)
172         self.save()
173
174     def delete_bookmark(self, bookmark):
175         try:
176             index = self.__bookmark_list.index(bookmark)
177         except ValueError:
178             pass
179         else:
180             del self.__bookmark_list[index]
181             self.save()
182
183     def add_bookmark_with_edit(self, name="New Bookmark", uri=None,
184                                categories=None):
185
186         def on_edit_complete(new_bookmark):
187             self.add_new_bookmark(new_bookmark)
188             self.new_bookmark_added()
189
190         if not categories:
191             categories = []
192         bookmark = Bookmark(name=name, uri=uri, categories=categories)
193         BookmarkEditWindow(on_edit_complete, bookmark)
194
195     def new_bookmark_added(self):
196         if self.__view:
197             self.__view.update_categories()