Skip to content

Commit d60a679

Browse files
committed
Scrape in six used modules from apitools.
Ignore them for pep8/pylint purposes until after refactoring/cleanup.
1 parent eb3d390 commit d60a679

File tree

8 files changed

+1877
-1
lines changed

8 files changed

+1877
-1
lines changed

gcloud/_apitools/buffered_stream.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
"""Small helper class to provide a small slice of a stream.
3+
4+
This class reads ahead to detect if we are at the end of the stream.
5+
"""
6+
7+
from apitools.base.py import exceptions
8+
9+
10+
# TODO(user): Consider replacing this with a StringIO.
11+
class BufferedStream(object):
12+
13+
"""Buffers a stream, reading ahead to determine if we're at the end."""
14+
15+
def __init__(self, stream, start, size):
16+
self.__stream = stream
17+
self.__start_pos = start
18+
self.__buffer_pos = 0
19+
self.__buffered_data = self.__stream.read(size)
20+
self.__stream_at_end = len(self.__buffered_data) < size
21+
self.__end_pos = self.__start_pos + len(self.__buffered_data)
22+
23+
def __str__(self):
24+
return ('Buffered stream %s from position %s-%s with %s '
25+
'bytes remaining' % (self.__stream, self.__start_pos,
26+
self.__end_pos, self._bytes_remaining))
27+
28+
def __len__(self):
29+
return len(self.__buffered_data)
30+
31+
@property
32+
def stream_exhausted(self):
33+
return self.__stream_at_end
34+
35+
@property
36+
def stream_end_position(self):
37+
return self.__end_pos
38+
39+
@property
40+
def _bytes_remaining(self):
41+
return len(self.__buffered_data) - self.__buffer_pos
42+
43+
def read(self, size=None): # pylint: disable=invalid-name
44+
"""Reads from the buffer."""
45+
if size is None or size < 0:
46+
raise exceptions.NotYetImplementedError(
47+
'Illegal read of size %s requested on BufferedStream. '
48+
'Wrapped stream %s is at position %s-%s, '
49+
'%s bytes remaining.' %
50+
(size, self.__stream, self.__start_pos, self.__end_pos,
51+
self._bytes_remaining))
52+
53+
data = ''
54+
if self._bytes_remaining:
55+
size = min(size, self._bytes_remaining)
56+
data = self.__buffered_data[
57+
self.__buffer_pos:self.__buffer_pos + size]
58+
self.__buffer_pos += size
59+
return data

gcloud/_apitools/exceptions.py

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env python
2+
"""Exceptions for generated client libraries."""
3+
4+
5+
class Error(Exception):
6+
7+
"""Base class for all exceptions."""
8+
9+
10+
class TypecheckError(Error, TypeError):
11+
12+
"""An object of an incorrect type is provided."""
13+
14+
15+
class NotFoundError(Error):
16+
17+
"""A specified resource could not be found."""
18+
19+
20+
class UserError(Error):
21+
22+
"""Base class for errors related to user input."""
23+
24+
25+
class InvalidDataError(Error):
26+
27+
"""Base class for any invalid data error."""
28+
29+
30+
class CommunicationError(Error):
31+
32+
"""Any communication error talking to an API server."""
33+
34+
35+
class HttpError(CommunicationError):
36+
37+
"""Error making a request. Soon to be HttpError."""
38+
39+
def __init__(self, response, content, url):
40+
super(HttpError, self).__init__()
41+
self.response = response
42+
self.content = content
43+
self.url = url
44+
45+
def __str__(self):
46+
content = self.content.decode('ascii', 'replace')
47+
return 'HttpError accessing <%s>: response: <%s>, content <%s>' % (
48+
self.url, self.response, content)
49+
50+
@property
51+
def status_code(self):
52+
# TODO(craigcitro): Turn this into something better than a
53+
# KeyError if there is no status.
54+
return int(self.response['status'])
55+
56+
@classmethod
57+
def FromResponse(cls, http_response):
58+
return cls(http_response.info, http_response.content,
59+
http_response.request_url)
60+
61+
62+
class InvalidUserInputError(InvalidDataError):
63+
64+
"""User-provided input is invalid."""
65+
66+
67+
class InvalidDataFromServerError(InvalidDataError, CommunicationError):
68+
69+
"""Data received from the server is malformed."""
70+
71+
72+
class BatchError(Error):
73+
74+
"""Error generated while constructing a batch request."""
75+
76+
77+
class ConfigurationError(Error):
78+
79+
"""Base class for configuration errors."""
80+
81+
82+
class GeneratedClientError(Error):
83+
84+
"""The generated client configuration is invalid."""
85+
86+
87+
class ConfigurationValueError(UserError):
88+
89+
"""Some part of the user-specified client configuration is invalid."""
90+
91+
92+
class ResourceUnavailableError(Error):
93+
94+
"""User requested an unavailable resource."""
95+
96+
97+
class CredentialsError(Error):
98+
99+
"""Errors related to invalid credentials."""
100+
101+
102+
class TransferError(CommunicationError):
103+
104+
"""Errors related to transfers."""
105+
106+
107+
class TransferRetryError(TransferError):
108+
109+
"""Retryable errors related to transfers."""
110+
111+
112+
class TransferInvalidError(TransferError):
113+
114+
"""The given transfer is invalid."""
115+
116+
117+
class RequestError(CommunicationError):
118+
119+
"""The request was not successful."""
120+
121+
122+
class RetryAfterError(HttpError):
123+
124+
"""The response contained a retry-after header."""
125+
126+
def __init__(self, response, content, url, retry_after):
127+
super(RetryAfterError, self).__init__(response, content, url)
128+
self.retry_after = int(retry_after)
129+
130+
@classmethod
131+
def FromResponse(cls, http_response):
132+
return cls(http_response.info, http_response.content,
133+
http_response.request_url, http_response.retry_after)
134+
135+
136+
class BadStatusCodeError(HttpError):
137+
138+
"""The request completed but returned a bad status code."""
139+
140+
141+
class NotYetImplementedError(GeneratedClientError):
142+
143+
"""This functionality is not yet implemented."""
144+
145+
146+
class StreamExhausted(Error):
147+
148+
"""Attempted to read more bytes from a stream than were available."""

0 commit comments

Comments
 (0)