1from typing import List
2
3from fontTools.ufoLib import UFOReader, UFOWriter
4
5from ufoLib2.objects.misc import DataStore
6
7
8class DataSet(DataStore):
9    """Represents a mapping of POSIX filename strings to arbitrary data bytes.
10
11    Always use forward slahes (/) as directory separators, even on Windows.
12
13    Behavior:
14        DataSet behaves like a dictionary of type ``Dict[str, bytes]``.
15
16        >>> from ufoLib2 import Font
17        >>> font = Font()
18        >>> font.data["test.txt"] = b"123"
19        >>> font.data["directory/my_binary_blob.bin"] = b"456"
20        >>> font.data["test.txt"]
21        b'123'
22        >>> del font.data["test.txt"]
23        >>> list(font.data.items())
24        [('directory/my_binary_blob.bin', b'456')]
25    """
26
27    @staticmethod
28    def list_contents(reader: UFOReader) -> List[str]:
29        """Returns a list of POSIX filename strings in the data store."""
30        return reader.getDataDirectoryListing()
31
32    @staticmethod
33    def read_data(reader: UFOReader, filename: str) -> bytes:
34        """Returns the data at filename within the store."""
35        return reader.readData(filename)
36
37    @staticmethod
38    def write_data(writer: UFOWriter, filename: str, data: bytes) -> None:
39        """Writes the data to filename within the store."""
40        writer.writeData(filename, data)
41
42    @staticmethod
43    def remove_data(writer: UFOWriter, filename: str) -> None:
44        """Remove the data at filename within the store."""
45        writer.removeData(filename)
46