More fixes for signal simulation in the completion prototype
[ephy-history.git] / xml2db.py
blob29a949ee5e9d256340c94ceb4e276d68744b2f1d
1 #!/usr/bin/env python
3 from xml.sax import make_parser
4 from xml.sax.handler import ContentHandler
5 from ephyhistory import History, HistoryPageNode
6 import sys
7 import os.path
9 class HistoryHandler(ContentHandler):
10 def __init__ (self):
11 self.propId = 0
12 self.props = {}
13 self.inProp = False
14 self.history = History()
16 def startElement(self, name, attrs):
17 # page item
18 if name == 'node':
19 self.props = {}
21 # page properties
22 elif name == 'property':
23 self.propId = int(attrs.get('id'))
24 self.inProp = True
26 def endElement(self, name):
27 if name == 'node':
28 # print self.props
29 # insert into DB
30 self.createVisit(self.props)
31 elif name == 'property':
32 self.inProp = False
34 def characters(self, ch):
35 if self.inProp:
36 self.props[self.propId] = self.props.get(self.propId, '') + ch
38 def createVisit(self, props):
39 # refer to enum in ephy-history.h for propId -> property mapping
41 # XXX: add a function to the API for specifying
42 # visit count and visit date when adding a page
44 url = props.get(3, None)
45 if url:
46 self.history.add_page(url, redirect=False, toplevel=False, ref_url='')
47 title = props.get(2, None)
48 if title:
49 self.history.set_page_title(url, title.encode('utf-8'))
50 favicon = props.get(9, None)
51 if favicon:
52 self.history.set_icon(url, favicon)
54 if __name__ == '__main__':
55 if len(sys.argv) < 2:
56 xmlfile = os.path.expanduser('~/.gnome2/epiphany/ephy-history.xml')
57 else:
58 xmlfile = sys.argv[1]
59 parser = make_parser()
60 hh = HistoryHandler()
61 parser.setContentHandler(hh)
62 parser.parse(xmlfile)