summaryrefslogtreecommitdiff
path: root/src/subcircuit/uploadSub.py
blob: 00cc9df018b3b9d462d5db8b182f2a8b9683b2a9 (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
from PyQt4 import QtGui
from configuration.Appconfig import Appconfig
from projManagement.Validation import Validation
import os
import shutil


class UploadSub(QtGui.QWidget):
    """
    This class contain function for ulaoding subcircuits
    in Subcircuit library present in src folder.
        A folder is created in src/SubcircuitLibrary
    and desired file is moved to that folder.
    """

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

    def upload(self):
        """
        This method opens a dialogue box when Upload subcircuit button is
        clicked and after entering folder name, it opens directory system
        to chose file for folder, it only shows file with extension .sub
        and with the name of project entered earlier as folder name.

            It then validates file if it is in proper format or not, for it
            the file is passed to the function **validateSub** and it returns
            true if file has valid format or else it shows an error message.
        """

        editfile = QtGui.QFileDialog.getOpenFileName(
            None, "Upload Subcircuit File", os.path.expanduser("~"), "*.sub")

        if editfile == '':
            return

        upload = os.path.basename(editfile)
        create_subcircuit, ext = os.path.splitext(upload)

        if ext != '.sub':
            self.msg = QtGui.QErrorMessage(self)
            self.msg.showMessage("Please ensure that filename ends with .sub")
            self.msg.setWindowTitle("Error Message")
            print("Invalid filename")
            return

        valid = self.obj_validation.validateSubcir(editfile, create_subcircuit)
        if not valid:
            self.msg = QtGui.QErrorMessage(self)
            self.msg.showMessage(
                "Content of file does not meet the required format.\
                 Please ensure that file starts with **.subckt \
                 " + create_subcircuit + "** and ends with **.ends \
                 " + create_subcircuit + "**")
            self.msg.setWindowTitle("Error Message")
            print("Invalid file format")
            return

        subcircuit_path = os.path.join(
            os.path.abspath('..'), 'SubcircuitLibrary', create_subcircuit)

        reply = self.obj_validation.validateNewproj(subcircuit_path)

        if reply == "VALID":
            print("Validated: Creating subcircuit directory")
            os.makedirs(subcircuit_path)
            subcircuit = os.path.join(subcircuit_path, upload)

            print("===================")
            print("Current path of subcircuit file is " + editfile)
            print("Selected file is " + upload)
            print("Final path of file is " + subcircuit)
            print("===================")
            shutil.copy(editfile, subcircuit)

        elif reply == "CHECKEXIST":
            print("Project name already exists.")
            print("==========================")
            msg = QtGui.QErrorMessage(self)
            msg.showMessage(
                "The project already exist. Please select  \
                the different name or delete existing project")
            msg.setWindowTitle("Error Message")

        elif reply == "CHECKNAME":
            print("Name can not contain space between them")
            print("===========================")
            msg = QtGui.QErrorMessage(self)
            msg.showMessage(
                'The project name should not contain space between them')
            msg.setWindowTitle("Error Message")