Esempio n. 1
0
def resizeAndSave(billingWidget):
    #def function():
        #print("just inside")
        roll = billingWidget.roll
        database = billingWidget.database
        FIXED_EDGE = 1024 #Fixed longer edge of snap
        rollName = roll.name.split(os.sep)[-1]
        sourcePath = roll.path

        #for an empty roll, do not proceed
        if not roll.snapList:
            QtGui.QMessageBox.information(None, "Message", "There are no snaps to bill", QtGui.QMessageBox.Ok)
            return None

        #handling destinationpath
        trackedPathFile = open(os.getcwd() + os.sep + "resources" + os.sep
                               + "trackedPath.txt", "r")
        openFolder = os.path.expanduser('~')
        for line in trackedPathFile: openFolder = os.sep.join(line.split(os.sep)[:-1])
        print("patha", openFolder)
        destinationPath = QtGui.QFileDialog.getExistingDirectory(None,"Locate the Billed Snaps Folder", openFolder)
        if destinationPath == "": return None
        trackedPathFile.close()


        progress = niggerFiles.customProgressWidget1()
        progress.headerLabel.setText("Resizing and Copying Snaps")
        #count total no of copies to be made
        totalCount = 0
        for snap in roll.snapList:
            if snap.dept: totalCount += 1
            if snap.outsi: totalCount += snap.outsi
            for entry in snap.idList: totalCount += entry[2]
        progress.bar.setRange(0,totalCount)
        progress.messageLabel.setText("copying {} of {} snaps".format(1,totalCount))
        progress.bar.setValue(1)
        progress.show()
        billingWidget.setEnabled(False)

        billingPath = os.path.join(destinationPath, rollName)
        print(billingPath)
        deptPath = os.sep.join(destinationPath.split(os.sep)[:-1]) + os.sep + "Departments"
        outsiPath = os.sep.join(destinationPath.split(os.sep)[:-1]) + os.sep + "Outsi"

        #Create roll folder
        try:
            os.mkdir(billingPath)
        except: #if folder has already been billed
            reply = QtGui.QMessageBox.question(None, 'Message',
                                               "This Folder has already been billed. Do you want\nto go ahead and overwrite the files?",
                                               QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
            if reply == QtGui.QMessageBox.No: #Terminate
                return None
            else: #Delete existing folder, create new one
                shutil.rmtree(billingPath)
                os.mkdir(billingPath)

        #variables to count total no of snaps copied
        rollCount = 0
        deptCount = 0
        outsiCount = 0
        emailCount = 0
        failedResize = 0
        failedCopies = 0

        #print(roll.snapList)
        for snap in roll.snapList:
            #print("hi")
            #Extract name and extension of files with and without extensions
            snapExtension = snap.name.split('.')[-1] if len(snap.name.split('.')) > 1 else ""
            snapName = "".join(snap.name.split('.')[:-1]) if len(snap.name.split('.')) > 1 else snap.name

            infile = open(sourcePath + os.sep + snap.name, 'rb')

            #handling unknown formats
            unsupportedFormat = False
            try:
                #Resize the snap
                im = Image.open(infile)
                width, height = im.size
                size = (FIXED_EDGE, FIXED_EDGE * height / width) if width > height else (FIXED_EDGE * width / height, FIXED_EDGE)
                im.thumbnail(size, Image.ANTIALIAS)
                im.save(sourcePath+os.sep+snap.name)
            except Exception as e:
                print(e)
                failedResize += 1
                unsupportedFormat = True

                continue

            #Copy roll snaps
            idList = snap.idList
            #print("idlist", idList)
            for entry in idList:

                id = entry[1]
                numCopies = entry[2]
                student = database.idValid(id)
                roomDetails = student[4] + "_" + student[5]

                for copyNo in range(numCopies):
                    try:
                        #print("check0045", snapExtension, snapName)
                        if snapExtension: outfile = open(billingPath+os.sep+"{}_{}_{}_{}.{}".format(roomDetails,snapName,copyNo + 1,id,snapExtension), 'wb')
                        else: outfile = open(billingPath+os.sep+"{}_{}_{}_{}".format(roomDetails,snapName,copyNo+1,id), 'wb')
                        if not unsupportedFormat: im.save(outfile) #save resized snap
                        else: #copying without resizing
                            bufferSize = 100000
                            buffer = infile.read(buffersize)
                            while len(buffer):
                                outfile.write(buffer)
                                buffer = infile.read(buffersize)
                        outfile.close()
                        rollCount += 1
                    except:
                        failedCopies += 1

                    progress.messageLabel.setText("copying {} of {} snaps".format(progress.bar.value()+1,totalCount))
                    progress.bar.setValue(progress.bar.value()+1)

            #Copy dept snaps
            if snap.dept:
                #create path if it doesnt exist
                try:
                    os.mkdir(deptPath)
                    os.mkdir(deptPath+os.sep+"{}".format(snap.dept))
                except:
                    try:
                        os.mkdir(deptPath+os.sep+"{}".format(snap.dept))
                    except:
                        pass
                if snapExtension: outfile = open(deptPath+os.sep+"{}"+os.sep+"{}.{}".format(snap.dept,snapName,snapExtension), 'wb')
                else:
                    outfile = open(deptPath+os.sep+"{}"+os.sep+"{}".format(snap.dept,snapName), 'wb')
                try:
                    if not unsupportedFormat: im.save(outfile) #save resized snap
                    else: #copying without resizing
                        bufferSize = 100000
                        buffer = infile.read(buffersize)
                        while len(buffer):
                            outfile.write(buffer)
                            buffer = infile.read(buffersize)
                    outfile.close()
                    deptCount += 1
                except:
                    failedCopies += 1


                progress.messageLabel.setText("copying {} of {} snaps".format(progress.bar.value()+1,totalCount))
                progress.bar.setValue(progress.bar.value()+1)

            #Copy outsi snaps
            if snap.outsi:
                #create path if it doesnt exist
                try:
                    os.mkdir(outsiPath)
                except:
                    pass
                quantity = snap.outsi
                for copyNo in range(quantity):
                    try:
                        if snapExtension: outfile = open(outsiPath+os.sep+"{}_{}.{}".format(snapName,copyNo + 1,snapExtension), 'wb')
                        else: outfile = open(outsiPath+os.sep+"{}_{}".format(snapName,copyNo + 1), 'wb')
                        if not unsupportedFormat: im.save(outfile) #save resized snap
                        else: #copying without resizing
                            bufferSize = 100000
                            buffer = infile.read(buffersize)
                            while len(buffer):
                                outfile.write(buffer)
                                buffer = infile.read(buffersize)
                        outfile.close()
                        outsiCount += 1
                    except:
                        failedCopies += 1

                #Generate/update csv for outsi
                emailList = [entry[1] for entry in snap.emailList ]
                row = snapName + "_1." + snapExtension if snapExtension else snapName + "_1"
                row+=";"
                try:
                    csvFile = open(outsiPath + os.sep + "outsi.csv", 'a+')
                    #add emailIDs with status = 0 to a string
                    for emailID in emailList:
                        row += emailID + "|0|"
                    if row[-1] == "|": row = row[:-1]
                    row += '\n'
                    csvFile.write(row)
                    csvFile.close()
                    emailCount += len(emailList)
                except:
                    pass

                progress.messageLabel.setText("copying {} of {} snaps".format(progress.bar.value()+1,totalCount))
                progress.bar.setValue(progress.bar.value()+1)

            infile.close()

        progress.close()
        billingWidget.setEnabled(True)
        QtGui.QMessageBox.information(None, "Success!!!","Snaps copied successfully\nRoll Snaps copied:\t\t{}\nOutsi Snaps copied:\t\t{}\nDept Snaps Copied:\t\t{}\nEmailIDs written\t\t{}\nSnaps not resized\t\t{}\nSnaps not copied\t\t{}\nPlease make a note of it!!!".format(rollCount,outsiCount,deptCount,emailCount,failedResize,failedCopies), QtGui.QMessageBox.Ok)
        return 1
Esempio n. 2
0
    def loadSnaps(self):
        def loadRoll():
            #Sprint("loading Roll")
            try:
                item = self.navigator.currentItem()
                collectionName = item.text(0)
                #print("collectionName", collectionName)
                list_ = []
                parent = item.parent()
                if parent == None:
                    for i in range(item.childCount()):
                        index = [tempRec[0] for tempRec in self.parent.billingList].index(item.child(i).text(0))
                        list_.append((self.parent.billingList[index][0],self.parent.billingList[index][5]))
                    
                else:
                    index = [tempRec[0] for tempRec in self.parent.billingList].index(item.text(0))
                    list_.append((self.parent.billingList[index][0],self.parent.billingList[index][5]))
                #print("list: ",list_)
            except:
                #print("exceptionnnnnnnnnnnnnnhagu")
                collectionName = self.navigatorField.text()
                list_ = []
                for i in range(self.navigator.topLevelItemCount()):
                    for j in range(self.navigator.topLevelItem(i).childCount()):
                        index = [tempRec[0] for tempRec in self.parent.billingList].index(self.navigator.topLevelItem(i).child(j).text(0))
                        list_.append((self.parent.billingList[index][0],self.parent.billingList[index][5]))
                

            #print("roll assign start")
            self.parent.roll = bill.Roll(None, collectionName, list_)
            #print("roll assign over")

        progress = niggerFiles.customProgressWidget1()
        progress.headerLabel.setText("Loading Snaps")
        progress.bar.setRange(0,self.parent.roll.numSnaps)
        progress.messageLabel.setText("Loading {} of {} snaps".format(0,self.parent.roll.numSnaps))
        progress.bar.setValue(0)
        progress.show()
        loadRoll()
        progress.bar.setRange(0,self.parent.roll.numSnaps)
        progress.messageLabel.setText("Loading {} of {} snaps".format(1,self.parent.roll.numSnaps))

        for i, snap in enumerate(self.parent.roll.snapList):
            for rec in self.parent.billingList:
                if snap.name == rec[0]:
                    rec2 = self.database.idValid(rec[2])
                    gender = rec2[3]
                    id = rec2[0]
                    quantity = rec[4]
                    snap.insertID(len(snap.idList),id,quantity, gender)
            progress.bar.setValue(progress.bar.value() + 1)
            progress.messageLabel.setText("Loading {} of {} snaps".format(i+2,self.parent.roll.numSnaps))       
                    
        #self.parent.propertiesWidget.refresh()

        #print(self.parent.roll.currentPos,self.parent.roll.snapList)
        #print("checking..............")
        #for i,snap in enumerate(self.parent.roll.snapList):
            #print("Total:", snap.numCopies)
            #print("Females:", snap.numFemales)
            #for id in snap.idList: print(id)
        self.parent.displayWidget.loadSnap()
        self.parent.propertiesWidget.refresh()
        self.refresh()
Esempio n. 3
0
            QtGui.QMessageBox.information(None, "Message", "There are no snaps to bill", QtGui.QMessageBox.Ok)
            return None

>>>>>>> 088001604e17d143ebc21dcd1aced053e32305bf
        #handling destinationpath
        trackedPathFile = open(os.getcwd() + os.sep + "resources" + os.sep
                               + "trackedPath.txt", "r")
        openFolder = os.path.expanduser('~')
        for line in trackedPathFile: openFolder = os.sep.join(line.split(os.sep)[:-1])
        print("patha", openFolder)
        destinationPath = QtGui.QFileDialog.getExistingDirectory(None,"Locate the Billed Snaps Folder", openFolder)
        if destinationPath == "": return None
        trackedPathFile.close()
        

        progress = niggerFiles.customProgressWidget1()
        progress.headerLabel.setText("Resizing and Copying Snaps")
        #count total no of copies to be made
        totalCount = 0
        for snap in roll.snapList:
            if snap.dept: totalCount += 1
            if snap.outsi: totalCount += snap.outsi
            for entry in snap.idList: totalCount += entry[2]
        progress.bar.setRange(0,totalCount)
        progress.messageLabel.setText("copying {} of {} snaps".format(1,totalCount))
        progress.bar.setValue(1)
        progress.show()
        billingWidget.setEnabled(False)
        
        billingPath = destinationPath + "\\" + rollName
        deptPath = "\\".join(destinationPath.split("\\")[:-1]) + "\\Departments"
Esempio n. 4
0
    def sortAndSave(self):
        try:
            folders = os.listdir(self.path)
        except:
            return None
        count = 0
        for i in range(self.availableRollsViewer.rowCount()):
            if self.availableRollsViewer.cellWidget(i, 0).isChecked():
                count += 1
        if count == 0:
            return None
        path = os.sep.join(self.path.split(
            os.sep)[:-1]) + os.sep + "Sent for Printing"
        print(path)
        try:
            sentFolders = os.listdir(path)
        except:
            os.mkdir(path)
            sentFolders = []
        num = 0
        for i in range(len(sentFolders)):
            num = max(num, int(sentFolders[i].split('S')[0]))
        num += 1
        path += os.sep + str(num) + "S"
        os.mkdir(path)

        snapList = []
        for i in range(self.availableRollsViewer.rowCount()):
            entry = self.availableRolls[i]
            widget = self.availableRollsViewer.cellWidget(i, 0)
            print(widget)
            if widget.isChecked():
                snapList.extend([[
                    snapName, entry[1]
                ] for snapName in os.listdir(self.path + os.sep + entry[1])])
        hostelList = []
        self.progress = niggerFiles.customProgressWidget1()
        self.progress.headerLabel.setText("Copying Snaps")
        self.progress.bar.setRange(0, len(snapList))
        self.progress.messageLabel.setText("Copying snap {} of {}".format(
            0, len(snapList)))
        self.progress.bar.setValue(self.progress.bar.value() + 1)
        self.progress.show()
        for i, snap in enumerate(snapList):
            self.progress.messageLabel.setText("Copying snap {} of {}".format(
                i + 1, len(snapList)))
            self.progress.bar.setValue(self.progress.bar.value() + 1)
            hostelName = snap[0].split('_')[0]
            if hostelName not in hostelList:
                print(hostelName, hostelList)

                hostelList.append(hostelName)
                os.mkdir(path + os.sep + hostelName)
            bufferSize = 1000000
            infile = open(self.path + os.sep + snap[1] + os.sep + snap[0],
                          'rb')
            outfile = open(path + os.sep + hostelName + os.sep + snap[0], 'wb')
            buffer = infile.read(bufferSize)
            while len(buffer):
                outfile.write(buffer)
                buffer = infile.read(bufferSize)
            infile.close()
            outfile.close()

        self.progress.close()
        if len(self.availableRolls) > 0:
            QtGui.QMessageBox.information(self, "Message",
                                          "Snaps successfilly copied",
                                          QtGui.QMessageBox.Ok)