示例#1
0
文件: test.py 项目: hamilyon/pyraft
 def test_new_term_follower_update(self):
     self.raft.state.votedFor = 'second'
     message = UpdateMessage(term=2,
                             prevLogIndex=-1,
                             prevLogTerm=-1,
                             entries=[Command('1', 1)],
                             leaderCommit=1,
                             fromLeader='second')
     actions = self.raft.receive(message)
     self.assertEqual(actions, [
         Ack(term=2, reply_to=message),
         StateUpdate(
             [Command('1', term=1)], commitIndex=1, term=2, prevLogIndex=-1)
     ])
示例#2
0
文件: test.py 项目: hamilyon/pyraft
 def test_answers_negative_when_log_desnt_match(self):
     action = StateUpdate(
         log=[Command('w', 1),
              Command('y', 2),
              Command('q', 2)],
         prevLogIndex=-1)
     # action.perform(None, self.raft.state)
     message = UpdateMessage(term=3,
                             leaderCommit=2,
                             fromLeader='first',
                             prevLogIndex=2,
                             prevLogTerm=3,
                             entries=[Command('q', 3),
                                      Command('z', 3)])
     answer = self.raft.receive(message)
     self.assertEqual(answer[0].__class__, Nack)
     self.assertEqual(self.raft.state.log, [])
示例#3
0
文件: test.py 项目: hamilyon/pyraft
 def test_old_term_follower_ignore(self):
     self.raft.state.votedFor = 'second'
     self.raft.state.term = 1
     message = UpdateMessage(term=0,
                             prevLogIndex=0,
                             prevLogTerm=0,
                             entries=[Command('1', 1)],
                             leaderCommit=1,
                             fromLeader='second')
     actions = self.raft.receive(message)
     self.assertEqual(actions, [Nack(term=1, reply_to=message)])
示例#4
0
    def __init__(self, parent = None):
        Window.__init__(self,parent)
	#Important must be empty this is a reference
        self.files = []
        self.recent = None
        self.dirty = None
        self.isFull = False
        self.adb = Adb(self)
        self.parser = Parser(self)
        self.command = Command(self)
        self.ant = Ant(self)
        self.updater = update.Updater(self)
        self.update()
示例#5
0
文件: test.py 项目: hamilyon/pyraft
 def test_follower_match_log_vote(self):
     self.raft.state.votedFor = 'second'
     self.raft.state.term = 0
     self.raft.state.log = [Command('1', term=1)]
     message = RequestVoteMessage(
         term=1,
         name='third',
         lastLogIndex=0,
         lastLogTerm=1,
     )
     actions = self.raft.receive(message)
     self.assertEqual(actions, [
         ElectionVote(
             name='first', term=1, reply_to=message, voteGranted=True),
         StateUpdate(votedFor='third', term=1)
     ])
示例#6
0
 def __init__(self, parent=None):
     # pyqt必要的代码
     self.app = QApplication(sys.argv)
     QMainWindow.__init__(self, parent)
     self.ui = main_gui.Ui_MainWindow()
     self.ui.setupUi(self)
     # 程序功能模块(core.py)初始化
     self.afk = Command()
     # 让功能模块(core.py)也能访问UI
     self.afk.utils.ui = self.ui
     # GUI数值初始化
     self.init_interface()
     # 信号初始化
     self.init_signal()
     # 显示GUI
     self.show()
     self.write_log("程序核心初始化完成!")
     # 加载图片资源
     self.afk.utils.load_res()
     self.write_log("图片资源加载完成!")
     self.write_log("afk-arena-tools是开源软件,如果有任何问题建议想法,欢迎提issue或PR~")
     # 检查更新
     Thread(target=self.check_update).start()
     sys.exit(self.app.exec_())
示例#7
0
class MainWindow(Window):
    def __init__(self, parent = None):
        Window.__init__(self,parent)
	#Important must be empty this is a reference
        self.files = []
        self.recent = None
        self.dirty = None
        self.isFull = False
        self.adb = Adb(self)
        self.parser = Parser(self)
        self.command = Command(self)
        self.ant = Ant(self)
        self.updater = update.Updater(self)
        self.update()
        
    def update(self):
        self.updater.start()
                    
    def init(self):
        self.toolBar = Tool(self)
        self.addToolBar(self.toolBar)
        self.initConfig()
        self.treeWidget.initProjects()
        self.connect(self, SIGNAL('triggered()'), self.closeEvent)
        self.connect(self.tabWidget,SIGNAL("dropped"), self.createTabs)
        self.tabWidget.tabCloseRequested.connect(self.closeTab)
        self.treeWidget.itemDoubleClicked.connect(self.treeItemClicked)
        self.connect(self.treeWidget,SIGNAL("openFileClicked"),self.treeItemClicked)
        self.connect(self.treeWidget,SIGNAL("sendFileClicked"),self.sendFile)
        self.connect(self.treeWidget,SIGNAL("create"), lambda x:self.ant.create(x))
        self.connect(self.treeWidget,SIGNAL("build"), lambda x:self.ant.build(x))
        self.connect(self.treeWidget,SIGNAL("buildRun"), lambda x:self.ant.buildRun(x))
        self.connect(self.treeWidget,SIGNAL("clean"), lambda x:self.ant.clean(x))
        self.connect(self.treeWidget,SIGNAL("run"), lambda x:self.ant.run(x))
        #self.initInterpreter()

    def initConfig(self): 
        self.recent = config.recent()
        self.dirty = []
        if(config.files() != None):
            if(len(config.files()) != 0):
                for i in config.files():
                    self.createTab(i)
          
    def treeItemClicked(self,item):
        if(item.isFile()):
            if(item.isDoc()):
                self.createTab(item.getPath())
            elif(item.isPic()):
                self.openImage(item.getPath())
            elif(item.isAudio()):
                self.openAudio(item.getPath())
                
    def sendFile(self,item):
        if(item.isFile()):
            self.command.setCmdText("adb push "+item.getPath()+" /sdcard/")
                
    
        
    def initInterpreter(self):
        self.ipy = PyInterp(self)
        self.ipy.initInterpreter(locals())
        self.outputTabWidget.addTab(self.ipy, "Python")

    '''Must go through this only'''
    def createTab(self,nfile):
        if(nfile != None):
            if(self.files != None):
                if(len(self.files) != 0):
                        if(nfile in self.files):
                            #print "File Already Open\n"+nfile
                            self.tabWidget.setCurrentIndex(self.files.index(nfile))
                            return False
                if(ospathexists(nfile)):
                        self.openEditor(nfile)  
                        return True    
                else:
                    if(nfile in self.files):
                        self.files.remove(nfile)
                    config.setFile(self.files)
                    QMessageBox.about(self,"Can't Open","File Does Not Exist\n"+nfile) 
                    return False
                           
    def createTabs(self,links):
        if(links != None):
            if(len(links) != 0):
                for i in links:
                    self.createTab(i)
            
    def openEditor(self,nfile):
        text = ""
        try:
            infile = open(nfile, 'r')
            tt = infile.read()
            if(config.encoding() == Encoding.UNICODE):
                text = unicode(tt,"utf-8")#must add utf-8 for it to work
            else:
                text = str(tt)

            #infile.close()
            self.files.append(nfile)
            config.setFile(self.files) 
            self.dirty.append(False)
            #print len(self.files)
            tab = Editor(self,text,self.syntax(nfile),self.colorStyle) 
            self.tabWidget.addTab(tab,ospathbasename(nfile))
            tab.textChanged.connect(lambda:self.setDirty(nfile))  
            if(self.files != None):
                if(len(self.files)) != 0:
                    #This line sets the opened file to display first Important not checked
                    self.tabWidget.setCurrentIndex(len(self.files)-1)
            return True
        except:
            if(nfile in self.files):
                self.files.remove(nfile)
            config.setFile(self.files)
            QMessageBox.about(self,"Can't Open","File is Being Used\n"+nfile)
            return False
        finally:
            if(infile != None):
                infile.close()
            
               
    def openImage(self,nfile):
        if(ospathexists(nfile)):
            form = Image(self,nfile)
            form.show()
            return True
        else:
            QMessageBox.about(self,"Can't Open","File Does Not Exist\n"+nfile)
            return False
        #print nfile
        #self.tiler.addImage(nfile)
        #self.tiler.show()
        
    def openAudio(self,nfile):
        if(ospathexists(nfile)):
            form = Audio(self,nfile)
            form.show()
            return True
        else:
            QMessageBox.about(self,"Can't Open","File Does Not Exist\n"+nfile)
            return False
            
    def closeTab(self,index):
        '''Boolean result invocation method.'''
        done = True
        if self.dirty[index]:
            reply = QMessageBox.question(self,
                    "Sabel IDE - Unsaved Changes",
                    "Save unsaved changes?",
                    QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
            if reply == QMessageBox.Cancel:
                done = False
            elif reply == QMessageBox.Yes:
                done = self.fileSave(index)
            elif reply == QMessageBox.No:
                self.clearDirty(index)
                done = True
        if(done):
            #print index
            '''Must set the editor text to None to gc the memory used by text'''
            self.files.remove(self.files[index])
            config.setFile(self.files)
            self.tabWidget.removeTab(index)

    def setDirty(self,file):
        '''On change of text in textEdit window, set the flag
        "dirty" to True'''
        index = self.files.index(file)
        if self.dirty[index]:
            return True
        self.dirty[index] = True
        flbase = ospathbasename(self.files[index])
        self.tabWidget.setTabText(index,"*"+flbase)

    def clearDirty(self,index):
        '''Clear the dirty.'''
        self.dirty[index] = False
        flbase = ospathbasename(self.files[index])
        self.tabWidget.setTabText(index,flbase)

    def fileOpen(self):
        fname = str(QFileDialog.getOpenFileName(self,"Open File", '.', "Files (*.*)"))
        if not (fname == ""):
            if self.files != None:
                if len(self.files) != 0:
                        if(fname in self.files):
                            self.createTab(fname)
                            return True
                        else:
                            QMessageBox.about(self, "Already Open","File Already Open")
                            return False
                else:
                    self.createTab(fname)
            else:
                self.files = []
                self.createTab(fname)

    def fileSave(self):
        if(self.files != None):
            if len(self.files) != 0:
                index = self.tabWidget.currentIndex()
                if not self.dirty[index]:
                    return
                fname = self.files[index]
                try:
                    fl = open(fname, 'w')
                    self.statusSaving()
                    self.progressStart()
                    if(config.encoding() == Encoding.UNICODE):
                        tempText = unicode(self.tabWidget.widget(index).text())
                        fl.write(tempText.encode("utf-8"))
                        fl.close()
                    else:
                        tempText = str(self.tabWidget.widget(index).text())
                        fl.write(tempText)
                        fl.close()
                    self.clearDirty(index)
                except:
                    QMessageBox.about(self, "Can't Save","File is Locked")
                self.statusWriting()
                self.progressStop()
                self.parser.run(self.files[index])
                #must implement for all files

    def fileSaveAll(self):
        def fileSaveIndex(index):
                if not self.dirty[index]:
                    return
                fname = self.files[index]
                try:
                    fl = open(fname, 'w')
                    self.statusSaving()
                    self.progressStart()
                    if(config.encoding() == Encoding.UNICODE):
                        tempText = unicode(self.tabWidget.widget(index).text())
                        fl.write(tempText.encode("utf-8"))
                        fl.close()
                    else:
                        tempText = str(self.tabWidget.widget(index).text())
                        fl.write(tempText)
                        fl.close()
                    self.clearDirty(index)
                    self.statusWriting()
                    self.progressStop()
                except:
                    QMessageBox.about(self, "Can't Save","File is Locked")
        if(self.files != None):
            if len(self.files) != 0:
                for file in self.files:
                    fileSaveIndex(self.files.index(file))


    def closeEvent(self, event):
        self.adb.close()
        self.parser.close()
        self.command.close()
        self.ant.close()
        notSaved = False
        for files in self.dirty:
            if files == True:
                notSaved = True
        if notSaved:
            reply = QMessageBox.question(self,
                                        "Sabel - Unsaved Changes",
                                        "Save unsaved changes?",
                                        QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
            if reply == QMessageBox.Cancel:
                    return
            elif reply == QMessageBox.Yes:
                    self.fileSaveAll()
        sys.exit()
                    
    def syntax(self,nfile):
        lang = 0
        if nfile.endswith(".py"):
            lang = 0
        elif (nfile.endswith(".cpp") or nfile.endswith(".h") or nfile.endswith(".c") or nfile.endswith(".hpp")):
            lang = 1
        elif nfile.endswith(".nut"):
            lang = 2
        elif nfile.endswith(".neko"):
            lang = 2
        return lang
示例#8
0
 def init_core(self):
     self.afk = Command()