Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: gslib/plurality_checkable_iterator.py

Issue 698893003: Update checked in version of gsutil to version 4.6 (Closed) Base URL: http://dart.googlecode.com/svn/third_party/gsutil/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gslib/parallelism_framework_util.py ('k') | gslib/progress_callback.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # -*- coding: utf-8 -*-
1 # Copyright 2012 Google Inc. All Rights Reserved. 2 # Copyright 2012 Google Inc. All Rights Reserved.
2 # 3 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License. 5 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at 6 # You may obtain a copy of the License at
6 # 7 #
7 # http://www.apache.org/licenses/LICENSE-2.0 8 # http://www.apache.org/licenses/LICENSE-2.0
8 # 9 #
9 # Unless required by applicable law or agreed to in writing, software 10 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and 13 # See the License for the specific language governing permissions and
13 # limitations under the License. 14 # limitations under the License.
15 """Iterator wrapper for checking wrapped iterator's emptiness or plurality."""
14 16
15 """ 17 # TODO: Here and elsewhere (wildcard_iterator, name_expansion), do not reference
16 Iterator wrapper that allows you to check whether the wrapped iterator 18 # __iter__ directly because it causes the first element to be instantiated.
17 is empty and whether it has more than 1 element. 19 # Instead, implement __iter__ as a return self and implement the next() function
18 """ 20 # which returns (not yields) the values. This necessitates that in the case
21 # of the iterator classes, the iterator is used once per class instantiation
22 # so that next() calls do not collide, but this semantic has been long-assumed
23 # by the iterator classes for the use of __iter__ anyway.
24
25 from __future__ import absolute_import
26
27 import sys
28
19 29
20 class PluralityCheckableIterator(object): 30 class PluralityCheckableIterator(object):
31 """Iterator wrapper class.
32
33 Allows you to check whether the wrapped iterator is empty and
34 whether it has more than 1 element. This iterator accepts three types of
35 values from the iterator it wraps:
36 1. A yielded element (this is the normal case).
37 2. A raised exception, which will be buffered and re-raised when it
38 is reached in this iterator.
39 3. A yielded tuple of (exception, stack trace), which will be buffered
40 and raised with it is reached in this iterator.
41 """
21 42
22 def __init__(self, it): 43 def __init__(self, it):
23 self.it = it.__iter__() 44 # Need to get the iterator function here so that we don't immediately
45 # instantiate the first element (which could raise an exception).
46 self.orig_iterator = it
47 self.base_iterator = None
24 self.head = [] 48 self.head = []
49 self.underlying_iter_empty = False
25 # Populate first 2 elems into head so we can check whether iterator has 50 # Populate first 2 elems into head so we can check whether iterator has
26 # more than 1 item. 51 # more than 1 item.
27 for i in range(0, 2): 52 for _ in range(0, 2):
28 self.__populate_head__() 53 self._PopulateHead()
29 54
30 def __populate_head__(self): 55 def _PopulateHead(self):
31 try: 56 if not self.underlying_iter_empty:
32 e = self.it.next() 57 try:
33 self.underlying_iter_empty = False 58 if not self.base_iterator:
34 self.head.append(e) 59 self.base_iterator = iter(self.orig_iterator)
35 except StopIteration: 60 e = self.base_iterator.next()
36 # Indicates we can no longer call next() on underlying iterator, but 61 self.underlying_iter_empty = False
37 # there could still be elements left to iterate in head. 62 if isinstance(e, tuple) and isinstance(e[0], Exception):
38 self.underlying_iter_empty = True 63 self.head.append(('exception', e[0], e[1]))
64 else:
65 self.head.append(('element', e))
66 except StopIteration:
67 # Indicates we can no longer call next() on underlying iterator, but
68 # there could still be elements left to iterate in head.
69 self.underlying_iter_empty = True
70 except Exception, e:
71 # Buffer the exception and raise it when the element is accessed.
72 # Also, preserve the original stack trace, as the stack trace from
73 # within plurality_checkable_iterator.next is not very useful.
74 self.head.append(('exception', e, sys.exc_info()[2]))
39 75
40 def __iter__(self): 76 def __iter__(self):
41 while len(self.head) > 0: 77 return self
42 yield self.next()
43 else:
44 raise StopIteration()
45 78
46 def next(self): 79 def next(self):
47 # Backfill into head each time we pop an element so we can always check 80 # Backfill into head each time we pop an element so we can always check
48 # for emptiness and for has_plurality(). 81 # for emptiness and for HasPlurality().
49 self.__populate_head__() 82 while self.head:
50 return self.head.pop(0) 83 self._PopulateHead()
84 item_tuple = self.head.pop(0)
85 if item_tuple[0] == 'element':
86 return item_tuple[1]
87 else: # buffered exception
88 raise item_tuple[1].__class__, item_tuple[1], item_tuple[2]
89 raise StopIteration()
51 90
52 def is_empty(self): 91 def IsEmpty(self):
53 return len(self.head) == 0 92 return not self.head
54 93
55 def has_plurality(self): 94 def HasPlurality(self):
56 return len(self.head) > 1 95 return len(self.head) > 1
OLDNEW
« no previous file with comments | « gslib/parallelism_framework_util.py ('k') | gslib/progress_callback.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698