1 2 3 4 5 6 7 8 9 10 11 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
from codecs import decode, encode
from functools import partial
from typing import Any as TAny, Callable as TCallable, NamedTuple
from traits.api import Any, HasStrictTraits, Interface
class DataFormat(NamedTuple):
""" Information about a mimetype and serializers.
Simple namedtuple-based class that stores the mimetype, serializer and
deserializer together.
"""
#: The mimetype of the data.
mimetype: str
#: A callable that serializes this format. It should take an python
#: object of a supported type, and return a bytestring.
serialize: TCallable[[TAny], bytes]
#: A callable that deserializes this format. It should take a
#: bytestring and return the extracted object.
deserialize: TCallable[[bytes], TAny]
def text_format(encoding='utf-8', mimetype='text/plain'):
""" DataFormat factory for text mimetypes.
"""
return DataFormat(
mimetype=mimetype,
serialize=partial(encode, encoding=encoding),
deserialize=partial(decode, encoding=encoding),
)
class IDataWrapper(Interface):
""" Wrapper around polymorphic toolkit data object containing mimedata.
To support clipboard and drag and drop operations, toolkits need a way
of generically representing data in multiple formats. This is a wrapper
class that provides a toolkit independent intreface to these classes
which allow the exchange of data, with types specified by MIME types.
"""
#: The toolkit data object.
toolkit_data = Any()
def mimetypes(self):
""" Return a set of mimetypes holding data.
Returns
-------
mimetypes : set of str
The set of mimetypes currently storing data in the toolkit data
object.
"""
pass
def has_format(self, format):
""" Whether or not a particular format has available data.
Parameters
----------
format : DataFormat
A data format object.
Returns
-------
has_format : bool
Whether or not there is data associated with that format in the
underlying toolkit object.
"""
raise NotImplementedError()
def get_format(self, format):
""" The decoded data associted with the format.
Parameters
----------
format : DataFormat
A data format object.
Returns
-------
data : Any
The data decoded for the given format.
"""
raise NotImplementedError()
def set_format(self, format, data):
""" Encode and set data for the format.
Parameters
----------
format : DataFormat
A data format object.
data : Any
The data to be encoded and stored.
"""
raise NotImplementedError()
def get_mimedata(self, mimetype):
""" Get raw data for the given media type.
Parameters
----------
mimetype : str
The mime media type to be extracted.
Returns
-------
mimedata : bytes
The mime media data as bytes.
"""
raise NotImplementedError()
def set_mimedata(self, mimetype, mimedata):
""" Set raw data for the given media type.
Parameters
----------
mimetype : str
The mime media type to be extracted.
mimedata : bytes
The mime media data encoded as bytes..
"""
raise NotImplementedError()
class MDataWrapper(HasStrictTraits):
""" Mixin class for DataWrappers.
This provides standard methods for using DataFormat objects, but not the
low-level communication with the underlying toolkit.
"""
def has_format(self, format):
""" Whether or not a particular format has available data.
Parameters
----------
format : DataFormat
A data format object.
Returns
-------
has_format : bool
Whether or not there is data associated with that format in the
underlying toolkit object.
"""
return format.mimetype in self.mimetypes()
def get_format(self, format):
""" The decoded data associted with the format.
Parameters
----------
format : DataFormat
A data format object.
Returns
-------
data : Any
The data decoded for the given format.
"""
return format.deserialize(self.get_mimedata(format.mimetype))
def set_format(self, format, data):
""" Encode and set data for the format.
Parameters
----------
format : DataFormat
A data format object.
data : Any
The data to be encoded and stored.
"""
self.set_mimedata(format.mimetype, format.serialize(data))
|