1# Copyright (c) 2013 The Chromium OS 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
5class ShillTemporaryProfile(object):
6    """Context enclosing the use of a temporary shill profile.  It takes
7    a shill manager dbus object and profile name, and makes sure that
8    this profile is pushed atop the topmost default profile for the duration
9    of this object lifetime."""
10    def __init__(self, manager, profile_name='test'):
11        self._manager = manager
12        self._profile_name = profile_name
13
14
15    def __enter__(self):
16        self._manager.PopAllUserProfiles()
17        try:
18            self._manager.RemoveProfile(self._profile_name)
19        except:
20            pass
21        self._manager.CreateProfile(self._profile_name)
22        self._manager.PushProfile(self._profile_name)
23        return self
24
25
26    def __exit__(self, exception, value, traceback):
27        try:
28            self._manager.PopProfile(self._profile_name)
29            self._manager.RemoveProfile(self._profile_name)
30        except:
31            pass
32