1 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """BookmarkModel: python representation of the bookmark model.
7 Obtain one of these from PyUITestSuite::GetBookmarkModel() call.
11 import simplejson
as json
14 class BookmarkModel(object):
16 def __init__(self
, json_string
):
17 """Initialize a BookmarkModel from a string of json.
19 The JSON representation is the same as used by the bookmark model
23 json_string: a string of JSON.
25 self
.bookdict
= json
.loads(json_string
)
27 def BookmarkBar(self
):
28 """Return the bookmark bar node as a dict."""
29 return self
.bookdict
['roots']['bookmark_bar']
32 """Return the 'other' node (e.g. parent of "Other Bookmarks")"""
33 return self
.bookdict
['roots']['other']
35 def NodeCount(self
, node
=None):
36 """Return a count of bookmark nodes, including folders.
38 The root node itself is included in the count.
41 node: the root to start with. If not specified, count all."""
43 return reduce(lambda x
, y
: x
+ y
,
45 for x
in self
.bookdict
['roots'].values()])
47 children
= node
.get('children', None)
49 total
= total
+ reduce(lambda x
,y
: x
+ y
,
50 [self
.NodeCount(x
) for x
in children
])
53 def FindByID(self
, id, nodes
=None):
54 """Find the bookmark by id. Return the dict or None.
57 id: the id to look for.
58 nodes: an iterable of nodes to start with. If not specified, search all.
59 'Not specified' means None, not [].
61 # Careful; we may get an empty list which is different than not
62 # having specified a list.
64 nodes
= self
.bookdict
['roots'].values()
65 # Check each item. If it matches, return. If not, check each of
70 for child
in node
.get('children', []):
71 found_node
= self
.FindByID(id, [child
])
77 def FindByTitle(self
, title
, nodes
=None):
78 """Return a tuple of all nodes which have |title| in their title.
81 title: the title to look for.
82 node: an iterable of nodes to start with. If not specified, search all.
83 'Not specified' means None, not [].
85 # Careful; we may get an empty list which is different than not
86 # having specified a list.
88 nodes
= self
.bookdict
['roots'].values()
89 # Check each item. If it matches, return. If not, check each of
93 node_title
= node
.get('title', None) or node
.get('name', None)
94 if title
== node_title
:
96 # Note we check everything; unlike the FindByID, we do not stop early.
97 for child
in node
.get('children', []):
98 results
+= self
.FindByTitle(title
, [child
])