summaryrefslogtreecommitdiff
path: root/src/projManagement/newProject.py
blob: 10fb0cb5a8d9af7e790b69d1a33eccf127fc1210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
# =========================================================================
#          FILE: newProject.py
#
#         USAGE: ---
#
#   DESCRIPTION: It is called whenever new project is being called.
#
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Fahim Khan, fahim.elex@gmail.com
#      MODIFIED: Rahul Paknikar, rahulp@iitb.ac.in
#  ORGANIZATION: eSim Team at FOSSEE, IIT Bombay
#       CREATED: Wednesday 12 February 2015
#      REVISION: Sunday 26 July 2020
# =========================================================================

from PyQt5 import QtWidgets
from .Validation import Validation
from configuration.Appconfig import Appconfig
import os
import json


class NewProjectInfo(QtWidgets.QWidget):
    """
    This class is called when User create new Project.
    """

    def __init__(self):
        super(NewProjectInfo, self).__init__()
        self.obj_validation = Validation()
        self.obj_appconfig = Appconfig()

    def createProject(self, projName):
        """
        This function create Project related directories and files.
        Before creating also validates using the `Validation` class

        Validation codes

        - VALID
        - CHECKEXIST
        - CHECKNAME
        - NONE

         @params
            :projName   => name of the project created passed from
                        frontEnd/Application new_project()
         @return
            :dirs        => The directories inside the project folder
            :filelist    => The files inside the project folder

         @params
            :projName   => name of the project created passed from
                        frontEnd/Application new_project()
         @return
            :dirs        => The directories inside the project folder
            :filelist    => The files inside the project folder

        """
        self.projName = projName
        self.workspace = self.obj_appconfig.default_workspace['workspace']
        # self.projName = self.projEdit.text()
        # Remove leading and trailing space
        self.projName = str(self.projName).rstrip().lstrip()

        self.projDir = os.path.join(self.workspace, str(self.projName))

        # Validation for newProject
        if self.projName == "":
            self.reply = "NONE"
        else:
            self.reply = self.obj_validation.validateNewproj(str(self.projDir))

        # Checking Validations Response
        if self.reply == "VALID":
            # create project directory
            try:
                os.mkdir(self.projDir)
                self.close()
                self.projFile = os.path.join(
                    self.projDir, self.projName + ".proj")
                f = open(self.projFile, "w")

            except BaseException:
                self.msg = QtWidgets.QErrorMessage(self)
                self.msg.setModal(True)
                self.msg.setWindowTitle("Error Message")
                self.msg.showMessage(
                    'Unable to create project. Please make sure you have ' +
                    'write permission on ' + self.workspace
                )
                self.msg.exec_()

            # New KiCad v6 file extension
            f.write("schematicFile " + self.projName + ".kicad_sch\n")
            f.close()

            # Now Change the current working project
            newprojlist = []
            # self.obj_appconfig = Appconfig()
            self.obj_appconfig.current_project['ProjectName'] = self.projDir
            newprojlist.append(self.projName + '.proj')
            self.obj_appconfig.project_explorer[self.projDir] = newprojlist

            self.obj_appconfig.print_info(
                'New project created : ' + self.projName)
            self.obj_appconfig.print_info(
                'Current project is : ' + self.projDir)

            json.dump(
                self.obj_appconfig.project_explorer, open(
                    self.obj_appconfig.dictPath["path"], 'w'))
            return self.projDir, newprojlist

        elif self.reply == "CHECKEXIST":
            self.msg = QtWidgets.QErrorMessage(self)
            self.msg.setModal(True)
            self.msg.setWindowTitle("Error Message")
            self.msg.showMessage(
                'The project "' + self.projName +
                '" already exist.Please select the different name or delete' +
                ' existing project'
            )
            self.msg.exec_()
            return None, None

        elif self.reply == "CHECKNAME":
            self.msg = QtWidgets.QErrorMessage(self)
            self.msg.setModal(True)
            self.msg.setWindowTitle("Error Message")
            self.msg.showMessage(
                'The project name should not contain space between them')
            self.msg.exec_()
            return None, None

        elif self.reply == "NONE":
            self.msg = QtWidgets.QErrorMessage(self)
            self.msg.setModal(True)
            self.msg.setWindowTitle("Error Message")
            self.msg.showMessage('The project name cannot be empty')
            self.msg.exec_()
            return None, None

    def cancelProject(self):
        self.close()