author | fabien |
Fri, 24 Oct 2003 17:24:19 -0400 | |
branch | xbelweb |
changeset 48 | 9cde0ea6f411 |
parent 26 | 17b0cd274530 |
permissions | -rw-r--r-- |
24 | 1 |
#!/usr/bin/python |
2 |
||
3 |
from xbel_handler import XBELHandler, bookmark |
|
4 |
from xml.sax import saxexts |
|
5 |
import bkmark |
|
6 |
||
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
7 |
def parse_xbel(xbelfile): |
24 | 8 |
xbel_handler = XBELHandler() |
9 |
p=saxexts.XMLParserFactory.make_parser("xml.sax.drivers.drv_xmlproc") |
|
10 |
p.setDocumentHandler( xbel_handler ) |
|
11 |
p.parseFile( xbelfile ) |
|
12 |
return xbel_handler.bms |
|
13 |
||
14 |
_keywords = {} |
|
15 |
||
16 |
def import_bookmarks(db, bms): |
|
17 |
global _keywords |
|
18 |
_keywords = {} |
|
19 |
for elem in db.get_all_keywords(): |
|
20 |
_keywords[elem[1]] = elem[0] |
|
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
21 |
#print "<body>" |
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
22 |
#print "<ol>" |
25 | 23 |
for child in bms.folders: |
24 |
if isinstance(child, bookmark.Bookmark): |
|
25 |
import_bookmark(db, child) |
|
26 |
else: |
|
27 |
import_folder(db, child) |
|
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
28 |
#print "</ol>" |
24 | 29 |
|
30 |
def import_folder(db, folder, keywords = []): |
|
31 |
global _keywords |
|
25 | 32 |
kw = folder.title.strip().encode('utf-8') |
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
33 |
#print "<li>" + kw |
24 | 34 |
if _keywords.has_key(kw): |
35 |
id = _keywords[kw] |
|
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
36 |
#print " [%d]" % (id,) |
24 | 37 |
else: |
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
38 |
id = db.add_keyword(kw) |
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
39 |
id = len(_keywords) |
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
40 |
#print " [%d] (added)" % (id,) |
24 | 41 |
_keywords[kw] = id |
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
42 |
#print "<ol>" |
24 | 43 |
for child in folder.children: |
44 |
if isinstance(child, bookmark.Bookmark): |
|
45 |
import_bookmark(db, child, keywords+[id]) |
|
46 |
else: |
|
47 |
import_folder(db, child, keywords+[id]) |
|
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
48 |
#print "</ol></li>" |
24 | 49 |
|
25 | 50 |
def import_bookmark(db, bookmark, keywords = []): |
24 | 51 |
bk = bkmark.Bookmark() |
25 | 52 |
bk.name = bookmark.title.encode('utf-8') |
53 |
bk.url = bookmark.href.encode('utf-8') |
|
54 |
if hasattr(bookmark, 'desc'): |
|
55 |
bk.desc = bookmark.desc.encode('utf-8') |
|
26
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
56 |
id = db.add_bookmark(bk) |
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
57 |
#print "<li>add bookmark '" + bk.name + "' [%d]" % (id,) |
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
58 |
#print " with keywords '" + ','.join(map(str,keywords)) + "'</li>" |
17b0cd274530
[svn r1560] Add import functionality and correct a bug in the merge_kw.
fabien
parents:
25
diff
changeset
|
59 |
db.update_keywords(id, keywords) |
24 | 60 |