From da21c5e7e8e63de6a07207ffd53ec4481458ab0e Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Sun, 30 Dec 2018 14:12:49 +0100 Subject: [PATCH 1/8] FIX(buku) ff json import folder w/o children - fixes jarun/Buku#340 - replaced error with warning - proper warning message added - test added --- buku | 6 +++--- tests/test_import_firefox_json.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/buku b/buku index 090ece5..ddb3456 100755 --- a/buku +++ b/buku @@ -2973,7 +2973,7 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) logdbg("Entry found: {}, {}, {}, {} ".format(bm_entry['uri'], bm_entry['title'], tags, desc)) yield (bm_entry['uri'], bm_entry['title'], tags, desc, 0, True, False) except Exception as e: - logerr(e) + logerr("Error parsing entry '{}' Exception '{}'".format(bm_entry['title'], e)) elif TypeCode.folder.value == typeCode: try: @@ -2982,9 +2982,9 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) for entry in iterate_children(parent_folder+"/"+bm_entry['title'], bm_entry['children']): yield entry - except Exception as e: + except Exception: # if any of the properties does not exist, bail out silently - logerr(e) + logdbg("No 'children' found in bookmark folder - skipping: {}".format(bm_entry['title'])) elif TypeCode.separator.value == typeCode: logdbg("Unknonw typeCode found : {}".format(typeCode)) diff --git a/tests/test_import_firefox_json.py b/tests/test_import_firefox_json.py index 3919830..75cb806 100644 --- a/tests/test_import_firefox_json.py +++ b/tests/test_import_firefox_json.py @@ -110,6 +110,25 @@ def test_load_invalid_typecode(): assert 0 == len(result) +def test_load_folder_with_no_children(): + """test method.""" + + # Arrange + data = json.loads(""" + { + "title" : "title", + "typeCode" : 2 + } """) + + # Act + items = import_firefox_json(data) + + # Assert + result = [] + for item in items: + result.append(item) + + assert 0 == len(result) def test_load_one_child(): """test method.""" From 578cd28edd0a30864145a6cffe4cef04ef2a898a Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Sun, 30 Dec 2018 15:37:38 +0100 Subject: [PATCH 2/8] 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.""" From 727141b5e240071460294bde624e51481c01d310 Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Sun, 30 Dec 2018 14:12:49 +0100 Subject: [PATCH 3/8] FIX(buku) ff json import folder w/o children - fixes jarun/Buku#340 - replaced error with warning - proper warning message added - test added --- buku | 6 +++--- tests/test_import_firefox_json.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/buku b/buku index a9aaf88..337364b 100755 --- a/buku +++ b/buku @@ -3027,7 +3027,7 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) .format(bm_entry['uri'], bm_entry['title'], tags, desc)) yield (bm_entry['uri'], bm_entry['title'], tags, desc, 0, True, False) except Exception as e: - LOGERR(e) + LOGERR("Error parsing entry '{}' Exception '{}'".format(bm_entry['title'], e)) elif TypeCode.folder.value == typeCode: try: @@ -3037,9 +3037,9 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) for entry in iterate_children( parent_folder+"/"+bm_entry['title'], bm_entry['children']): yield entry - except Exception as e: + except Exception: # if any of the properties does not exist, bail out silently - LOGERR(e) + LOGDBG("No 'children' found in bookmark folder - skipping: {}".format(bm_entry['title'])) elif TypeCode.separator.value == typeCode: LOGDBG("Unknown typeCode found : {}".format(typeCode)) diff --git a/tests/test_import_firefox_json.py b/tests/test_import_firefox_json.py index b068330..44efd66 100644 --- a/tests/test_import_firefox_json.py +++ b/tests/test_import_firefox_json.py @@ -110,6 +110,25 @@ def test_load_invalid_typecode(): assert len(result) == 0 +def test_load_folder_with_no_children(): + """test method.""" + + # Arrange + data = json.loads(""" + { + "title" : "title", + "typeCode" : 2 + } """) + + # Act + items = import_firefox_json(data) + + # Assert + result = [] + for item in items: + result.append(item) + + assert 0 == len(result) def test_load_one_child(): """test method.""" From 9159e148d91c3e3adba1c02fb50c329dcd83981b Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Sun, 30 Dec 2018 15:37:38 +0100 Subject: [PATCH 4/8] 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 | 18 +++++++++++---- tests/test_import_firefox_json.py | 37 +++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/buku b/buku index 337364b..c3d9f56 100755 --- a/buku +++ b/buku @@ -3014,7 +3014,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: @@ -3033,10 +3034,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'])) @@ -3050,7 +3055,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 44efd66..45a0b30 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 len(result) == 3 +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 result[4][0] == 'http://uri5.com/xyz' assert result[5][0] == 'http://uri6.com' + 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.""" From c32cf5911bad4b836713179d025eb1cc7fbe3c62 Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Wed, 2 Jan 2019 12:35:59 +0100 Subject: [PATCH 5/8] FIX(buku) pylint errors in ff json import tests --- tests/test_import_firefox_json.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_import_firefox_json.py b/tests/test_import_firefox_json.py index 4ee2e6b..05aced1 100644 --- a/tests/test_import_firefox_json.py +++ b/tests/test_import_firefox_json.py @@ -139,7 +139,7 @@ def test_load_folder_with_no_children(): for item in items: result.append(item) - assert 0 == len(result) + assert len(result) == 0 def test_load_one_child(): """test method.""" @@ -269,9 +269,9 @@ def test_load_container_no_title(): for item in items: result.append(item) - assert 1 == len(result) - assert 'http://uri.com' == result[0][0] - assert ',' == result[0][2] + assert len(result) == 1 + assert result[0][0] == 'http://uri.com' + assert result[0][2] == ',' def test_load_hierarchical_container(): """test method.""" @@ -319,12 +319,12 @@ def test_load_hierarchical_container(): assert result[4][0] == 'http://uri5.com/xyz' assert result[5][0] == 'http://uri6.com' - assert ',title2,' == result[0][2] - assert ',title2,' == result[1][2] - assert ',title2,' == result[2][2] - assert ',' == result[3][2] - assert ',' == result[4][2] - assert ',' == result[5][2] + 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] == ',' def test_load_separator(): """test method.""" From b5844ee1c12d89f210e39d17e876434a72a01f56 Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Wed, 2 Jan 2019 16:11:21 +0100 Subject: [PATCH 6/8] REF(buku) ff json import improve debug logging --- buku | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/buku b/buku index c8faa97..ee3c319 100755 --- a/buku +++ b/buku @@ -2428,7 +2428,7 @@ class BukuDb: items = import_firefox_json(data, add_bookmark_folder_as_tag, newtag) except ValueError as e: - LOGERR("JSON Decode Error: {}".format(e)) + LOGERR("ff_json: JSON Decode Error: {}".format(e)) return False except Exception as e: LOGERR(e) @@ -2981,7 +2981,7 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) ] return d[0]['value'] except Exception: - LOGDBG("No description found for entry: {} {}".format(entry['uri'], entry['title'])) + LOGDBG("ff_json: No description found for entry: {} {}".format(entry['uri'], entry['title'])) return "" def extract_tags(entry): @@ -2989,7 +2989,7 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) try: tags = entry['tags'].split(',') except Exception: - LOGDBG("No tags found for entry: {} {}".format(entry['uri'], entry['title'])) + LOGDBG("ff_json: No tags found for entry: {} {}".format(entry['uri'], entry['title'])) return tags @@ -2998,17 +2998,18 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) try: typeCode = bm_entry['typeCode'] except Exception: - LOGDBG("item without typeCode found, ignoring: {}".format(bm_entry['title'])) + LOGDBG("ff_json: item without typeCode found, ignoring: {}".format(bm_entry['title'])) continue + LOGDBG("ff_json: processing typeCode '{}', title '{}'".format(typeCode, bm_entry['title'])) if TypeCode.uri.value == typeCode: try: if is_smart(bm_entry): - LOGDBG("SmartBookmark found, ignoring: {}".format(bm_entry['title'])) + LOGDBG("ff_json: SmartBookmark found, ignoring: {}".format(bm_entry['title'])) continue if is_nongeneric_url(bm_entry['uri']): - LOGDBG("Non-Generic URL found, ignoring: {}".format(bm_entry['title'])) + LOGDBG("ff_json: Non-Generic URL found, ignoring: {}".format(bm_entry['title'])) continue desc = extract_desc(bm_entry) @@ -3024,11 +3025,11 @@ 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("Entry found: {}, {}, {}, {} " - .format(bm_entry['uri'], bm_entry['title'], tags, desc)) + 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) + except Exception as e: - LOGERR("Error parsing entry '{}' Exception '{}'".format(bm_entry['title'], e)) + LOGERR("ff_json: Error parsing entry '{}' Exception '{}'".format(bm_entry['title'], e)) elif TypeCode.folder.value == typeCode: try: @@ -3041,28 +3042,34 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) 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'])) + LOGDBG("ff_json: No 'children' found in bookmark folder - skipping: {}".format(bm_entry['title'])) elif TypeCode.separator.value == typeCode: - LOGDBG("Unknown typeCode found : {}".format(typeCode)) + LOGDBG("ff_json: Unknown typeCode found : {}".format(typeCode)) try: main_entry_list = json['children'] except Exception: - LOGERR("No children in Root entry found") + LOGDBG("ff_json: No children in Root entry found") return [] # interate over each main bookmark container, ignoring main container title # assuming all entries with 'children' are containers, skipping typeCode test for main_container in main_entry_list: try: - yield from iterate_children(None, main_container['children']) + main_container_title = main_container['title'] except Exception: - try: - title = main_container['title'] - except Exception: - title = None - LOGDBG("No 'children' found in main bookmark folder - skipping: {}".format(title)) + main_container_title = "
" + + try: + main_container_children = main_container['children'] + except Exception: + main_container_children = None + LOGDBG("ff_json: No 'children' found in main bookmark folder - skipping: {}".format(main_container_title)) + + if main_container_children: + LOGDBG("ff_json: main bookmark folder : {}".format(main_container_title)) + yield from iterate_children(None, main_container_children) def import_html(html_soup, add_parent_folder_as_tag, newtag): From 3d5739027f2579968e1df167ce3a714ff5f0f512 Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Wed, 2 Jan 2019 21:50:01 +0100 Subject: [PATCH 7/8] FIX(buku) ff json import mimics html import - json import ignores "Bookmark Menu" folder for tagging - all other folders are used if parent folder as tags is set - behavior follows html export/import --- buku | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) mode change 100755 => 100644 buku diff --git a/buku b/buku old mode 100755 new mode 100644 index ee3c319..3c5f393 --- a/buku +++ b/buku @@ -81,6 +81,7 @@ USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Fi MYHEADERS = None # Default dictionary of headers MYPROXY = None # Default proxy TEXT_BROWSERS = ['elinks', 'links', 'links2', 'lynx', 'w3m', 'www-browser'] +IGNORE_FF_BOOKMARK_FOLDERS = frozenset(["placesRoot", "bookmarksMenuFolder"]) # Set up logging LOGGER = logging.getLogger() @@ -3032,44 +3033,33 @@ def import_firefox_json(json, add_bookmark_folder_as_tag=False, unique_tag=None) LOGERR("ff_json: Error parsing entry '{}' Exception '{}'".format(bm_entry['title'], e)) elif TypeCode.folder.value == typeCode: - try: - try: - title = bm_entry['title'] - except Exception: - title = "" + title = bm_entry['title'] if 'title' in bm_entry else "" + + # 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 + + if "children" in bm_entry: yield from iterate_children(title, bm_entry['children']) - - except Exception: + 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'])) elif TypeCode.separator.value == typeCode: + # ignore separator + pass + else: LOGDBG("ff_json: Unknown typeCode found : {}".format(typeCode)) - try: + if "children" in json: main_entry_list = json['children'] - except Exception: + else: LOGDBG("ff_json: No children in Root entry found") return [] - # interate over each main bookmark container, ignoring main container title - # assuming all entries with 'children' are containers, skipping typeCode test - for main_container in main_entry_list: - try: - main_container_title = main_container['title'] - except Exception: - main_container_title = "
" - - try: - main_container_children = main_container['children'] - except Exception: - main_container_children = None - LOGDBG("ff_json: No 'children' found in main bookmark folder - skipping: {}".format(main_container_title)) - - if main_container_children: - LOGDBG("ff_json: main bookmark folder : {}".format(main_container_title)) - yield from iterate_children(None, main_container_children) + yield from iterate_children(None, main_entry_list) def import_html(html_soup, add_parent_folder_as_tag, newtag): From 4c247f10a16bf9eeef82627e575840ad809536d7 Mon Sep 17 00:00:00 2001 From: Chris Drexler Date: Wed, 2 Jan 2019 22:11:30 +0100 Subject: [PATCH 8/8] 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 --- buku | 26 ++++++------ tests/test_import_firefox_json.py | 70 ++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 38 deletions(-) diff --git a/buku b/buku index 3c5f393..1a83b98 100644 --- a/buku +++ b/buku @@ -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 "" + 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 "" - # 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 diff --git a/tests/test_import_firefox_json.py b/tests/test_import_firefox_json.py index 05aced1..7eb8bbf 100644 --- a/tests/test_import_firefox_json.py +++ b/tests/test_import_firefox_json.py @@ -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] == ',,' -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."""