Skip to content

adaptor

sleap.io.format.adaptor

File format adaptor base class.

Classes:

Name Description
Adaptor

File format adaptor base class.

SleapObjectType

Types of files that an adaptor could read/write.

Adaptor

File format adaptor base class.

An adaptor handles reading and/or writing a specific file format. To add support for a new file format, you'll create a new class which inherits from the Adaptor base class and implements the relevant functions.

Methods:

Name Description
can_read_file

Returns whether this adaptor can read this file.

can_write_filename

Returns whether this adaptor can write format of this filename.

does_match_ext

Returns whether this adaptor can write format of this filename.

does_read

Returns whether this adaptor supports reading.

does_write

Returns whether this adaptor supports writing.

read

Reads the file and returns the appropriate deserialized object.

write

Writes the object to a file.

Attributes:

Name Type Description
all_exts List[str]

List of all file extensions supported by adaptor.

default_ext str

The default file extension, e.g., 'json' (without '.').

formatted_ext_options

String for Qt file dialog extension options.

handles SleapObjectType

Returns the type of object that can be read/written.

name str

Human-reading name of the file format

Source code in sleap/io/format/adaptor.py
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
@attr.s(auto_attribs=True)
class Adaptor:
    """
    File format adaptor base class.

    An adaptor handles reading and/or writing a specific file format. To add
    support for a new file format, you'll create a new class which inherits from
    the Adaptor base class and implements the relevant functions.
    """

    @property
    def handles(self) -> SleapObjectType:
        """
        Returns the type of object that can be read/written.

        The Dispatch class calls this method on all registered adaptors to
        determine which to use for reading/writing.
        """
        raise NotImplementedError

    @property
    def default_ext(self) -> str:
        """The default file extension, e.g., 'json' (without '.')."""
        raise NotImplementedError

    @property
    def all_exts(self) -> List[str]:
        """List of all file extensions supported by adaptor."""
        raise NotImplementedError

    @property
    def name(self) -> str:
        """Human-reading name of the file format"""
        raise NotImplementedError

    def can_read_file(self, file) -> bool:
        """Returns whether this adaptor can read this file."""
        raise NotImplementedError

    def can_write_filename(self, filename: str) -> bool:
        """Returns whether this adaptor can write format of this filename."""
        raise NotImplementedError

    def does_read(self) -> bool:
        """Returns whether this adaptor supports reading."""
        raise NotImplementedError

    def does_write(self) -> bool:
        """Returns whether this adaptor supports writing."""
        raise NotImplementedError

    def read(self, file) -> object:
        """Reads the file and returns the appropriate deserialized object."""
        raise NotImplementedError

    def write(self, filename: str, source_object: object):
        """Writes the object to a file."""
        raise NotImplementedError

    # Methods with default implementation

    def does_match_ext(self, filename: str) -> bool:
        """Returns whether this adaptor can write format of this filename."""

        # We don't match the ext against the result of os.path.splitext because
        # we want to match extensions like ".pkg.slp".

        return str(filename).endswith(tuple(self.all_exts))

    @property
    def formatted_ext_options(self):
        """String for Qt file dialog extension options."""
        return f"{self.name} ({' '.join(self.all_exts)})"

all_exts property

List of all file extensions supported by adaptor.

default_ext property

The default file extension, e.g., 'json' (without '.').

formatted_ext_options property

String for Qt file dialog extension options.

handles property

Returns the type of object that can be read/written.

The Dispatch class calls this method on all registered adaptors to determine which to use for reading/writing.

name property

Human-reading name of the file format

can_read_file(file)

Returns whether this adaptor can read this file.

Source code in sleap/io/format/adaptor.py
53
54
55
def can_read_file(self, file) -> bool:
    """Returns whether this adaptor can read this file."""
    raise NotImplementedError

can_write_filename(filename)

Returns whether this adaptor can write format of this filename.

Source code in sleap/io/format/adaptor.py
57
58
59
def can_write_filename(self, filename: str) -> bool:
    """Returns whether this adaptor can write format of this filename."""
    raise NotImplementedError

does_match_ext(filename)

Returns whether this adaptor can write format of this filename.

Source code in sleap/io/format/adaptor.py
79
80
81
82
83
84
85
def does_match_ext(self, filename: str) -> bool:
    """Returns whether this adaptor can write format of this filename."""

    # We don't match the ext against the result of os.path.splitext because
    # we want to match extensions like ".pkg.slp".

    return str(filename).endswith(tuple(self.all_exts))

does_read()

Returns whether this adaptor supports reading.

Source code in sleap/io/format/adaptor.py
61
62
63
def does_read(self) -> bool:
    """Returns whether this adaptor supports reading."""
    raise NotImplementedError

does_write()

Returns whether this adaptor supports writing.

Source code in sleap/io/format/adaptor.py
65
66
67
def does_write(self) -> bool:
    """Returns whether this adaptor supports writing."""
    raise NotImplementedError

read(file)

Reads the file and returns the appropriate deserialized object.

Source code in sleap/io/format/adaptor.py
69
70
71
def read(self, file) -> object:
    """Reads the file and returns the appropriate deserialized object."""
    raise NotImplementedError

write(filename, source_object)

Writes the object to a file.

Source code in sleap/io/format/adaptor.py
73
74
75
def write(self, filename: str, source_object: object):
    """Writes the object to a file."""
    raise NotImplementedError

SleapObjectType

Bases: Enum

Types of files that an adaptor could read/write.

Source code in sleap/io/format/adaptor.py
11
12
13
14
15
class SleapObjectType(Enum):
    """Types of files that an adaptor could read/write."""

    misc = 0
    labels = 1