def flush(self):
        if not self.parentDB.todoAInited:
            return True
            
        if not self.dbOk:
            print("TypeTodo: 'file' db was not properly inited. Saving disabled.")
            return False

        dirty= False
        for iT in self.parentDB.todoA:
            curTodo= self.parentDB.todoA[iT]
            if curTodo.savePending(self.dbId):
                dirty= True

        if dirty or self.maxIdSaved!=self.maxId:
            try:
                with codecs.open(self.settings.file, 'w+', 'UTF-8') as f:
                    f.write(self.settings.head)
                    f.write("\n")

                    for iT in sorted(self.parentDB.todoA):
                        curTodo= self.parentDB.todoA[iT]
                        if curTodo.initial:
                            continue

                        stateSign= curTodo.state
                        if stateSign=='': stateSign='-'

                        lvl= curTodo.lvl
                        if curTodo.lvl>=0: lvl= '+' +str(curTodo.lvl)

                        #runtime GMT time to local
                        gmtTime= time.localtime(curTodo.stamp)

                        f.write(stateSign +', '.join(curTodo.tagsA) +' ' +str(curTodo.id)+ ': ' +' '.join([str(lvl), '"'+curTodo.fileName+'"', curTodo.editor, time.strftime('%y/%m/%d %H:%M:%S', gmtTime)]) +"\n\t" +curTodo.comment +"\n\n")

                    f.write(self.maxId % '\nReserved: %d')


            except Exception as e:
                print("TypeTodo: 'file' db experienced error while flushing")
                print(e)

                return False


        for iT in self.parentDB.todoA:
            self.parentDB.todoA[iT].setSaved(SAVE_STATES.IDLE, self.dbId)

        self.maxIdSaved= self.maxId

        return True




#   newId() is suited to be called in a short sequence, to iplement lowcost retrying.
#   .lastId holds reserved Id and is cached out if matches _wantedId.
#   Id is initially reserved if _wantedId=0, thich is case on every sequence start.
#
#   return actually reserved new Id
#
#   Same is true for other engines.
dataset/ETHPy150Open NikolayRag/typeTodo/dbFile.py/TodoDbFile.flush/swapped operands of `%`