1. Nikolaj Sjujskij
  2. rst2epub

Source

rst2epub / rst2epub / container.py

from xml.dom.minidom import Document

class Container(Document):
    """Represents container.xml see 3.5.1 in [OCF10]_

       .. [OCF10] OEBPS Container Format (OCF) 1.0
          http://www.idpf.org/ocf/ocf1.0/download/ocf10.htm


        This file is required.
    """

    path = "META-INF/container.xml"
    """Path of the container.xml specified by OCF1.0"""

    namespace = "urn:oasis:names:tc:opendocument:xmlns:container"
    """Namespace for container.xml"""

    version = "1.0"
    """Container file version supported by this implemntation."""

    def __init__(self):
        Document.__init__(self)

        container = self.createElement("container")
        self.appendChild(container)
        container.setAttribute("xmlns", self.namespace)
        container.setAttribute("version", self.version)

        self.rootfiles = self.createElement("rootfiles")
        container.appendChild(self.rootfiles)

    def add_rootfile(self, full_path, media_type):
        """Creates and adds a rootfile elemnt to the container.

            full_path   must be relative to the physical container root
        """
        rootfile = self.createElement("rootfile")
        rootfile.setAttribute("full-path", full_path)
        rootfile.setAttribute("media-type", media_type)

        self.rootfiles.appendChild(rootfile)

    def to_xml(self):
        return self.toprettyxml(indent="    ", encoding="utf-8")