Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / future.py
blob0e9ef24317d36464cb237387d8cdc1be824d97be
1 # Copyright (c) 2012 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 import sys
7 _no_value = object()
10 class Gettable(object):
11 '''Allows a Future to accept a callable as a delegate. Wraps |f| in a .Get
12 interface required by Future.
13 '''
14 def __init__(self, f, *args):
15 self._g = lambda: f(*args)
16 def Get(self):
17 return self._g()
20 class Future(object):
21 '''Stores a value, error, or delegate to be used later.
22 '''
23 def __init__(self, value=_no_value, delegate=None, exc_info=None):
24 self._value = value
25 self._delegate = delegate
26 self._exc_info = exc_info
27 if (self._value is _no_value and
28 self._delegate is None and
29 self._exc_info is None):
30 raise ValueError('Must have either a value, error, or delegate.')
32 def Get(self):
33 '''Gets the stored value, error, or delegate contents.
34 '''
35 if self._value is not _no_value:
36 return self._value
37 if self._exc_info is not None:
38 self._Raise()
39 try:
40 self._value = self._delegate.Get()
41 return self._value
42 except:
43 self._exc_info = sys.exc_info()
44 self._Raise()
46 def _Raise(self):
47 exc_info = self._exc_info
48 raise exc_info[0], exc_info[1], exc_info[2]