python类exception()的实例源码

awsqueryservice.py 文件源码 项目:depot_tools 作者: webrtc-uwp 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def __init__(self, **args):
        self.args = args
        self.check_for_credential_file()
        self.check_for_env_url()
        if 'host' not in self.args:
            if self.Regions:
                region_name = self.args.get('region_name',
                                            self.Regions[0]['name'])
                for region in self.Regions:
                    if region['name'] == region_name:
                        self.args['host'] = region['endpoint']
        if 'path' not in self.args:
            self.args['path'] = self.Path
        if 'port' not in self.args:
            self.args['port'] = self.Port
        try:
            boto.connection.AWSQueryConnection.__init__(self, **self.args)
            self.aws_response = None
        except boto.exception.NoAuthHandlerFound:
            raise NoCredentialsError()
wait.py 文件源码 项目:LIS-Tempest 作者: LIS 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def wait_exception(lfunction):
    """Returns with the exception or raises one."""
    start_time = time.time()
    while True:
        try:
            lfunction()
        except BaseException as exc:
            LOG.info('Exception in %d second',
                     time.time() - start_time)
            return exc
        dtime = time.time() - start_time
        if dtime > CONF.boto.build_timeout:
            raise TestCase.failureException("Wait timeout exceeded! (%ds)" %
                                            dtime)
        time.sleep(CONF.boto.build_interval)

# TODO(afazekas): consider strategy design pattern..
rgw.py 文件源码 项目:DeepSea 作者: SUSE 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def create_bucket(**kwargs):
    """
    Create a bucket for a user
    """
    s3conn = s3connect(kwargs['user'])
    try:
        s3conn.create_bucket(kwargs['bucket_name'])
    except boto.exception.S3CreateError:
        return False
    return True
connection.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def _make_request(self, action, params=None):
        """Make a call to the SES API.

        :type action: string
        :param action: The API method to use (e.g. SendRawEmail)

        :type params: dict
        :param params: Parameters that will be sent as POST data with the API
            call.
        """
        ct = 'application/x-www-form-urlencoded; charset=UTF-8'
        headers = {'Content-Type': ct}
        params = params or {}
        params['Action'] = action

        for k, v in params.items():
            if isinstance(v, six.text_type):  # UTF-8 encode only if it's Unicode
                params[k] = v.encode('utf-8')

        response = super(SESConnection, self).make_request(
            'POST',
            '/',
            headers=headers,
            data=urllib.parse.urlencode(params)
        )
        body = response.read().decode('utf-8')
        if response.status == 200:
            list_markers = ('VerifiedEmailAddresses', 'Identities',
                            'DkimTokens', 'VerificationAttributes',
                            'SendDataPoints')
            item_markers = ('member', 'item', 'entry')

            e = boto.jsonresponse.Element(list_marker=list_markers,
                                          item_marker=item_markers)
            h = boto.jsonresponse.XmlHandler(e, None)
            h.parse(body)
            return e
        else:
            # HTTP codes other than 200 are considered errors. Go through
            # some error handling to determine which exception gets raised,
            self._handle_error(response, body)
sdbmanager.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def load_object(self, obj):
        if not obj._loaded:
            a = self.domain.get_attributes(obj.id, consistent_read=self.consistent)
            if '__type__' in a:
                for prop in obj.properties(hidden=False):
                    if prop.name in a:
                        value = self.decode_value(prop, a[prop.name])
                        value = prop.make_value_from_datastore(value)
                        try:
                            setattr(obj, prop.name, value)
                        except Exception as e:
                            boto.log.exception(e)
            obj._loaded = True
document.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __init__(self, response, doc_service, sdf):
        self.response = response
        self.doc_service = doc_service
        self.sdf = sdf

        _body = response.content.decode('utf-8')

        try:
            self.content = json.loads(_body)
        except:
            boto.log.error('Error indexing documents.\nResponse Content:\n{0}\n\n'
                'SDF:\n{1}'.format(_body, self.sdf))
            raise boto.exception.BotoServerError(self.response.status_code, '',
                body=_body)

        self.status = self.content['status']
        if self.status == 'error':
            self.errors = [e.get('message') for e in self.content.get('errors',
                [])]
            for e in self.errors:
                if "Illegal Unicode character" in e:
                    raise EncodingError("Illegal Unicode character in document")
                elif e == "The Content-Length is too long":
                    raise ContentTooLongError("Content was too long")
            if 'adds' not in self.content or 'deletes' not in self.content:
                raise SearchServiceException("Error indexing documents"
                    " => %s" % self.content.get('message', ''))
        else:
            self.errors = []

        self.adds = self.content['adds']
        self.deletes = self.content['deletes']
        self._check_num_ops('add', self.adds)
        self._check_num_ops('delete', self.deletes)
document.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __init__(self, response, doc_service, sdf):
        self.response = response
        self.doc_service = doc_service
        self.sdf = sdf

        _body = response.content.decode('utf-8')

        try:
            self.content = json.loads(_body)
        except:
            boto.log.error('Error indexing documents.\nResponse Content:\n{0}'
                           '\n\nSDF:\n{1}'.format(_body, self.sdf))
            raise boto.exception.BotoServerError(self.response.status_code, '',
                                                 body=_body)

        self.status = self.content['status']
        if self.status == 'error':
            self.errors = [e.get('message') for e in self.content.get('errors',
                                                                      [])]
            for e in self.errors:
                if "Illegal Unicode character" in e:
                    raise EncodingError("Illegal Unicode character in document")
                elif e == "The Content-Length is too long":
                    raise ContentTooLongError("Content was too long")
        else:
            self.errors = []

        self.adds = self.content['adds']
        self.deletes = self.content['deletes']
        self._check_num_ops('add', self.adds)
        self._check_num_ops('delete', self.deletes)
document.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _check_num_ops(self, type_, response_num):
        """Raise exception if number of ops in response doesn't match commit

        :type type_: str
        :param type_: Type of commit operation: 'add' or 'delete'

        :type response_num: int
        :param response_num: Number of adds or deletes in the response.

        :raises: :class:`boto.cloudsearch2.document.CommitMismatchError`
        """
        commit_num = len([d for d in self.doc_service.documents_batch
                          if d['type'] == type_])

        if response_num != commit_num:
            boto.log.debug(self.response.content)
            # There will always be a commit mismatch error if there is any
            # errors on cloudsearch. self.errors gets lost when this
            # CommitMismatchError is raised. Whoever is using boto has no idea
            # why their commit failed. They can't even notify the user of the
            # cause by parsing the error messages from amazon. So let's
            # attach the self.errors to the exceptions if we already spent
            # time and effort collecting them out of the response.
            exc = CommitMismatchError(
                'Incorrect number of {0}s returned. Commit: {1} Response: {2}'
                .format(type_, commit_num, response_num)
            )
            exc.errors = self.errors
            raise exc
layer1.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def make_request(self, action, body='', object_hook=None):
        """
        :raises: ``SWFResponseError`` if response status is not 200.
        """
        headers = {'X-Amz-Target': '%s.%s' % (self.ServiceName, action),
                   'Host': self.region.endpoint,
                   'Content-Type': 'application/json; charset=UTF-8',
                   'Content-Encoding': 'amz-1.0',
                   'Content-Length': str(len(body))}
        http_request = self.build_base_http_request('POST', '/', '/',
                                                    {}, headers, body, None)
        response = self._mexe(http_request, sender=None,
                              override_num_retries=10)
        response_body = response.read().decode('utf-8')
        boto.log.debug(response_body)
        if response.status == 200:
            if response_body:
                return json.loads(response_body, object_hook=object_hook)
            else:
                return None
        else:
            json_body = json.loads(response_body)
            fault_name = json_body.get('__type', None)
            # Certain faults get mapped to more specific exception classes.
            excp_cls = self._fault_excp.get(fault_name, self.ResponseError)
            raise excp_cls(response.status, response.reason, body=json_body)

    # Actions related to Activities
layer1.py 文件源码 项目:cuny-bdif 作者: aristotle-tek 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def delete_stream(self, stream_name):
        """
        This operation deletes a stream and all of its shards and
        data. You must shut down any applications that are operating
        on the stream before you delete the stream. If an application
        attempts to operate on a deleted stream, it will receive the
        exception `ResourceNotFoundException`.

        If the stream is in the ACTIVE state, you can delete it. After
        a `DeleteStream` request, the specified stream is in the
        DELETING state until Amazon Kinesis completes the deletion.

        **Note:** Amazon Kinesis might continue to accept data read
        and write operations, such as PutRecord and GetRecords, on a
        stream in the DELETING state until the stream deletion is
        complete.

        When you delete a stream, any shards in that stream are also
        deleted.

        You can use the DescribeStream operation to check the state of
        the stream, which is returned in `StreamStatus`.

        `DeleteStream` has a limit of 5 transactions per second per
        account.

        :type stream_name: string
        :param stream_name: The name of the stream to delete.

        """
        params = {'StreamName': stream_name, }
        return self.make_request(action='DeleteStream',
                                 body=json.dumps(params))
connection.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def _make_request(self, action, params=None):
        """Make a call to the SES API.

        :type action: string
        :param action: The API method to use (e.g. SendRawEmail)

        :type params: dict
        :param params: Parameters that will be sent as POST data with the API
            call.
        """
        ct = 'application/x-www-form-urlencoded; charset=UTF-8'
        headers = {'Content-Type': ct}
        params = params or {}
        params['Action'] = action

        for k, v in params.items():
            if isinstance(v, six.text_type):  # UTF-8 encode only if it's Unicode
                params[k] = v.encode('utf-8')

        response = super(SESConnection, self).make_request(
            'POST',
            '/',
            headers=headers,
            data=urllib.parse.urlencode(params)
        )
        body = response.read().decode('utf-8')
        if response.status == 200:
            list_markers = ('VerifiedEmailAddresses', 'Identities',
                            'DkimTokens', 'DkimAttributes',
                            'VerificationAttributes', 'SendDataPoints')
            item_markers = ('member', 'item', 'entry')

            e = boto.jsonresponse.Element(list_marker=list_markers,
                                          item_marker=item_markers)
            h = boto.jsonresponse.XmlHandler(e, None)
            h.parse(body)
            return e
        else:
            # HTTP codes other than 200 are considered errors. Go through
            # some error handling to determine which exception gets raised,
            self._handle_error(response, body)
sdbmanager.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def encode_string(self, value):
        """Convert ASCII, Latin-1 or UTF-8 to pure Unicode"""
        if not isinstance(value, str):
            return value
        try:
            return six.text_type(value, 'utf-8')
        except:
            # really, this should throw an exception.
            # in the interest of not breaking current
            # systems, however:
            arr = []
            for ch in value:
                arr.append(six.unichr(ord(ch)))
            return u"".join(arr)
sdbmanager.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def load_object(self, obj):
        if not obj._loaded:
            a = self.domain.get_attributes(obj.id, consistent_read=self.consistent)
            if '__type__' in a:
                for prop in obj.properties(hidden=False):
                    if prop.name in a:
                        value = self.decode_value(prop, a[prop.name])
                        value = prop.make_value_from_datastore(value)
                        try:
                            setattr(obj, prop.name, value)
                        except Exception as e:
                            boto.log.exception(e)
            obj._loaded = True
document.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, response, doc_service, sdf):
        self.response = response
        self.doc_service = doc_service
        self.sdf = sdf

        _body = response.content.decode('utf-8')

        try:
            self.content = json.loads(_body)
        except:
            boto.log.error('Error indexing documents.\nResponse Content:\n{0}\n\n'
                'SDF:\n{1}'.format(_body, self.sdf))
            raise boto.exception.BotoServerError(self.response.status_code, '',
                body=_body)

        self.status = self.content['status']
        if self.status == 'error':
            self.errors = [e.get('message') for e in self.content.get('errors',
                [])]
            for e in self.errors:
                if "Illegal Unicode character" in e:
                    raise EncodingError("Illegal Unicode character in document")
                elif e == "The Content-Length is too long":
                    raise ContentTooLongError("Content was too long")
            if 'adds' not in self.content or 'deletes' not in self.content:
                raise SearchServiceException("Error indexing documents"
                    " => %s" % self.content.get('message', ''))
        else:
            self.errors = []

        self.adds = self.content['adds']
        self.deletes = self.content['deletes']
        self._check_num_ops('add', self.adds)
        self._check_num_ops('delete', self.deletes)
bucket.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def disable_logging(self, headers=None):
        """
        Disable logging on a bucket.

        :rtype: bool
        :return: True if ok or raises an exception.
        """
        blogging = BucketLogging()
        return self.set_xml_logging(blogging.to_xml(), headers=headers)
document.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 56 收藏 0 点赞 0 评论 0
def __init__(self, response, doc_service, sdf, signed_request=False):
        self.response = response
        self.doc_service = doc_service
        self.sdf = sdf
        self.signed_request = signed_request

        if self.signed_request:
            self.content = response
        else:
            _body = response.content.decode('utf-8')

            try:
                self.content = json.loads(_body)
            except:
                boto.log.error('Error indexing documents.\nResponse Content:\n{0}'
                               '\n\nSDF:\n{1}'.format(_body, self.sdf))
                raise boto.exception.BotoServerError(self.response.status_code, '',
                                                     body=_body)

        self.status = self.content['status']
        if self.status == 'error':
            self.errors = [e.get('message') for e in self.content.get('errors',
                                                                      [])]
            for e in self.errors:
                if "Illegal Unicode character" in e:
                    raise EncodingError("Illegal Unicode character in document")
                elif e == "The Content-Length is too long":
                    raise ContentTooLongError("Content was too long")
        else:
            self.errors = []

        self.adds = self.content['adds']
        self.deletes = self.content['deletes']
        self._check_num_ops('add', self.adds)
        self._check_num_ops('delete', self.deletes)
document.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _check_num_ops(self, type_, response_num):
        """Raise exception if number of ops in response doesn't match commit

        :type type_: str
        :param type_: Type of commit operation: 'add' or 'delete'

        :type response_num: int
        :param response_num: Number of adds or deletes in the response.

        :raises: :class:`boto.cloudsearch2.document.CommitMismatchError`
        """
        commit_num = len([d for d in self.doc_service.documents_batch
                          if d['type'] == type_])

        if response_num != commit_num:
            if self.signed_request:
                boto.log.debug(self.response)
            else:
                boto.log.debug(self.response.content)
            # There will always be a commit mismatch error if there is any
            # errors on cloudsearch. self.errors gets lost when this
            # CommitMismatchError is raised. Whoever is using boto has no idea
            # why their commit failed. They can't even notify the user of the
            # cause by parsing the error messages from amazon. So let's
            # attach the self.errors to the exceptions if we already spent
            # time and effort collecting them out of the response.
            exc = CommitMismatchError(
                'Incorrect number of {0}s returned. Commit: {1} Response: {2}'
                .format(type_, commit_num, response_num)
            )
            exc.errors = self.errors
            raise exc
layer1.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def make_request(self, action, body='', object_hook=None):
        """
        :raises: ``SWFResponseError`` if response status is not 200.
        """
        headers = {'X-Amz-Target': '%s.%s' % (self.ServiceName, action),
                   'Host': self.region.endpoint,
                   'Content-Type': 'application/json; charset=UTF-8',
                   'Content-Encoding': 'amz-1.0',
                   'Content-Length': str(len(body))}
        http_request = self.build_base_http_request('POST', '/', '/',
                                                    {}, headers, body, None)
        response = self._mexe(http_request, sender=None,
                              override_num_retries=10)
        response_body = response.read().decode('utf-8')
        boto.log.debug(response_body)
        if response.status == 200:
            if response_body:
                return json.loads(response_body, object_hook=object_hook)
            else:
                return None
        else:
            json_body = json.loads(response_body)
            fault_name = json_body.get('__type', None)
            # Certain faults get mapped to more specific exception classes.
            excp_cls = self._fault_excp.get(fault_name, self.ResponseError)
            raise excp_cls(response.status, response.reason, body=json_body)

    # Actions related to Activities
layer1.py 文件源码 项目:learneveryword 作者: karan 项目源码 文件源码 阅读 15 收藏 0 点赞 0 评论 0
def delete_stream(self, stream_name):
        """
        Deletes a stream and all its shards and data. You must shut
        down any applications that are operating on the stream before
        you delete the stream. If an application attempts to operate
        on a deleted stream, it will receive the exception
        `ResourceNotFoundException`.

        If the stream is in the `ACTIVE` state, you can delete it.
        After a `DeleteStream` request, the specified stream is in the
        `DELETING` state until Amazon Kinesis completes the deletion.

        **Note:** Amazon Kinesis might continue to accept data read
        and write operations, such as PutRecord, PutRecords, and
        GetRecords, on a stream in the `DELETING` state until the
        stream deletion is complete.

        When you delete a stream, any shards in that stream are also
        deleted, and any tags are dissociated from the stream.

        You can use the DescribeStream operation to check the state of
        the stream, which is returned in `StreamStatus`.

        `DeleteStream` has a limit of 5 transactions per second per
        account.

        :type stream_name: string
        :param stream_name: The name of the stream to delete.

        """
        params = {'StreamName': stream_name, }
        return self.make_request(action='DeleteStream',
                                 body=json.dumps(params))
connection.py 文件源码 项目:Chromium_DepotTools 作者: p07r0457 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _make_request(self, action, params=None):
        """Make a call to the SES API.

        :type action: string
        :param action: The API method to use (e.g. SendRawEmail)

        :type params: dict
        :param params: Parameters that will be sent as POST data with the API
            call.
        """
        ct = 'application/x-www-form-urlencoded; charset=UTF-8'
        headers = {'Content-Type': ct}
        params = params or {}
        params['Action'] = action

        for k, v in params.items():
            if isinstance(v, unicode):  # UTF-8 encode only if it's Unicode
                params[k] = v.encode('utf-8')

        response = super(SESConnection, self).make_request(
            'POST',
            '/',
            headers=headers,
            data=urllib.urlencode(params)
        )
        body = response.read()
        if response.status == 200:
            list_markers = ('VerifiedEmailAddresses', 'Identities',
                            'VerificationAttributes', 'SendDataPoints')
            item_markers = ('member', 'item', 'entry')

            e = boto.jsonresponse.Element(list_marker=list_markers,
                                          item_marker=item_markers)
            h = boto.jsonresponse.XmlHandler(e, None)
            h.parse(body)
            return e
        else:
            # HTTP codes other than 200 are considered errors. Go through
            # some error handling to determine which exception gets raised,
            self._handle_error(response, body)


问题


面经


文章

微信
公众号

扫码关注公众号