data.py 1.79 KB
Newer Older
1 2
from zope.interface import implementer
import six
3 4 5
from Products.PortalTransforms.interfaces import IDataStream


6
@implementer(IDataStream)
Yusei Tahara's avatar
Yusei Tahara committed
7 8
class datastream:
    """A transformation datastream packet"""
9 10
    if six.PY2:
        __slots__ = ('name', '_data', '_metadata')
11

Yusei Tahara's avatar
Yusei Tahara committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    def __init__(self, name):
        self.__name__ = name
        self._data = ''
        self._metadata = {}
        self._objects = {}
        self._cacheable = 1

    def __str__(self):
        return self.getData()

    def name(self):
        return self.__name__

    def setData(self, value):
        """set the main data produced by a transform, i.e. usually a string"""
        self._data = value

    def getData(self):
        """provide access to the transformed data object, i.e. usually a string.
        This data may references subobjects.
        """
        if callable(self._data):
            data = self._data()
        else:
            data = self._data
        return data

    def setSubObjects(self, objects):
        """set a dict-like object containing subobjects.
        keys should be object's identifier (e.g. usually a filename) and
        values object's content.
        """
        self._objects = objects

    def getSubObjects(self):
        """return a dict-like object with any optional subobjects associated
        with the data"""
        return self._objects

    def getMetadata(self):
        """return a dict-like object with any optional metadata from
        the transform"""
        return self._metadata
55

Yusei Tahara's avatar
Yusei Tahara committed
56 57
    def isCacheable(self):
        """Return a bool which indicates wether the result should be cached
58

Yusei Tahara's avatar
Yusei Tahara committed
59 60 61
        Default is true
        """
        return self._cacheable
62

Yusei Tahara's avatar
Yusei Tahara committed
63 64 65
    def setCacheable(self, value):
        """Set cacheable flag to yes or no
        """
66
        self._cacheable = bool(value)