OLD | NEW |
| (Empty) |
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. | |
4 | |
5 """Python representation for Chromium Preferences. | |
6 | |
7 Obtain one of these from a call to PyUITest::GetPrefsInfo() or | |
8 PyUITest::GetLocalStatePrefsInfo(). | |
9 | |
10 Example: | |
11 class MyTest(pyauto.PyUITest): | |
12 def testBasic(self): | |
13 info = self.GetPrefsInfo() # fetch prefs snapshot | |
14 print info.Prefs() # all prefs | |
15 print info.Prefs('session.restore_on_startup') # a single pref | |
16 | |
17 See more tests in chrome/test/functional/prefs.py. | |
18 """ | |
19 | |
20 import simplejson as json | |
21 | |
22 from pyauto_errors import JSONInterfaceError | |
23 | |
24 | |
25 class PrefsInfo(object): | |
26 """Represent info for Chromium preferences. | |
27 | |
28 The info is represented as a hierarchy of prefs values. | |
29 The values could be plain (integer, bool, float) or complex (like | |
30 dictionary, list). | |
31 """ | |
32 def __init__(self, prefs_dict): | |
33 """Initialize a PrefsInfo from a json string. | |
34 | |
35 Args: | |
36 prefs_dict: a dictionary as returned by the IPC command 'GetPrefsInfo'. | |
37 A typical dict representing prefs snapshot looks like: | |
38 { u'prefs': | |
39 { u'alternate_error_pages': {u'enabled': True}, | |
40 u'autofill': { u'auxiliary_profiles_enabled': False, | |
41 u'default_creditcard': u'', | |
42 u'default_profile': u'', | |
43 u'enabled': True, | |
44 u'infobar_shown': False, | |
45 u'negative_upload_rate': 0.01, | |
46 u'positive_upload_rate': 0.01}, | |
47 u'bookmark_bar': {u'show_on_all_tabs': False}, | |
48 ... | |
49 ... | |
50 } | |
51 } | |
52 | |
53 Raises: | |
54 pyauto_errors.JSONInterfaceError if the automation call returns an error. | |
55 """ | |
56 # JSON string prepared in PrefsInfo() in automation_provider.cc | |
57 self.prefsdict = prefs_dict | |
58 if self.prefsdict.has_key('error'): | |
59 raise JSONInterfaceError(self.prefsdict['error']) | |
60 | |
61 def Prefs(self, path=None): | |
62 """Get preferences. | |
63 | |
64 The preference dictionary (when using path=None) looks like: | |
65 | |
66 { u'alternate_error_pages': {u'enabled': True}, | |
67 u'autofill': { u'auxiliary_profiles_enabled': False, | |
68 u'default_creditcard': u'', | |
69 u'default_profile': u'', | |
70 u'enabled': True, | |
71 u'infobar_shown': False, | |
72 u'negative_upload_rate': 0.01, | |
73 u'positive_upload_rate': 0.01}, | |
74 u'bookmark_bar': {u'show_on_all_tabs': False}, | |
75 ... | |
76 ... | |
77 } | |
78 | |
79 In this case, to fetch the preference value for autofill enabled, use | |
80 'autofill.enabled' as the path. | |
81 | |
82 Args: | |
83 path: If specified, return the preference item for the given path. | |
84 path is a dot-separated string like "session.restore_on_startup". | |
85 One of the equivalent names in chrome/common/pref_names.h could | |
86 also be used. | |
87 | |
88 Returns: | |
89 preference value. It could be a dictionary (like the example above), a | |
90 list or a plain value. | |
91 None, if prefernece for path not found (if path is given). | |
92 """ | |
93 all = self.prefsdict.get('prefs', {}) | |
94 if not path: # No path given. Return all prefs. | |
95 return all | |
96 for part in path.split('.'): # Narrow down to the requested prefs path. | |
97 all = all.get(part) | |
98 if all is None: return None | |
99 return all | |
OLD | NEW |