# $SnapHashLicense:
#
# SnapLogic - Open source data services
#
# Copyright (C) 2008, SnapLogic, Inc. All rights reserved.
#
# See http://www.snaplogic.org for more information about
# the SnapLogic project.
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
#
# "SnapLogic" is a trademark of SnapLogic, Inc.
#
#
# $
# $Id: stream_base.py 1416 2008-03-07 22:55:34Z kurt $
"""
Contains the StreamBase class, parent to the Reader and Writer SnapStream objects.
"""
import time
from snaplogic import rp
class StreamBase(object):
"""
Base class to the SnapStream Reader and Writer classes.
Code common to both reading and writing is present in this class. This includes some constants and
informational properties.
"""
RECORD_STREAM = 'record'
BINARY_STREAM = 'binary'
def __init__(self, request_url, view_name):
self.request_url = request_url
self.view_name = view_name
self.content_type = None
self.stream_mode = None
self.data_transferred = 0
self.transfer_rate = 0
self.timers = {}
self._start_time = time.time()
def _exception(self, exc_cls, msg, *params):
return exc_cls(msg,
('URL', self.request_url),
('View', self.view_name),
*params)
def _transferred_data(self, count):
time_delta = time.time() - self._start_time
self.data_transferred += count
if time_delta != 0:
self.transfer_rate = self.data_transferred / time_delta
def _parse_media_type(self, media_type):
params = [param.strip() for param in media_type.split(';')]
content_type = params.pop(0)
for p in params:
(name, value) = p.split('=', 1)
if name == 'q':
return (content_type, value)
return (content_type, '1.0')
def close(self):
"""
Close the underlying connection. In the case of a record-mode Writer stream, the
Writer.end_of_stream() method should be called prior to this method to indicate a successful
close. Otherwise, the receiving endpoint will consider it an erroneous close such as a Component run
failure.
"""
# This method just serves as a reminder to children to implement
raise NotImplementedError()
|