eric6/Project/ProjectFile.py

Thu, 28 Jan 2021 11:49:28 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 28 Jan 2021 11:49:28 +0100
branch
jsonfiles
changeset 8015
09b24828d787
parent 8012
ecf45f723038
child 8016
e484fc67677e
permissions
-rw-r--r--

Cprrected the use of the typing module.

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

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

"""
Module implementing a class representing the project JSON file.
"""

import json
import time
import typing

from PyQt5.QtCore import QObject

from E5Gui import E5MessageBox
from E5Gui.E5OverrideCursor import E5OverridenCursor

import Preferences

Project = typing.TypeVar("Project")


class ProjectFile(QObject):
    """
    Class representing the project JSON file.
    """
    def __init__(self, project: Project, parent: QObject = None):
        """
        Constructor
        
        @param project reference to the project object
        @type Project
        @param parent reference to the parent object
        @type QObject (optional)
        """
        super(ProjectFile, self).__init__(parent)
        self.__project = project
    
    def writeFile(self, filename: str) -> bool:
        """
        Public method to write the project data to a project JSON file.
        
        @param filename name of the project file
        @type str
        @return flag indicating a successful write
        @rtype bool
        """
        projectDict = {}
        projectDict["header"] = {
            "comment": "eric project file for project {0}".format(
                self.__project.getProjectName()),
            "copyright": "Copyright (C) {0} {1}, {2}".format(
                time.strftime('%Y'),
                self.__project.pdata["AUTHOR"],
                self.__project.pdata["EMAIL"])
        }
        
        # TODO: replace 'XMLTimestamp' by 'Timestamp'
        if Preferences.getProject("XMLTimestamp"):
            projectDict["header"]["saved"] = (
                time.strftime('%Y-%m-%d, %H:%M:%S')
            )
        
        projectDict["project"] = self.__project.pdata
        
        try:
            jsonString = json.dumps(projectDict, indent=2)
            with open(filename, "w") as f:
                f.write(jsonString)
        except (TypeError, EnvironmentError) as err:
            with E5OverridenCursor():
                E5MessageBox.critical(
                    None,
                    self.tr("Save Project File"),
                    self.tr(
                        "<p>The project file <b>{0}</b> could not be "
                        "written.</p><p>Reason: {1}</p>"
                    ).format(filename, str(err))
                )
                return False
        
        return True
    
    def readFile(self, filename: str) -> bool:
        """
        Public method to read the project data from a project JSON file.
        
        @param filename name of the project file
        @type str
        @return flag indicating a successful read
        @rtype bool
        """
        try:
            with open(filename, "r") as f:
                jsonString = f.read()
            projectDict = json.loads(jsonString)
        except (EnvironmentError, json.JSONDecodeError) as err:
            E5MessageBox.critical(
                None,
                self.tr("Read Project File"),
                self.tr(
                    "<p>The project file <b>{0}</b> could not be "
                    "read.</p><p>Reason: {1}</p>"
                ).format(filename, str(err))
            )
            return False
        
        self.__project.pdata = projectDict["project"]
        
        return True

eric ide

mercurial