From 578cd28edd0a30864145a6cffe4cef04ef2a898a Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Sun, 30 Dec 2018 15:37:38 +0100 Subject: [PATCH] FIX(buku) correct hierarchical folder handling - fix hierarchical folder handling when adding parent folder as tag: only parent folder is added, no concatenation - handling of folder without title added - tests added for the above --- buku | 17 +++++++++++--- tests/test_import_firefox_json.py | 37 +++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/buku b/buku index ddb3456..7bf01b9 100755 --- a/buku +++ b/buku @@ -2961,7 +2961,8 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) desc = extract_desc(bm_entry) bookmark_tags = extract_tags(bm_entry) - if add_bookmark_folder_as_tag: + # if parent_folder is not "None" + if add_bookmark_folder_as_tag and parent_folder: bookmark_tags.append(parent_folder) if unique_tag: @@ -2979,9 +2980,14 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) try: # from python 3.3 on: # yield from iterate_children(bm_entry['title'], bm_entry['children']) + try: + title = bm_entry['title'] + except Exception: + title = "" - for entry in iterate_children(parent_folder+"/"+bm_entry['title'], bm_entry['children']): + for entry in iterate_children(title, bm_entry['children']): yield entry + except Exception: # if any of the properties does not exist, bail out silently logdbg("No 'children' found in bookmark folder - skipping: {}".format(bm_entry['title'])) @@ -2995,7 +3001,12 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) logerr("No children in Root entry found") return [] - yield from iterate_children("", entry_list) + try: + title = json['title'] + except Exception: + title = None + + yield from iterate_children(title, entry_list) def import_html(html_soup, add_parent_folder_as_tag, newtag): diff --git a/tests/test_import_firefox_json.py b/tests/test_import_firefox_json.py index 75cb806..e8a7884 100644 --- a/tests/test_import_firefox_json.py +++ b/tests/test_import_firefox_json.py @@ -215,6 +215,31 @@ def test_load_many_children(): assert 3 == len(result) +def test_load_container_no_title(): + """test method.""" + + # Arrange + data = json.loads(""" + { + "typeCode" : 2, + "children": [ + {"title":"title1","typeCode":1,"uri":"http://uri.com"} + ] + } + """) + + # Act + items = import_firefox_json(data, add_bookmark_folder_as_tag=True) + + # Assert + result = [] + for item in items: + result.append(item) + + assert 1 == len(result) + assert 'http://uri.com' == result[0][0] + assert ',' == result[0][2] + def test_load_hierarchical_container(): """test method.""" @@ -225,7 +250,7 @@ def test_load_hierarchical_container(): "typeCode" : 2, "children": [ { - "title" : "title", + "title" : "title2", "typeCode" : 2, "children": [ {"title":"title1","typeCode":1,"uri":"http://uri1.com/#more-74"}, @@ -241,7 +266,7 @@ def test_load_hierarchical_container(): """) # Act - items = import_firefox_json(data) + items = import_firefox_json(data, add_bookmark_folder_as_tag=True) # Assert result = [] @@ -256,6 +281,14 @@ def test_load_hierarchical_container(): assert 'http://uri5.com/xyz' == result[4][0] assert 'http://uri6.com' == result[5][0] + assert ',title2,' == result[0][2] + assert ',title2,' == result[1][2] + assert ',title2,' == result[2][2] + assert ',title,' == result[3][2] + assert ',title,' == result[4][2] + assert ',title,' == result[5][2] + + def test_load_separator(): """test method."""