コード例 #1
0
 def __init__(self, parent=None):
     QtGui.QWidget.__init__(self, parent)
     self.thread = Worker()
     self.ui = Ui_YDDFWindow()
     self.ui.setupUi(self)
     self.logger = logging.getLogger('Yahoo DDF Import Log')
     self.logfile = logging.FileHandler('yffdimport.log')
     self.logformatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
     self.logfile.setFormatter(self.logformatter)
     self.logger.addHandler(self.logfile)
     self.logger.setLevel(logging.WARNING)
     self.ui.messageLabel.setText('Please fill out all fields above')
     self.connect(self.thread, QtCore.SIGNAL("finished()"), self.updateUi)
     self.connect(self.thread, QtCore.SIGNAL("terminated()"), self.updateUi)
     self.connect(self.thread, QtCore.SIGNAL("message_output(PyQt_PyObject)"), self.updateMessage)
     self.connect(self.thread, QtCore.SIGNAL("progress_output(PyQt_PyObject)"), self.updateProgress)
     self.connect(self.thread, QtCore.SIGNAL("progress_max_output(PyQt_PyObject)"), self.updateProgressMax)
     self.connect(self.thread, QtCore.SIGNAL("ask_auth()"), self.askAuth)
     self.connect(self.thread, QtCore.SIGNAL("get_auth_pin()"), self.getAuthPin)
     self.loadProfiles()
コード例 #2
0
class MyForm(QtGui.QWidget):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.thread = Worker()
        self.ui = Ui_YDDFWindow()
        self.ui.setupUi(self)
        self.logger = logging.getLogger('Yahoo DDF Import Log')
        self.logfile = logging.FileHandler('yffdimport.log')
        self.logformatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
        self.logfile.setFormatter(self.logformatter)
        self.logger.addHandler(self.logfile)
        self.logger.setLevel(logging.WARNING)
        self.ui.messageLabel.setText('Please fill out all fields above')
        self.connect(self.thread, QtCore.SIGNAL("finished()"), self.updateUi)
        self.connect(self.thread, QtCore.SIGNAL("terminated()"), self.updateUi)
        self.connect(self.thread, QtCore.SIGNAL("message_output(PyQt_PyObject)"), self.updateMessage)
        self.connect(self.thread, QtCore.SIGNAL("progress_output(PyQt_PyObject)"), self.updateProgress)
        self.connect(self.thread, QtCore.SIGNAL("progress_max_output(PyQt_PyObject)"), self.updateProgressMax)
        self.connect(self.thread, QtCore.SIGNAL("ask_auth()"), self.askAuth)
        self.connect(self.thread, QtCore.SIGNAL("get_auth_pin()"), self.getAuthPin)
        self.loadProfiles()



    def startImportButton(self):
        #Disable te UI buttons so we don't get double clicks
        self.ui.startImportButton.setEnabled(False)
        self.ui.chooseDDFButton.setEnabled(False)
        self.ui.profileComboBox.setEnabled(False)
        self.ui.deleteProfileButton.setEnabled(False)
        self.ui.saveProfileButton.setEnabled(False)
        self.ui.filePathTextbox.setEnabled(False)
        self.ui.leagueIDTextbox.setEnabled(False)
        self.ui.teamIDTextbox.setEnabled(False)
        self.ui.overwriteCheckBox.setEnabled(False)

        #Capture the user input and create the output file name
        league_id = str(self.ui.leagueIDTextbox.text())
        user_team = str(self.ui.teamIDTextbox.text())
        ddf_file = str(self.ui.filePathTextbox.text())
        split_ddf_file = ddf_file.partition('.')
        current_date = datetime.date.today().isoformat()
        output_file = split_ddf_file[0]+'-'+current_date+'.'+split_ddf_file[2]
        overwrite_ddf = self.ui.overwriteCheckBox.isChecked()

        #Start the worker thread
        self.thread.doWork(league_id, user_team, ddf_file, current_date, output_file, overwrite_ddf)

    def chooseDDFButton(self):
        #Present a file select dialog box that filter out all files except *.ddf
        file_name = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', "DDF File (*.ddf)")
        self.ui.filePathTextbox.setText(file_name)
        self.ui.filePathTextbox.repaint()

    def saveProfileButton(self):
        if str(self.ui.leagueIDTextbox.text()) in self.profile_leagie_ids:
            self.updateMessage('A profile already exists for this league ID')
        else:
            ask_profile_message = ('Would you like to save this league info in a profile?')
            ask_profile = QtGui.QMessageBox.question(self, 'Create Profile', ask_profile_message,
                                                  QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

            if ask_profile == QtGui.QMessageBox.Yes:
                profile_name, get_profile_name = QtGui.QInputDialog.getText(self, 'Profile Name',
                                                               'Please give your profile a name:')
                if str(profile_name) != '':
                    self.league_profiles[str(profile_name)] = [str(self.ui.leagueIDTextbox.text()),
                                                               str(self.ui.teamIDTextbox.text()),
                                                               str(self.ui.filePathTextbox.text())]
                    with open('data/profiles.json', 'wb') as fp:
                        json.dump(self.league_profiles, fp)
                    self.loadProfiles()
                    self.ui.profileComboBox.setCurrentIndex(self.ui.profileComboBox.findText(profile_name))
                    self.updateMessage('New Profile save.')
                else:
                    self.updateMessage('Please enter a league profile name.')
            else:
                pass

    def deleteProfileButton(self):
        if str(self.ui.profileComboBox.currentText()) == '':
            self.updateMessage('Please select a profile to delete')
        else:
            with open('data/profiles.json', 'rb') as fp:
                self.league_profiles = json.load(fp)
            del self.league_profiles[str(self.ui.profileComboBox.currentText())]
            with open('data/profiles.json', 'wb') as fp:
                        json.dump(self.league_profiles, fp)
            self.loadProfiles()
            self.updateMessage('Profile has been deleted')

    def loadProfiles(self):
        with open('data/profiles.json', 'rb') as fp:
            self.league_profiles = json.load(fp)
        self.profile_leagie_ids = []
        self.ui.profileComboBox.clear()
        self.ui.profileComboBox.addItem('')
        for league_name, value in self.league_profiles.items():
            self.ui.profileComboBox.addItem(league_name)
            self.profile_leagie_ids.append(self.league_profiles[league_name][0])

    def profileComboButton(self):
        if self.ui.profileComboBox.currentText() == '':
            self.ui.leagueIDTextbox.setText('')
            self.ui.teamIDTextbox.setText('')
            self.ui.filePathTextbox.setText('')
        else:
            current_profile = self.league_profiles[str(self.ui.profileComboBox.currentText())]
            self.ui.leagueIDTextbox.setText(current_profile[0])
            self.ui.teamIDTextbox.setText(current_profile[1])
            self.ui.filePathTextbox.setText(current_profile[2])

    def testButton(self):
        league_id = str(self.ui.leagueIDTextbox.text())
        user_team = str(self.ui.teamIDTextbox.text())
        self.ddfWriter.importSchedule(league_id, user_team)

    def askAuth(self):
        ask_message = ("""You must authorize this app the first time you run it. Please click yes to authrize"""+
                       """ this app. You will be redirected to Yahoo. Please click the 'agree' button and"""+
                       """ then copy and paste the code into the next window""")
        ask_auth = QtGui.QMessageBox.question(self, 'Authorization Needed', ask_message,
                                   QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

        if ask_auth == QtGui.QMessageBox.Yes:
            self.thread.ask_auth_answer = 'Yes'
        else:
            self.thread.ask_auth_answer = 'No'

    def getAuthPin(self):
        auth_pin, get_auth_answer = QtGui.QInputDialog.getText(self, 'Auth Request',
                                                               'Enter The Authoriation Code:')
        auth_pin = str(auth_pin)
        self.thread.auth_pin = auth_pin

    def updateMessage(self, PyQt_PyObject):
        self.ui.messageLabel.setText(PyQt_PyObject)

    def updateProgress(self, new_value):
        self.ui.progressBar.setValue(new_value)

    def updateProgressMax(self, max_value):
        self.ui.progressBar.setMaximum(max_value)

    def updateUi(self):
        self.ui.startImportButton.setEnabled(True)
        self.ui.chooseDDFButton.setEnabled(True)
        self.ui.profileComboBox.setEnabled(True)
        self.ui.deleteProfileButton.setEnabled(True)
        self.ui.saveProfileButton.setEnabled(True)
        self.ui.filePathTextbox.setEnabled(True)
        self.ui.leagueIDTextbox.setEnabled(True)
        self.ui.teamIDTextbox.setEnabled(True)
        self.ui.overwriteCheckBox.setEnabled(True)