python3 写的一个通过ftp远程批量处理的工具__界面处理代码
博客专区 > neverGG 的博客 > 博客详情
python3 写的一个通过ftp远程批量处理的工具__界面处理代码
neverGG 发表于2年前
python3 写的一个通过ftp远程批量处理的工具__界面处理代码
  • 发表于 2年前
  • 阅读 111
  • 收藏 6
  • 点赞 1
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

摘要: 此代码主要使用了线程QThread和进度条QProgressDialog来处理程序假死状态
# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import Qt, QFileDialog, QMessageBox, QStandardItemModel,\
    QModelIndex, QAbstractItemView, QThread, QProgressDialog
from ftpFun import ftpInterface
from ftpXML import ftpXml
import os

class PGThread(QThread):
    _signal = QtCore.pyqtSignal(int)
    pgValue = 0
    def __init__(self, parent = None):
        QThread.__init__(self, parent)
        self._signal.connect(self.parent().setPgValue)
    
    def initTask(self, curInfoL, taskID):
        self.curInfo = curInfoL
        self.taskID = taskID
        '''taskID = 1 upload  taskID = 2 remove  taskID = 3 rename'''
        #self.pgdlg.exec_()
        
    def run(self):
        row = 0
        strInfo = ''
        step = int(100 / len(self.curInfo))
        tmpValue = step
        isContinue = True
        for cur in self.curInfo:
            print(cur)
            if(isContinue):        
                ftpObj = ftpInterface(cur['ip'], 21, 'xxx', 'xxxxx')
                ftpObj.setlocalPath('')
                if(self.taskID == 1):
                    upFilePath = self.parent().folderLineEdit.text()
                    print(upFilePath)
                    ret = ftpObj.ftpUpLoadFile(upFilePath)
                    if(ret == -1):
                        isContinue = False
                        strInfo = "未找到/voicePrompt/1文件夹"
                    elif(ret == -3):
                        isContinue = False
                        strInfo = "请检查输入路径下无文件数据"
                    elif(ret == -2):
                        strInfo = "上传时出现异常"
                    else:
                        strInfo = "文件已上传请检查"
                elif(self.taskID == 2):
                    removePath = self.parent().folderLineEdit.text()
                    ret = ftpObj.ftpRemoveFile(removePath)
                    if(ret == -1):
                        isContinue = False
                        strInfo = "未找到/voicePrompt/1文件夹"
                    elif(ret == -2):
                        strInfo = "删除时出现异常有可能文件不存在"
                    else:
                        strInfo = "文件已删除请检查"
                elif(self.taskID == 3):
                    beforeName = self.parent().beforeLineEdit.text()
                    afterName = self.parent().afterLineEdit.text()
                    tmpName = os.path.basename(afterName)
                    dirName = os.path.dirname(beforeName)
                    aName = dirName + '//' + tmpName
                    ret = ftpObj.ftpRenameFile(beforeName, aName)
                    if(ret == -1):
                        isContinue = False
                        strInfo = "未找到/voicePrompt/1文件夹"
                    elif(ret == -2):
                        strInfo = "修改时出现异常有可能文件不存在"
                    else:
                        strInfo = "文件已修改请检查"
            self.parent().Model.setData(self.parent().Model.index(row, 0, QModelIndex()), cur['name'])
            self.parent().Model.setData(self.parent().Model.index(row, 1, QModelIndex()), strInfo)
            row += 1
            tmpValue += step
            tmpValue += 1
            self._signal.emit(tmpValue)
            print(tmpValue)
        self.parent().logTbView.setModel(self.parent().Model)


class Ui_Ftp(object):
    def setupUi(self, Ftp):        
        self.retranslateUi(Ftp)
        QtCore.QMetaObject.connectSlotsByName(Ftp)
        
        '''设置最小化按钮'''
        self.setWindowFlags(Qt.Widget)
        #self.setWindowFlags(Qt.WindowMaximizeButtonHint)
        self.radioButton.setChecked(True)
        self.ftpPathLineEdit.setText("/voicePrompt/1")
        #self.ftpPathLineEdit.setDisabled(True)
        self.ftpPathLineEdit.setReadOnly(True)
        self.hideRenameElemt(False)
        
        self.ckBoxList = [];
        self.checkBoxSignal()
        
        '''信号和槽'''
        self.allCheckBtn.clicked.connect(self.allCheckBtnClicked)
        self.clearBtn.clicked.connect(self.clearBtnClicked)
        self.isShowArea = False
        self.areaShwoBtn.clicked.connect(self.areaShowBtnClicked)
        self.openBtn.clicked.connect(self.openBtnClicked)
        self.execBtn.clicked.connect(self.execBtnClicked)
        
        
        '''operator'''
        self.radioButton.clicked['bool'].connect(self.upLoadSetting)
        self.radioButton_2.clicked['bool'].connect(self.RemoveFileSetting)
        self.radioButton_3.clicked['bool'].connect(self.RenameFileSetting)
        
        '''ftp ip info'''
        ftpX = ftpXml()
        self.ipInfo = ftpX.getServerInfo()
        
        '''exec log'''
        '''设置qtabview'''
        self.Model = QStandardItemModel()

    def setPgValue(self, value):
        if(value >= 100):
            self.pg.close()
            return
        self.pg.setValue(value)
    
    '''具体执行操作'''
    def execBtnClicked(self):
        ret = self.execLimit()
        if(ret == -1):
            return
        curInfoL = self.getAreaIpInfo()
        if(not curInfoL):
            QMessageBox.warning(self, '错误', '请检查serverinfo.xml是否存在', QMessageBox.Ok)
            return
        
        pt = PGThread(self)
        self.pg = QProgressDialog()
        self.pg.setModal(True)
        self.pg.show()
        
        self.Model.clear()
        self.Model.setRowCount(len(curInfoL))
        self.Model.setColumnCount(2)
        self.Model.setHorizontalHeaderLabels(['地区', '执行情况'])
        self.logTbView.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.logTbView.update()
        taskId = 0
        pgTitle = ''
        if(self.radioButton.isChecked()):
            taskId = 1
            pgTitle = "正在上传..."
        elif(self.radioButton_2.isChecked()):
            taskId = 2
            pgTitle = "正在上传..."
        elif(self.radioButton_3.isChecked()):
            taskId = 3
            pgTitle = "正在修改..."
        self.pg.setWindowTitle(pgTitle)
        self.pg.setLabelText("请稍后...")
        self.pg.setCancelButton(None)
        self.pg.setWindowFlags(Qt.WindowMinimizeButtonHint)
        pt.initTask(curInfoL, taskId)
        pt.start()
            
            
    def getAreaIpInfo(self):
        areaStr = self.areaLineEdit.text()
        areaList = areaStr.split('、')
        '''find areaIp in self.ipinfo'''
        curInfo = []
        for area in areaList:
            for ftpArea in self.ipInfo:
                if(area == ftpArea['name']):
                    ip = ftpArea['ip']
                    curInfo.append(ftpArea)
                    break
        return curInfo
    
    def execLimit(self):
        if(self.areaLineEdit.text() == ""):
            QMessageBox.information(self, "提示", "请输入地区,多个地区用'、'隔开", QMessageBox.Ok)


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 3
博文 12
码字总数 4435
×
neverGG
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: