eric6/WebBrowser/SpeedDial/SpeedDialWriter.py

Wed, 30 Dec 2020 11:00:44 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 30 Dec 2020 11:00:44 +0100
branch
maintenance
changeset 7924
8a96736d465e
parent 7781
607a6098cb44
child 8218
7c09585bd960
permissions
-rw-r--r--

Updated copyright for 2021.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a class to write speed dial data files.
"""

from PyQt5.QtCore import QXmlStreamWriter, QIODevice, QFile


class SpeedDialWriter(QXmlStreamWriter):
    """
    Class implementing a writer object to generate speed dial data files.
    """
    def __init__(self):
        """
        Constructor
        """
        super(SpeedDialWriter, self).__init__()
        
        self.setAutoFormatting(True)
    
    def write(self, fileNameOrDevice, pages, pagesPerRow, speedDialSize):
        """
        Public method to write a speed dial data file.
        
        @param fileNameOrDevice name of the file to write (string)
            or device to write to (QIODevice)
        @param pages list of speed dial pages (list of Page)
        @param pagesPerRow number of pages per row (integer)
        @param speedDialSize size of the speed dial pages (integer)
        @return flag indicating success (boolean)
        """
        if isinstance(fileNameOrDevice, QIODevice):
            f = fileNameOrDevice
        else:
            f = QFile(fileNameOrDevice)
            if not f.open(QFile.WriteOnly):
                return False
        
        self.setDevice(f)
        return self.__write(pages, pagesPerRow, speedDialSize)
    
    def __write(self, pages, pagesPerRow, speedDialSize):
        """
        Private method to write a speed dial file.
        
        @param pages list of speed dial pages (list of Page)
        @param pagesPerRow number of pages per row (integer)
        @param speedDialSize size of the speed dial pages (integer)
        @return flag indicating success (boolean)
        """
        self.writeStartDocument()
        self.writeDTD("<!DOCTYPE speeddial>")
        self.writeStartElement("SpeedDial")
        self.writeAttribute("version", "1.0")
        
        self.writeStartElement("Pages")
        self.writeAttribute("row", str(pagesPerRow))
        self.writeAttribute("size", str(speedDialSize))
        
        for page in pages:
            self.writeEmptyElement("Page")
            self.writeAttribute("url", page.url)
            self.writeAttribute("title", page.title)
        
        self.writeEndDocument()
        return True

eric ide

mercurial