1import io
2import json
3import logging
4
5log = logging.getLogger(__name__)
6
7
8class Stream:
9    __shortname__ = "stream"
10
11    """
12    This is a base class that should be inherited when implementing
13    different stream types. Should only be created by plugins.
14    """
15
16    def __init__(self, session):
17        self.session = session
18
19    def __repr__(self):
20        return "<Stream()>"
21
22    def __json__(self):
23        return dict(type=type(self).shortname())
24
25    def open(self):
26        """
27        Attempts to open a connection to the stream.
28        Returns a file-like object that can be used to read the stream data.
29
30        Raises :exc:`StreamError` on failure.
31        """
32        raise NotImplementedError
33
34    @property
35    def json(self):
36        obj = self.__json__()
37        return json.dumps(obj)
38
39    @classmethod
40    def shortname(cls):
41        return cls.__shortname__
42
43    def to_url(self):
44        raise TypeError("{0} cannot be converted to a URL".format(self.shortname()))
45
46    def to_manifest_url(self):
47        raise TypeError("{0} cannot be converted to a URL".format(self.shortname()))
48
49
50class StreamIO(io.IOBase):
51    pass
52
53
54__all__ = ["Stream", "StreamIO"]
55