stream_base.py :  » Development » SnapLogic » snaplogic » common » snapstream » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SnapLogic 
SnapLogic » snaplogic » common » snapstream » stream_base.py
# $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()
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.