blob: 958df62b7470e5714840a144cae0e706d17d33a1 [file] [log] [blame]
Ben Chanad9da222013-05-08 14:41:20 -07001# 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
5"""A collection of context managers for working with shill objects."""
6
7import logging
8import shill_proxy
9
10
11class ContextError(Exception):
12 """An error raised by a context managers dealing with shill objects."""
13 pass
14
15
16class ServiceAutoConnectContext(object):
17 """A context manager for overriding a service's 'AutoConnect' property.
18
19 As the service object of the same service may change during the lifetime
20 of the context, this context manager does not take a service object at
21 construction. Instead, it takes a |get_service| function at construction,
22 which it invokes to obtain a service object when entering and exiting the
23 context. It is assumed that |get_service| always returns a service object
24 that refers to the same service.
25
26 Usage:
27 def get_service():
28 # Some function that returns a service object.
29
30 with ServiceAutoConnectContext(get_service, False):
31 # Within this context, the 'AutoConnect' property of the service
32 # returned by |get_service| is temporarily set to False if it's
33 # initially set to True. The property is restored to its initial
34 # value after the context ends.
35
36 """
37 def __init__(self, get_service, autoconnect):
38 self._get_service = get_service
39 self._autoconnect = autoconnect
40 self._initial_autoconnect = None
41
42
43 def __enter__(self):
44 service = self._get_service()
45 if service is None:
46 raise ContextError('Could not obtain a service object.')
47
48 service_properties = service.GetProperties()
Prathmesh Prabhu6c1fca52013-07-03 13:38:58 -070049 self._initial_autoconnect = shill_proxy.ShillProxy.dbus2primitive(
Ben Chanad9da222013-05-08 14:41:20 -070050 service_properties[
51 shill_proxy.ShillProxy.SERVICE_PROPERTY_AUTOCONNECT])
52 if self._initial_autoconnect != self._autoconnect:
53 logging.info('ServiceAutoConnectContext: change autoconnect to %s',
54 self._autoconnect)
55 service.SetProperty(
56 shill_proxy.ShillProxy.SERVICE_PROPERTY_AUTOCONNECT,
57 self._autoconnect)
58 else:
59 logging.info('ServiceAutoConnectContext: autoconnect remains %s',
60 self._initial_autoconnect)
61 return self
62
63
64 def __exit__(self, exc_type, exc_value, traceback):
65 if self._initial_autoconnect != self._autoconnect:
66 service = self._get_service()
67 if service is None:
68 raise ContextError('Could not obtain a service object.')
69
70 logging.info('ServiceAutoConnectContext: restore autoconnect to %s',
71 self._initial_autoconnect)
72 service.SetProperty(
73 shill_proxy.ShillProxy.SERVICE_PROPERTY_AUTOCONNECT,
74 self._initial_autoconnect)
75 return False
76
77
78 @property
79 def autoconnect(self):
80 """AutoConnect property value within this context."""
81 return self._autoconnect
82
83
84 @property
85 def initial_autoconnect(self):
86 """Initial AutoConnect property value when entering this context."""
87 return self._initial_autoconnect