FIX(buku) ff json import improve test, fix var bug

- better json title parsing, catching "no title" situation
- add/fix test for "ignore special root bookmark folders" feature
This commit is contained in:
Chris Drexler 2019-01-02 22:11:30 +01:00
parent 3d5739027f
commit 4c247f10a1
2 changed files with 58 additions and 38 deletions

26
buku
View File

@ -2996,21 +2996,23 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None)
def iterate_children(parent_folder, entry_list):
for bm_entry in entry_list:
entry_title = bm_entry['title'] if 'title' in bm_entry else "<no title>"
try:
typeCode = bm_entry['typeCode']
except Exception:
LOGDBG("ff_json: item without typeCode found, ignoring: {}".format(bm_entry['title']))
LOGDBG("ff_json: item without typeCode found, ignoring: {}".format(entry_title))
continue
LOGDBG("ff_json: processing typeCode '{}', title '{}'".format(typeCode, bm_entry['title']))
LOGDBG("ff_json: processing typeCode '{}', title '{}'".format(typeCode, entry_title))
if TypeCode.uri.value == typeCode:
try:
if is_smart(bm_entry):
LOGDBG("ff_json: SmartBookmark found, ignoring: {}".format(bm_entry['title']))
LOGDBG("ff_json: SmartBookmark found, ignoring: {}".format(entry_title))
continue
if is_nongeneric_url(bm_entry['uri']):
LOGDBG("ff_json: Non-Generic URL found, ignoring: {}".format(bm_entry['title']))
LOGDBG("ff_json: Non-Generic URL found, ignoring: {}".format(entry_title))
continue
desc = extract_desc(bm_entry)
@ -3026,26 +3028,24 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None)
formatted_tags = [DELIM + tag for tag in bookmark_tags]
tags = parse_tags(formatted_tags)
LOGDBG("ff_json: Entry found: {}, {}, {}, {} " .format(bm_entry['uri'], bm_entry['title'], tags, desc))
yield (bm_entry['uri'], bm_entry['title'], tags, desc, 0, True, False)
LOGDBG("ff_json: Entry found: {}, {}, {}, {} " .format(bm_entry['uri'], entry_title, tags, desc))
yield (bm_entry['uri'], entry_title, tags, desc, 0, True, False)
except Exception as e:
LOGERR("ff_json: Error parsing entry '{}' Exception '{}'".format(bm_entry['title'], e))
LOGERR("ff_json: Error parsing entry '{}' Exception '{}'".format(entry_title, e))
elif TypeCode.folder.value == typeCode:
title = bm_entry['title'] if 'title' in bm_entry else "<no title>"
# ignore special bookmark folders
if 'root' in bm_entry and bm_entry['root'] in IGNORE_FF_BOOKMARK_FOLDERS:
LOGDBG("ff_json: ignoring root folder: {}" .format(title))
title = None
LOGDBG("ff_json: ignoring root folder: {}" .format(entry_title))
entry_title = None
if "children" in bm_entry:
yield from iterate_children(title, bm_entry['children'])
yield from iterate_children(entry_title, bm_entry['children'])
else:
# if any of the properties does not exist, bail out silently
LOGDBG("ff_json: No 'children' found in bookmark folder - skipping: {}".format(bm_entry['title']))
LOGDBG("ff_json: No 'children' found in bookmark folder - skipping: {}".format(entry_title))
elif TypeCode.separator.value == typeCode:
# ignore separator

View File

@ -271,9 +271,9 @@ def test_load_container_no_title():
assert len(result) == 1
assert result[0][0] == 'http://uri.com'
assert result[0][2] == ','
assert result[0][2] == ',<no title>,'
def test_load_hierarchical_container():
def test_load_hierarchical_container_without_ignore():
"""test method."""
# Arrange
@ -286,18 +286,7 @@ def test_load_hierarchical_container():
"title" : "title",
"typeCode" : 2,
"children": [
{
"title" : "title2",
"typeCode" : 2,
"children": [
{"title":"title1","typeCode":1,"uri":"http://uri1.com/#more-74"},
{"title":"title2","typeCode":1,"uri":"http://uri2.com/xyz"},
{"title":"title3","typeCode":1,"uri":"http://uri3.com"}
]
},
{"title":"title4","typeCode":1,"uri":"http://uri4.com/#more-74"},
{"title":"title5","typeCode":1,"uri":"http://uri5.com/xyz"},
{"title":"title6","typeCode":1,"uri":"http://uri6.com"}
{"title":"title1","typeCode":1,"uri":"http://uri.com"}
]
}]
}
@ -311,20 +300,51 @@ def test_load_hierarchical_container():
for item in items:
result.append(item)
assert len(result) == 6
assert len(result) == 1
assert result[0][0] == 'http://uri.com'
assert result[0][2] == ',title,'
def test_load_hierarchical_container_with_ignore():
"""test method."""
# Arrange
data = json.loads("""
{
"title" : "main",
"typeCode" : 2,
"children": [
{
"title" : "title",
"typeCode" : 2,
"root": "bookmarksMenuFolder",
"children": [
{
"title" : "title2",
"typeCode" : 2,
"children": [
{"title":"title1","typeCode":1,"uri":"http://uri1.com/#more-74"}
]
},
{"title":"title4","typeCode":1,"uri":"http://uri4.com/#more-74"}
]
}]
}
""")
# Act
items = import_firefox_json(data, add_bookmark_folder_as_tag=True)
# Assert
result = []
for item in items:
result.append(item)
assert len(result) == 2
assert result[0][0] == 'http://uri1.com/#more-74'
assert result[1][0] == 'http://uri2.com/xyz'
assert result[2][0] == 'http://uri3.com'
assert result[3][0] == 'http://uri4.com/#more-74'
assert result[4][0] == 'http://uri5.com/xyz'
assert result[5][0] == 'http://uri6.com'
assert result[1][0] == 'http://uri4.com/#more-74'
assert result[0][2] == ',title2,'
assert result[1][2] == ',title2,'
assert result[2][2] == ',title2,'
assert result[3][2] == ','
assert result[4][2] == ','
assert result[5][2] == ','
assert result[1][2] == ','
def test_load_separator():
"""test method."""