]> git.phdru.name Git - bookmarks_db.git/blob - bkmk_parser.py
DEBUG. Used +=.
[bookmarks_db.git] / bkmk_parser.py
1 """
2    Parser for Netscape Navigator's and Mozilla's bookmarks.html
3
4    Written by BroytMann. Copyright (C) 1997-2003 PhiloSoft Design
5 """
6
7
8 import os
9 from m_lib.net.www.html import HTMLParser
10 from bkmk_objects import Folder, Bookmark, Ruler
11
12
13 DEBUG = os.environ.has_key("BKMK_DEBUG")
14
15 if DEBUG:
16    def debug(note):
17       print note
18
19    def dump_names(folder_stack):
20       l = []
21       for object in folder_stack:
22          if object.isFolder:
23             l.append(object.name)
24       return "'%s'" % "' '".join(l)
25
26 else:
27    def debug(note):
28       pass
29    dump_names = debug
30
31
32 class BkmkParser(HTMLParser):
33    def __init__(self):
34       HTMLParser.__init__(self)
35
36       self.urls = 0
37       self.objects = 0
38
39       self.charset = ""
40       self.recode = None
41
42
43    def handle_data(self, data):
44       if data:
45          if self.charset:
46             data = unicode(data, self.charset).encode()
47          self.accumulator += data
48
49
50    # Mozilla - get charset
51    def do_meta(self, attrs):
52       http_equiv = ""
53       content = ""
54
55       for attrname, value in attrs:
56          value = value.strip()
57          if attrname == 'http-equiv':
58             http_equiv = value.lower()
59          elif attrname == 'content':
60             content = value
61
62       if http_equiv == "content-type":
63          try:
64             # extract charset from "text/html; charset=UTF-8"
65             self.charset = content.split('=')[1]
66          except IndexError:
67             pass
68
69
70    def start_title(self, attrs):
71       self.accumulator += "<TITLE>"
72
73    def end_title(self):
74       self.accumulator += "</TITLE>"
75
76
77    # Start root folder
78    def start_h1(self, attrs):
79       root_folder = Folder()
80       self.current_object = root_folder
81       self.root_folder = root_folder
82       self.current_folder = root_folder
83       self.folder_stack = [root_folder]
84
85       self.root_folder.header = self.accumulator.strip()
86       self.accumulator = ''
87
88    def end_h1(self):
89       accumulator = self.accumulator
90       self.accumulator = ''
91
92       debug("Root folder name: `%s'" % accumulator)
93       self.root_folder.name = accumulator
94
95
96    # Start next folder
97    def start_h3(self, attrs):
98       for attrname, value in attrs:
99          value = value.strip()
100          if attrname == 'add_date':
101             add_date = value
102
103       debug("New folder...")
104       folder = Folder(add_date)
105       self.current_object = folder
106       self.current_folder.append(folder)
107       self.folder_stack.append(folder) # push new folder
108       self.current_folder = folder
109       self.objects += 1
110
111    def end_h3(self):
112       accumulator = self.accumulator
113       self.accumulator = ''
114
115       debug("Folder name: `%s'" % accumulator)
116       self.current_folder.name = accumulator
117
118
119    # Start bookmark
120    def start_a(self, attrs):
121       last_visit = None
122       last_modified = None
123
124       for attrname, value in attrs:
125          value = value.strip()
126          if attrname == 'href':
127             href = value
128          if attrname == 'add_date':
129             add_date = value
130          if attrname == 'last_visit':
131             last_visit = value
132          if attrname == 'last_modified':
133             last_modified = value
134
135       debug("Bookmark points to: `%s'" % href)
136       bookmark = Bookmark(href, add_date, last_visit, last_modified)
137       self.current_object = bookmark
138       self.current_folder.append(bookmark)
139       self.urls += 1
140       self.objects += 1
141
142    def end_a(self):
143       accumulator = self.accumulator
144       self.accumulator = ''
145
146       debug("Bookmark name: `%s'" % accumulator)
147       bookmark = self.current_folder[-1]
148       bookmark.name = accumulator
149
150
151    def flush(self):
152       accumulator = self.accumulator
153
154       if accumulator:
155          self.accumulator = ''
156
157          current_object = self.current_object
158          if current_object:
159             current_object.comment += accumulator.strip()
160             debug("Comment: `%s'" % current_object.comment)
161
162
163    def start_dl(self, attrs):
164       self.flush()
165
166    do_dt = start_dl
167
168
169    # End of folder
170    def end_dl(self):
171       self.flush()
172       debug("End folder")
173       debug("Folder stack: %s" % dump_names(self.folder_stack))
174       if self.folder_stack:
175          del self.folder_stack[-1] # pop last folder
176          if self.folder_stack:
177             self.current_folder = self.folder_stack[-1]
178          else:
179             debug("FOLDER STACK is EMPTY!!! (1)")
180       else:
181          debug("FOLDER STACK is EMPTY!!! (2)")
182       self.current_object = None
183
184
185    def close(self):
186       HTMLParser.close(self)
187       if self.folder_stack:
188          raise ValueError, "wrong folder stack: %s" % self.folder_stack
189
190
191    def do_dd(self, attrs):
192       pass
193
194    do_p = do_dd
195
196
197    # Start ruler
198    def do_hr(self, attrs):
199       self.flush()
200       debug("Ruler")
201       self.current_folder.append(Ruler())
202       self.current_object = None
203       self.objects += 1
204
205
206    # BR in comment
207    def do_br(self, attrs):
208       self.accumulator += "<BR>"
209
210
211    # Allow < in the text
212    def unknown_starttag(self, tag, attrs):
213       self.accumulator += "<%s>" % tag
214
215
216    # Do not allow unknow end tags
217    def unknown_endtag(self, tag):
218       raise NotImplementedError("Unknow end tag `%s'" % tag)