1 |
commit: 1b23a4134f0919e9541e7544213321efc51cac81 |
2 |
Author: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Apr 25 01:09:54 2016 +0000 |
4 |
Commit: Brian Dolbec <dolsen <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Apr 25 15:28:53 2016 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=1b23a413 |
7 |
|
8 |
portage: Create a new extended futures class which adds standard data type access |
9 |
|
10 |
This new class adds standard get, set functions along with optional defaults. |
11 |
It also adds the capability to ignore InvalidStateErrors when trying to set the reslt more |
12 |
than once. |
13 |
|
14 |
pym/portage/util/futures/extendedfutures.py | 73 +++++++++++++++++++++++++++++ |
15 |
1 file changed, 73 insertions(+) |
16 |
|
17 |
diff --git a/pym/portage/util/futures/extendedfutures.py b/pym/portage/util/futures/extendedfutures.py |
18 |
new file mode 100644 |
19 |
index 0000000..af384c7 |
20 |
--- /dev/null |
21 |
+++ b/pym/portage/util/futures/extendedfutures.py |
22 |
@@ -0,0 +1,73 @@ |
23 |
+# Copyright 2016 Gentoo Foundation |
24 |
+# Distributed under the terms of the GNU General Public License v2 |
25 |
+# |
26 |
+# This module provides an extended subset of the asyncio.futures.Futures |
27 |
+# interface. |
28 |
+ |
29 |
+from __future__ import unicode_literals |
30 |
+ |
31 |
+__all__ = ( |
32 |
+ 'CancelledError', |
33 |
+ 'ExtendedFuture', |
34 |
+ 'InvalidStateError', |
35 |
+) |
36 |
+ |
37 |
+from portage.util.futures.futures import (Future, InvalidStateError, |
38 |
+ CancelledError) |
39 |
+ |
40 |
+# Create our one time settable unset constant |
41 |
+UNSET_CONST = Future() |
42 |
+UNSET_CONST.set_result(object()) |
43 |
+ |
44 |
+ |
45 |
+class ExtendedFuture(Future): |
46 |
+ '''Extended Future class adding convienince get and set operations with |
47 |
+ default result capabilities for unset result(). It also adds pass |
48 |
+ capability for duplicate set_result() calls. |
49 |
+ ''' |
50 |
+ |
51 |
+ def __init__(self, default_result=UNSET_CONST.result()): |
52 |
+ '''Class init |
53 |
+ |
54 |
+ @param default_result: Optional data type/value to return in the event |
55 |
+ of a result() call when result has not yet been |
56 |
+ set. |
57 |
+ ''' |
58 |
+ self.default_result = default_result |
59 |
+ super(ExtendedFuture, self).__init__() |
60 |
+ self.set = self.set_result |
61 |
+ |
62 |
+ def set_result(self, data, ignore_InvalidState=False): |
63 |
+ '''Set the Future's result to the data, optionally don't raise |
64 |
+ an error for 'InvalidStateError' errors |
65 |
+ |
66 |
+ @param ignore_exception: Boolean |
67 |
+ ''' |
68 |
+ if ignore_InvalidState: |
69 |
+ try: |
70 |
+ super(ExtendedFuture, self).set_result(data) |
71 |
+ except InvalidStateError: |
72 |
+ pass |
73 |
+ else: |
74 |
+ super(ExtendedFuture, self).set_result(data) |
75 |
+ |
76 |
+ def get(self, default=UNSET_CONST.result()): |
77 |
+ '''Convienience function to wrap result() but adds an optional |
78 |
+ default value to return rather than raise an InvalidStateError |
79 |
+ |
80 |
+ @param default: Optional override for the classwide default_result |
81 |
+ @returns: the result data or the default value, raisies an exception |
82 |
+ if result is unset and no default is defined. |
83 |
+ ''' |
84 |
+ if default is not UNSET_CONST.result(): |
85 |
+ pass |
86 |
+ elif self.default_result is not UNSET_CONST.result(): |
87 |
+ default = self.default_result |
88 |
+ if default is not UNSET_CONST.result(): |
89 |
+ try: |
90 |
+ data = super(ExtendedFuture, self).result() |
91 |
+ except InvalidStateError: |
92 |
+ data = default |
93 |
+ else: |
94 |
+ data = super(ExtendedFuture, self).result() |
95 |
+ return data |