Skip to content

Manifest

Representation of the “manifest.xml” part.

Classes:

Name Description
Manifest

Representation of the “manifest.xml” part.

Manifest

Bases: XmlPart

Representation of the “manifest.xml” part.

Methods:

Name Description
add_full_path

Add a new file entry to the manifest, or update an existing one.

del_full_path

Delete a file entry from the manifest.

get_media_type

Get the media type associated with a specific full path in the manifest.

get_path_medias

Get a list of all (full_path, media_type) pairs declared in the manifest.

get_paths

Get a list of all full paths (manifest:full-path) declared in the manifest.

make_file_entry

Create a new manifest:file-entry element.

set_media_type

Set the media type for an existing file entry in the manifest.

Source code in odfdo/manifest.py
 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
class Manifest(XmlPart):
    """Representation of the "manifest.xml" part."""

    def get_paths(self) -> list[str]:
        """Get a list of all full paths (`manifest:full-path`) declared in the manifest.

        Returns:
            list[str]: A list of strings, where each string is a full path.
        """
        xpath_query = "//manifest:file-entry/attribute::manifest:full-path"
        return [str(e) for e in self.xpath(xpath_query)]  # Explicitly cast EText to str

    def _file_entry(self, full_path: str) -> Element:
        """Internal helper to find a specific `manifest:file-entry` element.

        Args:
            full_path: The full path of the file entry to find.

        Returns:
            Element: The `manifest:file-entry` element.

        Raises:
            KeyError: If the specified `full_path` is not found in the manifest.
        """
        xpath_query = (
            f'//manifest:file-entry[attribute::manifest:full-path="{full_path}"]'
        )
        result = self.xpath(xpath_query)
        if not result:
            raise KeyError(f"Path not found: '{full_path}'")
        return result[0]  # type: ignore

    def get_path_medias(self) -> list[tuple[str | None, str | None]]:
        """Get a list of all (full_path, media_type) pairs declared in the manifest.

        Returns:
            list[tuple[str | None, str | None]]: A list of tuples, where each
                tuple contains the full path and its corresponding media type.
                Attribute values can be `None` if not found.
        """
        xpath_query = "//manifest:file-entry"
        result = []
        for file_entry in self.xpath(xpath_query):
            if not isinstance(file_entry, Element):  # pragma: no cover
                continue
            result.append(
                (
                    file_entry.get_attribute_string("manifest:full-path"),
                    file_entry.get_attribute_string("manifest:media-type"),
                )
            )
        return result

    def get_media_type(self, full_path: str) -> str | None:
        """Get the media type associated with a specific full path in the manifest.

        Args:
            full_path: The full path of the file entry.

        Returns:
            str | None: The media type string, or `None` if the path is not found.
        """
        xpath_query = (
            f'//manifest:file-entry[attribute::manifest:full-path="{full_path}"]'
            "/attribute::manifest:media-type"
        )
        result = self.xpath(xpath_query)
        if not result:
            return None
        return str(result[0])

    def set_media_type(self, full_path: str, media_type: str) -> None:
        """Set the media type for an existing file entry in the manifest.

        Args:
            full_path: The full path of the file entry.
            media_type: The new media type to set.
        """
        file_entry = self._file_entry(full_path)
        file_entry.set_attribute("manifest:media-type", media_type)

    @staticmethod
    def make_file_entry(full_path: str, media_type: str) -> Element:
        """Create a new `manifest:file-entry` element.

        Args:
            full_path: The full path for the file entry.
            media_type: The media type for the file entry.

        Returns:
            Element: A new `manifest:file-entry` element.
        """
        tag = (
            f"<manifest:file-entry "
            f'manifest:media-type="{media_type}" '
            f'manifest:full-path="{full_path}"/>'
        )
        return Element.from_tag(tag)

    def add_full_path(self, full_path: str, media_type: str = "") -> None:
        """Add a new file entry to the manifest, or update an existing one.

        If a file entry with the given `full_path` already exists, its media
        type is updated. Otherwise, a new `manifest:file-entry` element is
        created and added.

        Args:
            full_path: The full path of the file to add or update.
            media_type: The media type of the file.
        """
        # Existing?
        existing = self.get_media_type(full_path)
        if existing is not None:
            self.set_media_type(full_path, media_type)
        root = self.root
        root.append(self.make_file_entry(full_path, media_type))

    def del_full_path(self, full_path: str) -> None:
        """Delete a file entry from the manifest.

        Args:
            full_path: The full path of the file entry to delete.

        Raises:
            KeyError: If the specified `full_path` is not found in the manifest.
        """
        file_entry = self._file_entry(full_path)
        self.root.delete(file_entry)

_file_entry

_file_entry(full_path: str) -> Element

Internal helper to find a specific manifest:file-entry element.

Parameters:

Name Type Description Default
full_path str

The full path of the file entry to find.

required

Returns:

Name Type Description
Element Element

The manifest:file-entry element.

Raises:

Type Description
KeyError

If the specified full_path is not found in the manifest.

Source code in odfdo/manifest.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def _file_entry(self, full_path: str) -> Element:
    """Internal helper to find a specific `manifest:file-entry` element.

    Args:
        full_path: The full path of the file entry to find.

    Returns:
        Element: The `manifest:file-entry` element.

    Raises:
        KeyError: If the specified `full_path` is not found in the manifest.
    """
    xpath_query = (
        f'//manifest:file-entry[attribute::manifest:full-path="{full_path}"]'
    )
    result = self.xpath(xpath_query)
    if not result:
        raise KeyError(f"Path not found: '{full_path}'")
    return result[0]  # type: ignore

add_full_path

add_full_path(full_path: str, media_type: str = '') -> None

Add a new file entry to the manifest, or update an existing one.

If a file entry with the given full_path already exists, its media type is updated. Otherwise, a new manifest:file-entry element is created and added.

Parameters:

Name Type Description Default
full_path str

The full path of the file to add or update.

required
media_type str

The media type of the file.

''
Source code in odfdo/manifest.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def add_full_path(self, full_path: str, media_type: str = "") -> None:
    """Add a new file entry to the manifest, or update an existing one.

    If a file entry with the given `full_path` already exists, its media
    type is updated. Otherwise, a new `manifest:file-entry` element is
    created and added.

    Args:
        full_path: The full path of the file to add or update.
        media_type: The media type of the file.
    """
    # Existing?
    existing = self.get_media_type(full_path)
    if existing is not None:
        self.set_media_type(full_path, media_type)
    root = self.root
    root.append(self.make_file_entry(full_path, media_type))

del_full_path

del_full_path(full_path: str) -> None

Delete a file entry from the manifest.

Parameters:

Name Type Description Default
full_path str

The full path of the file entry to delete.

required

Raises:

Type Description
KeyError

If the specified full_path is not found in the manifest.

Source code in odfdo/manifest.py
147
148
149
150
151
152
153
154
155
156
157
def del_full_path(self, full_path: str) -> None:
    """Delete a file entry from the manifest.

    Args:
        full_path: The full path of the file entry to delete.

    Raises:
        KeyError: If the specified `full_path` is not found in the manifest.
    """
    file_entry = self._file_entry(full_path)
    self.root.delete(file_entry)

get_media_type

get_media_type(full_path: str) -> str | None

Get the media type associated with a specific full path in the manifest.

Parameters:

Name Type Description Default
full_path str

The full path of the file entry.

required

Returns:

Type Description
str | None

str | None: The media type string, or None if the path is not found.

Source code in odfdo/manifest.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def get_media_type(self, full_path: str) -> str | None:
    """Get the media type associated with a specific full path in the manifest.

    Args:
        full_path: The full path of the file entry.

    Returns:
        str | None: The media type string, or `None` if the path is not found.
    """
    xpath_query = (
        f'//manifest:file-entry[attribute::manifest:full-path="{full_path}"]'
        "/attribute::manifest:media-type"
    )
    result = self.xpath(xpath_query)
    if not result:
        return None
    return str(result[0])

get_path_medias

get_path_medias() -> list[tuple[str | None, str | None]]

Get a list of all (full_path, media_type) pairs declared in the manifest.

Returns:

Type Description
list[tuple[str | None, str | None]]

list[tuple[str | None, str | None]]: A list of tuples, where each tuple contains the full path and its corresponding media type. Attribute values can be None if not found.

Source code in odfdo/manifest.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def get_path_medias(self) -> list[tuple[str | None, str | None]]:
    """Get a list of all (full_path, media_type) pairs declared in the manifest.

    Returns:
        list[tuple[str | None, str | None]]: A list of tuples, where each
            tuple contains the full path and its corresponding media type.
            Attribute values can be `None` if not found.
    """
    xpath_query = "//manifest:file-entry"
    result = []
    for file_entry in self.xpath(xpath_query):
        if not isinstance(file_entry, Element):  # pragma: no cover
            continue
        result.append(
            (
                file_entry.get_attribute_string("manifest:full-path"),
                file_entry.get_attribute_string("manifest:media-type"),
            )
        )
    return result

get_paths

get_paths() -> list[str]

Get a list of all full paths (manifest:full-path) declared in the manifest.

Returns:

Type Description
list[str]

list[str]: A list of strings, where each string is a full path.

Source code in odfdo/manifest.py
33
34
35
36
37
38
39
40
def get_paths(self) -> list[str]:
    """Get a list of all full paths (`manifest:full-path`) declared in the manifest.

    Returns:
        list[str]: A list of strings, where each string is a full path.
    """
    xpath_query = "//manifest:file-entry/attribute::manifest:full-path"
    return [str(e) for e in self.xpath(xpath_query)]  # Explicitly cast EText to str

make_file_entry staticmethod

make_file_entry(full_path: str, media_type: str) -> Element

Create a new manifest:file-entry element.

Parameters:

Name Type Description Default
full_path str

The full path for the file entry.

required
media_type str

The media type for the file entry.

required

Returns:

Name Type Description
Element Element

A new manifest:file-entry element.

Source code in odfdo/manifest.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
@staticmethod
def make_file_entry(full_path: str, media_type: str) -> Element:
    """Create a new `manifest:file-entry` element.

    Args:
        full_path: The full path for the file entry.
        media_type: The media type for the file entry.

    Returns:
        Element: A new `manifest:file-entry` element.
    """
    tag = (
        f"<manifest:file-entry "
        f'manifest:media-type="{media_type}" '
        f'manifest:full-path="{full_path}"/>'
    )
    return Element.from_tag(tag)

set_media_type

set_media_type(full_path: str, media_type: str) -> None

Set the media type for an existing file entry in the manifest.

Parameters:

Name Type Description Default
full_path str

The full path of the file entry.

required
media_type str

The new media type to set.

required
Source code in odfdo/manifest.py
101
102
103
104
105
106
107
108
109
def set_media_type(self, full_path: str, media_type: str) -> None:
    """Set the media type for an existing file entry in the manifest.

    Args:
        full_path: The full path of the file entry.
        media_type: The new media type to set.
    """
    file_entry = self._file_entry(full_path)
    file_entry.set_attribute("manifest:media-type", media_type)