1 #=======================================================================
2 #-----------------------------------------------------------------------
4 __version__
= '''0.0.00'''
5 __sub_version__
= '''20031106031445'''
6 __copyright__
= '''(c) Alex A. Naanou 2003'''
10 #-----------------------------------------------------------------------
14 #-----------------------------------------------------------------------
17 # a recursive data format (container) that supports the stream
18 # read and write interfaces/protocols.
21 # an entity that reads data from a stream and writes it out.
24 # an entity that read data and writes it to a stream.
37 # -------------------- Serialize ------------------->
39 # StreamReader StreamWriter
40 # ----------------> ---------------->
41 # PythonData Stream Data
42 # <---------------- <----------------
43 # StreamWriter StreamReader
45 # <------------------ Deserialize -------------------
49 # all data is a stream.
51 # any stream can either be mutable or immutable (defined in
52 # constructor), this is only used for read write operations as on the
53 # inside, a stream may be all mutable (for optimization.)
55 # a stream is not editable!.
57 # a chained stream can be a receiver, a transmitter or a channeler
58 # (receive and transmit).
60 # Stream Data Processors:
61 # ReferenceStream: (???)
62 # this is a reference to another stream in the registry.
65 # this is a stream that can contain one or more basic values.
66 # corresponds to pythons:
72 # this is a stream that can contain one or more basic values.
73 # corresponds to pythons:
79 # this can contain zero or more streams (e.g. "stream of streams").
80 # corresponds to pythons:
85 # this contains zero or more pairs of the form:
86 # <ElementStream> : <any>
87 # corresponds to pythons:
91 # this is a Stream with additional stores/attributes (e.g. all data
92 # is stored in named streams)
93 # essential attributes:
98 # optional attributes:
101 # corresponds to pythons:
113 # StreamReader -> StreamWriter
114 # StreamReader -> GenericStream
115 # GenericStream -> StreamWriter
119 # open -- will open a sub-stream.
120 # close -- will close a sub-stream.
121 # insert -- will insert an element into the current stream.
122 # set -- will set a stream attribute (__setattr__).
127 #=======================================================================
132 #---------------------------------------------------------StreamError---
133 class StreamError(Exception):
140 #-----------------------------------------------------------------------
141 #------------------------------------------------------StreamRegistry---
142 class StreamRegistry(object):
144 this will keep track of objects in a stream.
149 #--------------------------------------------------------------Stream---
150 # TODO add support for stream object registry... (???)
151 class Stream(stream
.Stream
):
153 this is an abstract stream object.
155 ##!! revise the dispatcher interface in the following !!##
156 # Stream Magic Methods...
157 # NOTE: in the folowing methods the dispatch attribute must resolve
158 # to the stream managers dispatcher object...
159 # this will permit nested stream managers (e.g. serialize all
160 # objects using the Pickle format but the objects of type X
161 # which are to be serialized into XML, or use diferent xml
162 # formats for diferent object types... etc.)
163 # NOTE: for the above to work properlyit may be good to pass
164 # the "reference" to the serialized object or file
165 # upwards to the conatining stream...
166 def __open__(self
, parent_stream
, *pargs
, **nargs
):
168 this method is called opun stream open.
170 # call the dispatch...
171 ## self.dispatch('open', type(self), parent_stream, *pargs, **nargs)
172 self
.dispatch
.open(type(self
), parent_stream
, *pargs
, **nargs
)
175 this method is called on stream close.
177 # call the dispatch...
178 ## self.dispatch('close', type(self))
179 self
.dispatch
.close(type(self
))
180 def __insert__(self
, *pargs
, **nargs
):
182 this method is called on object insert.
184 # call the dispatch...
185 ## self.dispatch('insert', type(self), *pargs, **nargs)
186 self
.dispatch
.insert(type(self
), *pargs
, **nargs
)
190 #-----------------------------------------------------------------------
191 # these objects must call the *contexts* interface functions...
192 #--------------------------------------------------------NumberStream---
193 class NumberStream(Stream
):
198 #--------------------------------------------------------StringStream---
199 class StringStream(Stream
):
204 #------------------------------------------------------SequenceStream---
205 class SequenceStream(Stream
):
210 #-------------------------------------------------------MappingStream---
211 class MappingStream(Stream
):
216 #--------------------------------------------------------ObjectStream---
217 class ObjectStream(Stream
):
222 #-----------------------------------------------------ReferenceStream---
223 # this should hold information on the location of the stream, its type
224 # and data needed for the stream to be properely processed...
226 # 1. create a reference inside the current stream (needs: target id
227 # (from stream registry)).
228 # 2. create a stream object using an "external" stream processor
232 class ReferenceStream(Stream
):
234 this is a spetial object to be used as a link to a diferent stream object.
239 #=======================================================================
240 #---------------------------------------------StreamProcessorDispatch---
242 # 1. specific type processor in/for meta-stream
243 # 2. meta-stream processor
244 # 3. Err: unsupported object type.
246 class StreamProcessorDispatch(object):
253 # dispatch interface methods...
254 def register_processor(self
, stream_type
, obj
):
257 # stream interface methods...
258 def open(self
, stream_type
, stream_length
=None, stream_id
=None, *pargs
, **nargs
):
262 def close(self
, stream_type
):
266 def insert(self
, stream_type
, *pargs
, **nargs
):
270 # default processors...
275 #-----------------------------------------------------------------------
276 #--------------------------------------------------DataToStreamReader---
277 class DataToStreamReader(object):
279 this is a generic stream constructor class (data -> stream).
281 def __call__(self
, *pargs
, **nargs
):
283 this will initiate the read process and start writing to the target stream.
286 if not hasattr(self
, 'target_stream'):
287 raise StreamError
, 'stream reader not chained to a reciving stream.'
288 def chain(self
, stream
):
291 self
.target_stream
= stream
295 del self
.target_stream
298 #--------------------------------------------------StreamToDataWriter---
299 class StreamToDataWriter(ReferenceStream
):
301 this is a generic stream writer class (stream -> data).
304 # this is the generic stream processor...
305 dispatch
= StreamProcessorDispatch()
310 # TODO generate a better stream_id
311 super(StreamWriter
, self
).__init
__(self
, parent
=None, sream_id
=id(self
))
315 #=======================================================================
316 # vim:set ts=4 sw=4 nowrap :