selectable_rp_pipe.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 » selectable_rp_pipe.py
# $SnapHashLicense:
# 
# SnapLogic - Open source data services
# 
# Copyright (C) 2009, 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: selectable_rp_pipe.py 7930 2009-06-22 22:00:28Z dmitri $

"""
Contains the class SelectableRPPipe.

SelectableRPPipe is an implementation of SelectablePipe used to not only transfer data between streams
but conveniently perform an RP translation on it.

"""

from snaplogic.common.snap_exceptions import *
from snaplogic.common.snapstream.string_buffer import StringBuffer
from snaplogic.common.snapstream.selectable_object_pipe import SelectableObjectPipe
from snaplogic.common.snapstream.selectable_binary_pipe import SelectableBinaryPipe
from snaplogic.common.snap_exceptions import *

class SelectableRPReaderPipe(SelectableObjectPipe):
    """
    A hybrid binary to object pipe with an automatic RP conversion.

    This hybrid class of a binary and object SelectablePipe uses an RP Reader object to transform an incoming
    binary stream that is RP-parseable into its object stream automatically. As the binary data is put into
    the pipe, it is sent to the RP Reader that the object was initialized with. Objects are pulled from the RP
    as enough data binary data arrives to make a full object.

    """
    def __init__(self, rp):
        super(SelectableRPReaderPipe, self).__init__()
        
        if not rp.Reader.supports_non_blocking_read():
            raise SnapObjTypeError("SelectableRPReaderPipe requires a non-blocking RP Reader object.")
        
        self._input_buffer = StringBuffer()
        self._reader = rp.Reader(self._input_buffer)

    def _put(self, data):
        self._input_buffer.write(data)
        self._buffer.extend(self._reader.read_nb())

class SelectableRPWriterPipe(SelectableBinaryPipe):
    """
    A hybrid object to binary pipe with an automatic RP conversion.

    This hybrid class of a binary and object SelectablePipe uses an RP Writer object to transform an incoming
    object stream into its binary stream automatically by the pipe consumer. As the object data is put into
    the pipe, it is sent to the RP Writer that the object was initialized with. Binary data can be read from the
    pipe for each object that was already written to the pipe.

    """
    def __init__(self, rp):
        super(SelectableRPWriterPipe, self).__init__()

        self._writer = rp.Writer(self._buffer)
        self._writer.initialize()

    def _put(self, item):
        self._writer.write(item)

    def close(self):
        # This method must be synchronized, just like all the rest of the "public" methods
        # in pipes, otherwise one thread may be closing the pipe while another thread
        # is reading or writing to it.
        self._cond.acquire()
        try:
            if self._open:
                self._writer.end()
                
                # Note that we're calling the parent's method, which also
                # obtains the lock (that's why we use a reentrant lock).
                super(SelectableRPWriterPipe, self).close()
        finally:
            self._cond.release()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.