Example #1
0
class Controller(ttk.Frame):
    "represents 'Control' page in the main window notebook"
    def __init__(self, root):
        super().__init__(root)

        self["padding"] = (7, 7, 9, 9)

        self.root = root
        self.fileStorage = self.root.fileStorage

        # file selection
        self.saveToVar = StringVar()
        self.saveToVar.set(True)
        self.fileStorageFrame = FileStorageFrame(self)
        self.fileStorageFrame.grid(column = 1, row = 0, columnspan = 2, sticky = (N, S, E, W),
                                   pady = 5, padx = 2)

        # statusbar
        self.status = StringVar()
        
        self.statusBar = ttk.Label(self, textvariable = self.status)
        self.statusBar.grid(column = 0, row = 4, columnspan = 2, sticky = (S, E, W))
        
        # control button
        self.process = ttk.Button(self, text = "Control", command = self.controlFun)
        self.process.grid(column = 2, row = 4, sticky = E)
        self.process.state(["disabled"])
        
        # report
        self.reportFrame = ttk.LabelFrame(self, text = "Report")
        self.reportFrame.grid(column = 0, row = 0, rowspan = 4, sticky = (N, S, E, W), padx = 5)
        self.reportFrame.columnconfigure(0, weight = 1)
        self.reportFrame.rowconfigure(0, weight = 1)

        self.upFrame = ttk.Frame(self.reportFrame)
        self.upFrame.grid(column = 0, row = 0, columnspan = 2, sticky = (N, S, E, W))
        self.upFrame.columnconfigure(0, weight = 1)
        self.upFrame.rowconfigure(0, weight = 1)
        
        self.contentTree = ttk.Treeview(self.upFrame, selectmode = "none")
        self.contentTree.grid(column = 0, row = 0, sticky = (N, S, E, W))
        self.contentTree["columns"] = ("description", "importance", "tag")
        self.contentTree.column("#0", width = 250, anchor = "w")
        self.contentTree.heading("#0", text = "Problem",
                                 command = lambda: self.orderReport("name"))
        self.contentTree.column("description", width = 200, anchor = "e")
        self.contentTree.heading("description", text = "Description",
                                 command = lambda: self.orderReport("description"))
        self.contentTree.column("importance", width = 60, anchor = "e")
        self.contentTree.heading("importance", text = "Importance",
                                 command = lambda: self.orderReport("importance"))
        self.contentTree.column("tag", width = 10, anchor = "center")
        self.contentTree.heading("tag", text = "Tag", command = lambda: self.orderReport("tag"))
        self.scrollbar = ttk.Scrollbar(self.upFrame, orient = VERTICAL,
                                       command = self.contentTree.yview)
        self.scrollbar.grid(column = 1, row = 0, sticky = (N, S, E))
        self.contentTree.configure(yscrollcommand = self.scrollbar.set)
        
        self.saveToFrame = SaveToFrame(self.reportFrame, label = False)
        self.saveToFrame.grid(column = 0, row = 1, sticky = (E, W))

        self.saveBut = ttk.Button(self.reportFrame, text = "Save", command = self.saveFun)
        self.saveBut.grid(column = 1, row = 1, sticky = E, padx = 2)
    
        self.controlReport = ControlReport(self)
                       
        self.contentTree.tag_bind("file", "<Double-1>", lambda e: self.treeDoubleClick(e))
        self.contentTree.tag_bind("file", "<3>", lambda e: self.filePopUp(e))
        self.contentTree.tag_bind("ok", "<3>", lambda e: self.okPopUp(e))
        self.contentTree.tag_bind("control", "<3>", lambda e: self.controlPopUp(e))
        self.contentTree.tag_configure("comment", background = commentColor())
        
        # method selection frame                
        self.controlFrame = ControlFrame(self)
        self.controlFrame.grid(column = 1, row = 1, columnspan = 2, sticky = (N, W), padx = 10,
                               pady = 55)

        # time frame
        self.timeFrame = TimeFrame(self)
        self.timeFrame.grid(column = 1, row = 2, columnspan = 2, sticky = (N, W), padx = 10)


        self.columnconfigure(0, weight = 1)
        self.rowconfigure(2, weight = 1)


    def orderReport(self, byWhat):
        "orders control report"
        opened = {}
        for child in self.contentTree.get_children():
            opened[child] = self.contentTree.item(child, "open")
            self.contentTree.delete(child)
        self.controlReport.orderedBy = byWhat
        self.controlReport.updateTree()
        for child in self.contentTree.get_children():
            if child in opened:
                self.contentTree.item(child, open=opened[child])


    def refresh(self):
        "refreshes the tree after adding a comment"
        self.controlReport.clear(clearAll = False)
        self.controlReport.updateTree()
        

    def filePopUp(self, event):
        "pop-up menu for file item in the tree"
        menu = Menu(self, tearoff = 0)
        item = self.contentTree.identify("item", event.x, event.y)
        name = item.rstrip("0123456789")
        if name in self.fileStorage.tagged:
            menu.add_command(label = "Remove tag", command = lambda: self.removeTag(name))
        else:
            menu.add_command(label = "Add tag", command = lambda: self.addTag(name))
        menu.add_command(label = "Add comment", command = lambda: Comment(self, name))
        menu.add_separator()
        label = "Open arena file" if m.files == "pair" else "Open file"
        menu.add_command(label = label, command = lambda: self.openFile("arena", name))
        if m.files == "pair":
            menu.add_command(label = "Open room file",
                             command = lambda: self.openFile("room", name))
        menu.post(event.x_root, event.y_root)
        
    def controlPopUp(self, event):
        "pop-up menu for control item in the tree"
        menu = Menu(self, tearoff = 0)
        item = self.contentTree.identify("item", event.x, event.y)
        menu.add_command(label = "Add tags to all problem files",
                         command = lambda: self.addMoreTags(item, specified = ["Problem"]))
        menu.add_command(label = "Add tags to all files with at least a warning",
                         command = lambda: self.addMoreTags(item, specified =
                                                            ["Problem", "Warning"]))
        menu.add_command(label = "Add tags to all files with at least a concern",
                         command = lambda: self.addMoreTags(item, specified =
                                                            ["Problem", "Warning", "Concern"]))
        menu.add_separator()
        menu.add_command(label = "Remove tags from all problem files",
                         command = lambda: self.removeMoreTags(item, specified = ["Problem"]))
        menu.add_command(label = "Remove tags from all files with at least a warning",
                         command = lambda: self.removeMoreTags(item, specified =
                                                               ["Problem", "Warning"]))
        menu.add_command(label = "Remove tags from all files with at least a concern",
                         command = lambda: self.removeMoreTags(item, specified =
                                                               ["Problem", "Warning", "Concern"]))
        menu.post(event.x_root, event.y_root)
        
    def okPopUp(self, event):
        "pop-up menu for rest of files item in the tree"
        menu = Menu(self, tearoff = 0)
        item = self.contentTree.identify("item", event.x, event.y)
        menu.add_command(label = "Add tags to all OK files",
                         command = lambda: self.addMoreTags(item))
        menu.add_command(label = "Remove tags from all OK files",
                         command = lambda: self.removeMoreTags(item))
        menu.post(event.x_root, event.y_root)
        
    def openFile(self, frame, arenafile):
        "opens selected file; called from filePopUp"
        if frame == "room":
            if arenafile in self.fileStorage.pairedfiles:
                roomfile = self.fileStorage.pairedfiles[arenafile]
            else:
                if "Arena" in os.path.basename(arenafile):
                    splitName = os.path.split(arenafile)
                    roomfile = os.path.join(splitName[0], splitName[1].replace("Arena", "Room"))                    
                elif "arena" in os.path.basename(arenafile):
                    splitName = os.path.split(arenafile)
                    roomfile = os.path.join(splitName[0], splitName[1].replace("arena", "room"))
            if roomfile:
                os.startfile(roomfile)
            else:
                self.bell()
        elif frame == "arena":
            os.startfile(arenafile)

    def addTag(self, name):
        "adds tag to a single file"
        for num in range(len(self.controlReport.controls)):
            self.contentTree.set(name + str(num), "tag", "x")
        self.fileStorage.tag(name)
        
    def removeTag(self, name):
        "removes tag from a single file"
        for num in range(len(self.controlReport.controls)):
            self.contentTree.set(name + str(num), "tag", " ")
        self.fileStorage.tagged.remove(name)
        
    def addMoreTags(self, item, specified = False):
        "adds tags to more files; specified used for importance, e.g. ['Problem', 'Warning']"
        num = self.contentTree.index(item)
        for child in self.contentTree.get_children(item):
            name = child.rstrip("0123456789")
            if specified and not self.contentTree.set(name + str(num), "importance") in specified:
                next
            else:
                self.addTag(name)

    def removeMoreTags(self, item, specified = False):
        "removes tags to more files; specified used for importance, e.g. ['Problem', 'Warning']"
        num = self.contentTree.index(item)
        for child in self.contentTree.get_children(item):
            name = child.rstrip("0123456789")
            if specified and not self.contentTree.set(name + str(num), "importance") in specified:
                next
            else:
                if name in self.fileStorage.tagged:
                    self.removeTag(name)    

       
    def treeDoubleClick(self, event):
        "shows tracks in a ShowTracks toplevel window"
        item = self.contentTree.identify("item", event.x, event.y)
        name = item.rstrip("0123456789")
        tracks = self.controlReport.files # <- zmenit aby zobrazovalo serazene
        if item:
            showTracks = ShowTracks(self, nameA = name, tracks = tracks, controlled = True)


    def controlFun(self):
        "processes selected files, clears report, shows results in a report"

        # progressbar
        if len(self.fileStorage) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.fileStorage),
                                                 text = "controlled")

        # initialization
        controls = self.controlFrame.controlsGet() # selected controls
        self.controlReport.clear()  # clears report
        self.controlReport.addControls(controls) # adds selected controls to ControlReport

        self.problemOccured = False
        # processing      
        for file in self.fileStorage:
            tag = "x" if file in self.fileStorage.tagged else " "
            try:
                if file in self.fileStorage.pairedfiles:
                    cm = m.CL(file, self.fileStorage.pairedfiles[file])
                else:
                    cm = m.CL(file, "auto")
            except Exception:
                self.problemOccured = True
                for control in controls:
                    self.controlReport.addFile((control[0], [file, "Failed to load!", "Problem",
                                                             9999999, tag]))
            else:
                for control in controls:
                    try:
                        assessment = self.assessImportance(cm = cm, control = control, file = file)
                        assessment[1].append(tag)
                    except Exception:
                        self.problemOccured = True
                        assessment = (control[0], [file, "Failed to compute!", "Problem",
                                                   9999998, tag])
                    self.controlReport.addFile(assessment)
                     
            if len(self.fileStorage) > 1:
                if self.stoppedProcessing:
                    return
                else:
                    self.progressWindow.addOne()
                    
        self.controlReport.updateTree()

        # progressbar and status
        if len(self.fileStorage) > 1:
            self.progressWindow.destroy()
            if self.problemOccured:
                self.status.set("Files were not processed successfully!")
                self.bell()
            else:
                self.status.set("Files were processed successfully.")
        else:
            if self.problemOccured:
                self.status.set("File was not processed successfully!")
                self.bell()
            else:
                self.status.set("File was processed successfully.")          
        

    def assessImportance(self, cm, control, file):
        "method needed for evaluation of importance of results from CM class' control methods"
        method = control[0]
        startTime, time = int(self.timeFrame.startTimeVar.get()), int(self.timeFrame.timeVar.get())
        results = eval("cm.{}".format(control[1]))
        description = ""
        importance = ""
        value = 0
        
        if method == "Reflections":
            if results[1] * 3 + results[0] > 5:
                importance = "Problem"
            elif results[1] * 3 + results[0] > 2:
                importance = "Warning"
            elif results[0] > 0:
                importance = "Concern"
            else:
                importance = "OK"
            if results[1] != 1:
                description = "{} points problematic, {} of concern".format(results[1], results[0])
            else:
                description = "{} point problematic, {} of concern".format(results[1], results[0])
            value = results[1]

            self.fileStorage.saveReflections(file = file, points = results[2] + results[3])
            
        elif method == "Outside Points":
            if results > 250:
                importance = "Problem"
            elif results > 50:
                importance = "Warning"
            elif results > 5:
                importance = "Concern"
            else:
                importance = "OK"
            if results != 1:
                description = "{} points outside of arena".format(results)
            else:
                description = "1 point outside of arena"
            value = results

        elif method == "Bad Points":
            results = float(results)
            if results > 10:
                importance = "Problem"
            elif results > 5:
                importance = "Warning"
            elif results > 2:
                importance = "Concern"
            else:
                importance = "OK"
            description = "{0:.2f}% bad points".format(results)
            value = results            
                    
        return (method, [cm.nameA, description, importance, value])

            
    def saveFun(self):
        "writes results from controlReport to selected file"
        output = self.saveToFrame.saveToVar.get()
        if not self.controlReport.files:
            self.bell()
            self.status.set("No results prepared for saving.")
            return
        if not output:
            self.bell()
            self.status.set("You have to select a name of a file.")
            return
        
        separator = optionGet("ResultSeparator", ",", "str", True)
        results = separator.join(["File"] + self.controlReport.controls)
        for file in self.controlReport.files:
            filename = returnName(filename = file, allFiles = self.controlReport.files)
            result = [filename]
            for control in self.controlReport.controls:
                result += [i[3] for i in self.controlReport.results[control] if i[0] == file]
            results += "\n" + separator.join(map(str, result))
     
        writeResults(output, results)
        self.status.set("Results were saved.")
        

    def checkProcessing(self):
        "method updating page after change of notebook tab"
        self.controlReport.clear(clearAll = False)
        self.controlReport.updateTree()
        
        if self.fileStorage.arenafiles:
            self.process.state(["!disabled"])
        else:
            self.process.state(["disabled"])

        if self.fileStorage.arenafiles or self.fileStorage.wrongfiles:
            self.fileStorageFrame.removeFiles.state(["!disabled"])
        else:
            self.fileStorageFrame.removeFiles.state(["disabled"])

        self.fileStorageFrame.chosenVar.set(len(self.fileStorage))
        self.fileStorageFrame.nonMatchingVar.set(len(self.fileStorage.wrongfiles))
Example #2
0
class Processor(ttk.Frame):
    "represents 'Process' page in the main window notebook"
    def __init__(self, root):
        super().__init__(root)

        self["padding"] = (10, 10, 12, 12)
        self.root = root
        self.fileStorage = self.root.fileStorage
        self.selectedBatchTime = optionGet("LastBatchTime", [(0, 20)], "list") # zkontrolovat ""

        # variables    
        self.status = StringVar()
        self.useBatchTimeVar = BooleanVar()
              
        # frames
        self.parametersF = ParameterFrame(self)
        self.fileStorageFrame= FileStorageFrame(self)
        self.saveToFrame = SaveToFrame(self, parent = "processor")
        self.optionFrame = OptionFrame(self, text = "Options")
        self.timeLabFrame = ttk.Labelframe(self, text = "Time")
        self.timeLabFrame.root = self
        self.timeFrame = TimeFrame(self.timeLabFrame)
        
        # buttons
        self.process = ttk.Button(self, text = "Process Files", command = self.processFun,
                                  state = "disabled")
        self.useBatchTime = ttk.Checkbutton(self.timeLabFrame, text = "Use batch time",
                                            variable = self.useBatchTimeVar,
                                            command = self.toggledUseBatchTime)
        self.setBatchTimeBut = ttk.Button(self.timeLabFrame, text = "Set", width = 3,
                                          command = self.setBatchTime)

        # labels
        self.statusBar = ttk.Label(self, textvariable = self.status)
        self.modeLab = ttk.Label(self, text = m.fullname[m.mode], font = ("Helvetica", "16"))
       
        # adding to grid
        self.parametersF.grid(column = 0, row = 3, columnspan = 4, sticky = (N, W), padx = 4)
        self.fileStorageFrame.grid(column = 3, row = 0, pady = 5, padx = 4)
        self.timeLabFrame.grid(column = 1, row = 5, padx = 30, sticky = (N, W))
        self.timeFrame.grid(column = 0, row = 0, columnspan = 2, sticky = (E, W))
        
        self.process.grid(column = 3, row = 7, sticky = (S, E), padx = 4, pady = 3)
        self.useBatchTime.grid(column = 0, row = 1, pady = 5)
        self.setBatchTimeBut.grid(column = 1, row = 1, pady = 5)

        self.statusBar.grid(column = 0, row = 7, columnspan = 3, sticky = (N, S, E, W), padx = 6,
                            pady = 3)
        self.modeLab.grid(column = 0, row = 0)
       
        self.saveToFrame.grid(column = 1, row = 1, columnspan = 3, sticky = (N, S, E, W),
                              padx = 6, pady = 2)

        self.optionFrame.grid(column = 0, row = 5, sticky = (N, E), padx = 6)
  
        # what should be enlarged
        self.columnconfigure(2, weight = 1)
        self.rowconfigure(2, weight = 3)
        self.rowconfigure(4, weight = 2)
        self.rowconfigure(6, weight = 2)


    def processCheck(self):
        "checks whether all inputs are valid - helper function for processFun"

        if self.saveToFrame.saveToVar.get() == "":
            raise Exception("You have to choose an output file!")
        
        if len(self.fileStorage) == 0:
            raise Exception("You haven't chosen any file!")

        try:
            startTime = float(self.timeFrame.startTimeVar.get())
        except Exception:
            raise Exception("Start time has to be a number!")
        try:
            time = float(self.timeFrame.timeVar.get())
        except Exception:
            raise Exception("Stop time has to be a number!")
        
        if startTime >= time:
            raise Exception("Start time must be smaller than stop time!")

        if time < 0 or startTime < 0:
            raise Exception("Time has to be set to a positive value")

        if not os.path.exists(os.path.split(self.saveToFrame.saveToVar.get())[0]):
            if os.path.split(self.saveToFrame.saveToVar.get())[0]:
                raise Exception("Pathname of the output file doesn't exist!")
            
        if len(self.filesToProcess) == 0:
            raise Exception("There is no file selected for processing!")
      
        
    def processFun(self):
        "processes chosen files and saves the results in the save-to file"
        # files to be processed
        self.filesToProcess = [file for file in self.fileStorage if \
                               self.optionFrame.processFile(file)]

        # checking for mistakes
        try:
            self.processCheck()
        except Exception as e:
            self.bell()
            self.status.set(e)
            return                   

        # progressWindow
        if len(self.filesToProcess) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.filesToProcess))     

        # initializations
        output = self.saveToFrame.saveToVar.get()
        if not os.path.splitext(output)[1]:
            output = output + optionGet("DefProcessOutputFileType", ".txt", "str", True)
        if not os.path.dirname(output):
            output = os.path.join(optionGet("ResultDirectory", os.getcwd(), "str", True), output)
        startTime = float(self.timeFrame.startTimeVar.get())
        time = float(self.timeFrame.timeVar.get())
        separator = optionGet("ResultSeparator", ",", "str", True)
        batchTime = self.selectedBatchTime if self.useBatchTimeVar.get() else None
        self.someProblem = False
        developer = optionGet("Developer", False, 'bool', True)

        # selected methods          
        methods = OrderedDict()
        parameters = m.parameters
        for name, par in parameters.items():
            if eval("self.parametersF.%sVar.get()" % (name.replace(" ", ""))):
                options = {name: optionGet(*option[0]) for name, option in par.options.items()}
                if not self.useBatchTimeVar.get():
                    methods[name] = [methodcaller(par.method, startTime = startTime,
                                                  time = time, **options)]
                elif name not in parameters.noBatch:
                    methods[name] = [methodcaller(par.method, startTime = times[0],
                                                  time = times[1], **options)
                                     for times in batchTime]
                else:
                    methods[name] = [methodcaller(par.method, **options)]

        # log
        self.log = Log(methods, startTime, time, self.filesToProcess, self.fileStorage,
                       self.optionFrame.removeReflectionsWhere.get(), output,
                       batchTime = batchTime)
                    
        # results header
        if self.useBatchTimeVar.get():
            results = ["File"]
            for method in methods:
                if method in parameters.noBatch:
                    results.append(method)
                else:
                    results.extend([method + " ({}-{})".format(start, end) for
                                    start, end in self.selectedBatchTime])
            results = separator.join(results)
        else:
            results = separator.join(["File"] + [method for method in methods])
        if self.optionFrame.saveTags.get():
            results += separator + "Tag"
        if self.optionFrame.saveComments.get():
            results += separator + "Comment"
        
        # computing of results
        for file in self.filesToProcess:
            # loading of cm object
            if methods:
                try:
                    if file in self.fileStorage.pairedfiles:
                        cm = m.CL(file, self.fileStorage.pairedfiles[file])
                    else:
                        cm = m.CL(file, "auto")

                    if self.optionFrame.removeReflections(file):
                        cm.removeReflections(points = self.fileStorage.reflections.get(file,
                                                                                            None))
                except Exception as e:
                    if developer:
                        print(e)   
                    filename = returnName(filename = file, allFiles =
                                          self.fileStorage.arenafiles) 
                    results += "\n" + filename + "{}NA".format(separator) * len(methods)
                    self.log.failedToLoad.append(file)
                    self.someProblem = True
                    continue
                                
            result = []

            for name, funcs in methods.items():
                for func in funcs:                        
                    try:
                        #if method[2] == "custom":
                            #exec("from Stuff.Parameters import {}".format(method[5]))               
                        result.append(func(cm))
                    except Exception as e:
                        if developer:
                            print(e)   
                        result.append("NA")
                        self.log.methodProblems[name].append(file)
                        self.someProblem = True

            result = separator.join(map(str, result))
            if methods:
                result = separator + result               
            filename = returnName(filename = file, allFiles = self.fileStorage.arenafiles)      
            results += "\n" + filename + result
            
            # tag inclusion in results
            if self.optionFrame.saveTags.get(): 
                if file in self.fileStorage.tagged:
                    results += separator + "1"
                else:
                    results += separator + "0"

            # comment inclusion in results
            if self.optionFrame.saveComments.get():
                results += separator + self.fileStorage.comments[file]
                    
            # progress window update
            if len(self.filesToProcess) > 1:
                if self.stoppedProcessing:
                    writeResults(output, results)
                    self.log.stopped = file
                    self.log.writeLog()
                    self.status.set("Processing stopped")
                    return
                else:
                    self.progressWindow.addOne()

        # results and log writing, ending of processing
        writeResults(output, results)
        self.log.writeLog()
        self._setStatusEndProgressWindow()

        if self.someProblem:
            ProcessingProblemDialog(self, self.log.filename, output)
        elif self.optionFrame.showResults.get():
            os.startfile(output)   

            
    def _setStatusEndProgressWindow(self):
        if len(self.filesToProcess) > 1:
            if self.someProblem:
                self.status.set("Files were processed.")
            else:
                self.status.set("Files were processed successfully.")
            self.progressWindow.destroy()
        else:
            if self.someProblem:
                self.status.set("File was processed.")
            else:
                self.status.set("File was processed successfully.")        


    def toggledUseBatchTime(self):
        "called when batch time checkbutton is toggled; disables or enables changing time"
        if self.useBatchTimeVar.get():
            self.timeFrame.totalTime.state(["disabled"])
            self.timeFrame.startTime.state(["disabled"])
        else:
            self.timeFrame.totalTime.state(["!disabled"])
            self.timeFrame.startTime.state(["!disabled"])


    def setBatchTime(self):
        SetBatchTime(self)
        if not self.useBatchTimeVar.get():
            self.useBatchTimeVar.set(True)
            self.toggledUseBatchTime()        

                
    def checkProcessing(self):
        "method updating page after change of notebook tab"
        if self.fileStorage.arenafiles and self.saveToFrame.saveToVar.get():
            self.process.state(["!disabled"])
        else:
            self.process.state(["disabled"])

        if self.fileStorage.arenafiles or self.fileStorage.wrongfiles:
            self.fileStorageFrame.removeFiles.state(["!disabled"])
        else:
            self.fileStorageFrame.removeFiles.state(["disabled"])        

        self.fileStorageFrame.chosenVar.set(len(self.fileStorage))
        self.fileStorageFrame.nonMatchingVar.set(len(self.fileStorage.wrongfiles))
Example #3
0
class Controller(ttk.Frame):
    "represents 'Control' page in the main window notebook"
    def __init__(self, root):
        super().__init__(root)

        self["padding"] = (7, 7, 9, 9)

        self.root = root
        self.fileStorage = self.root.fileStorage

        # file selection
        self.saveToVar = StringVar()
        self.saveToVar.set(True)
        self.fileStorageFrame = FileStorageFrame(self)
        self.fileStorageFrame.grid(column = 1, row = 0, columnspan = 2, sticky = (N, S, E, W),
                                   pady = 5, padx = 2)

        # statusbar
        self.status = StringVar()
        
        self.statusBar = ttk.Label(self, textvariable = self.status)
        self.statusBar.grid(column = 0, row = 4, columnspan = 2, sticky = (S, E, W))
        
        # control button
        self.process = ttk.Button(self, text = "Control", command = self.controlFun)
        self.process.grid(column = 2, row = 4, sticky = E)
        self.process.state(["disabled"])
        
        # report
        self.reportFrame = ttk.LabelFrame(self, text = "Report")
        self.reportFrame.grid(column = 0, row = 0, rowspan = 4, sticky = (N, S, E, W), padx = 5)
        self.reportFrame.columnconfigure(0, weight = 1)
        self.reportFrame.rowconfigure(0, weight = 1)

        self.upFrame = ttk.Frame(self.reportFrame)
        self.upFrame.grid(column = 0, row = 0, columnspan = 2, sticky = (N, S, E, W))
        self.upFrame.columnconfigure(0, weight = 1)
        self.upFrame.rowconfigure(0, weight = 1)
        
        self.contentTree = ttk.Treeview(self.upFrame, selectmode = "none")
        self.contentTree.grid(column = 0, row = 0, sticky = (N, S, E, W))
        self.contentTree["columns"] = ("description", "importance")
        self.contentTree.column("#0", width = 250, anchor = "w")
        self.contentTree.heading("#0", text = "Problem",
                                 command = self.orderReportByNames)
        self.contentTree.column("description", width = 200, anchor = "e")
        self.contentTree.heading("description", text = "Description",
                                 command = self.orderReportByResults)
        self.contentTree.column("importance", width = 60, anchor = "e")
        self.contentTree.heading("importance", text = "Importance",
                                 command = self.orderReportByImportance)      
        self.scrollbar = ttk.Scrollbar(self.upFrame, orient = VERTICAL,
                                       command = self.contentTree.yview)
        self.scrollbar.grid(column = 1, row = 0, sticky = (N, S, E))
        self.contentTree.configure(yscrollcommand = self.scrollbar.set)
        
        self.saveToFrame = SaveToFrame(self.reportFrame, label = False)
        self.saveToFrame.grid(column = 0, row = 1, sticky = (E, W))

        self.saveBut = ttk.Button(self.reportFrame, text = "Save", command = self.saveFun)
        self.saveBut.grid(column = 1, row = 1, sticky = E, padx = 2)
    
        self.controlReport = ControlReport() #!
                       
        self.contentTree.tag_bind("file", "<Double-1>", lambda e: self.treeDoubleClick(e))
        
        # method selection frame                
        self.controlFrame = ControlFrame(self)
        self.controlFrame.grid(column = 1, row = 1, columnspan = 2, sticky = (N, W), padx = 10,
                               pady = 55)


        # time frame
        self.timeFrame = TimeFrame(self)
        self.timeFrame.grid(column = 1, row = 2, columnspan = 2, sticky = (N, W), padx = 10)


        self.columnconfigure(0, weight = 1)
        self.rowconfigure(2, weight = 1)


    def orderReportByNames(self):
        opened = {}
        for child in self.contentTree.get_children():
            opened[child] = self.contentTree.item(child, "open")
            self.contentTree.delete(child)
        self.controlReport.orderedBy = "name"
        self.controlReport.updateTree(self, deleteExisting = True)
        for child in self.contentTree.get_children():
            if child in opened:
                self.contentTree.item(child, open=opened[child])

    def orderReportByResults(self):
        opened = {}
        for child in self.contentTree.get_children():
            opened[child] = self.contentTree.item(child, "open")
            self.contentTree.delete(child)
        self.controlReport.orderedBy = "description"
        self.controlReport.updateTree(self, deleteExisting = True)
        for child in self.contentTree.get_children():
            if child in opened:
                self.contentTree.item(child, open=opened[child])

    def orderReportByImportance(self):
        opened = {}
        for child in self.contentTree.get_children():
            opened[child] = self.contentTree.item(child, "open")
            self.contentTree.delete(child)
        self.controlReport.orderedBy = "importance"
        self.controlReport.updateTree(self, deleteExisting = True)        
        for child in self.contentTree.get_children():
            if child in opened:
                self.contentTree.item(child, open=opened[child])
                
        
    def treeDoubleClick(self, event):
        "shows tracks in a ShowTracks toplevel window"
        item = self.contentTree.identify("item", event.x, event.y)
        name = item.rstrip("0123456789")
        tracks = self.controlReport.files # <- zmenit aby zobrazovalo serazene
        if item:
            time = (self.timeFrame.startTimeVar.get(), self.timeFrame.timeVar.get())
            showTracks = ShowTracks(self, nameA = name, tracks = tracks, time = time,
                                    controlled = True)


    def controlFun(self):
        "processes selected files, clears report, shows results in a report"

        # progressbar
        if len(self.root.fileStorage.arenafiles) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.root.fileStorage.arenafiles),
                                                 text = "controlled")

        # initialization
        controls = self.controlFrame.controlsGet() # selected controls
        self.controlReport.clear(self)  # clears report
        self.controlReport.addControls(controls) # adds selected controls to ControlReport

        self.problemOccured = False
        # processing      
        for file in self.root.fileStorage.arenafiles:
            try:
                if file in self.root.fileStorage.pairedfiles:
                    cm = CM(file, nameR = self.root.fileStorage.pairedfiles[file])
                else:
                    cm = CM(file, nameR = "auto")
            except Exception:
                self.problemOccured = True
                for control in controls:
                    self.controlReport.addFile((control[0], (file, "Failed to load!", "Problem",
                                                             9999999)))
            else:
                for control in controls:
                    try:
                        assessment = self.assessImportance(cm = cm, control = control, file = file)
                    except Exception:
                        self.problemOccured = True
                        assessment = (control[0], (file, "Failed to compute!", "Problem",
                                                   9999998))
                    self.controlReport.addFile(assessment)
                     
            if len(self.root.fileStorage.arenafiles) > 1:
                if self.stoppedProcessing:
                    return
                else:
                    self.progressWindow.addOne()
                    
        self.controlReport.updateTree(self)

        # progressbar and status
        if len(self.root.fileStorage.arenafiles) > 1:
            self.progressWindow.destroy()
            if self.problemOccured:
                self.status.set("Files were not processed successfully!")
                self.bell()
            else:
                self.status.set("Files were processed successfully.")
        else:
            if self.problemOccured:
                self.status.set("File was not processed successfully!")
                self.bell()
            else:
                self.status.set("File was processed successfully.")          
        

    def assessImportance(self, cm, control, file):
        "method needed for evaluation of importance of results from CM class' control methods"
        method = control[0]
        results = eval("cm.{}()".format(control[1]))
        description = ""
        importance = ""
        value = 0
        
        if method == "Reflections":
            if results[1] * 3 + results[0] > 5:
                importance = "Problem"
            elif results[1] * 3 + results[0] > 2:
                importance = "Warning"
            elif results[0] > 0:
                importance = "Concern"
            else:
                importance = "OK"
            if results[1] != 1:
                description = "{} points problematic, {} of concern".format(results[1], results[0])
            else:
                description = "{} point problematic, {} of concern".format(results[1], results[0])
            value = results[1]

            self.root.fileStorage.saveReflections(file = file, points = results[2] + results[3])
            
        elif method == "Outside Points":
            if results > 250:
                importance = "Problem"
            elif results > 50:
                importance = "Warning"
            elif results > 5:
                importance = "Concern"
            else:
                importance = "OK"
            if results != 1:
                description = "{} points outside of arena".format(results)
            else:
                description = "1 point outside of arena"
            value = results

        elif method == "Bad Points":
            if results > 10:
                importance = "Problem"
            elif results > 5:
                importance = "Warning"
            elif results > 2:
                importance = "Concern"
            else:
                importance = "OK"
            description = "{0:.2f}% bad points".format(results)
            value = results            
                    
        return (method, (cm.nameA, description, importance, value))

            
    def saveFun(self):
        "writes results from controlReport to selected file"
        output = self.saveToFrame.saveToVar.get()
        if not self.controlReport.files:
            self.bell()
            self.status.set("No results prepared for saving.")
            return
        if not output:
            self.bell()
            self.status.set("You have to select a name of a file.")
            return
        
        separator = optionGet("ResultSeparator", ",", "str")
        results = separator.join(["File"] + self.controlReport.controls)
        for file in self.controlReport.files:
            filename = returnName(filename = file, allFiles = self.controlReport.files)
            result = [filename]
            for control in self.controlReport.controls:
                result += [i[3] for i in self.controlReport.results[control] if i[0] == file]
            results += "\n" + separator.join(map(str, result))
     
        writeResults(output, results)
        self.status.set("Results were saved.")
        

    def checkProcessing(self):
        "method updating page after change of notebook tab"        
        if self.root.fileStorage.arenafiles:
            self.process.state(["!disabled"])
        else:
            self.process.state(["disabled"])

        if self.root.fileStorage.arenafiles or self.root.fileStorage.wrongfiles:
            self.fileStorageFrame.removeFiles.state(["!disabled"])
        else:
            self.fileStorageFrame.removeFiles.state(["disabled"])

        self.fileStorageFrame.chosenVar.set(len(self.root.fileStorage.arenafiles))
        self.fileStorageFrame.nonMatchingVar.set(len(self.root.fileStorage.wrongfiles))
Example #4
0
class Processor(ttk.Frame):
    "represents 'Process' page in the main window notebook"

    def __init__(self, root):
        super().__init__(root)

        self["padding"] = (10, 10, 12, 12)
        self.root = root
        self.fileStorage = self.root.fileStorage
        self.selectedBatchTime = optionGet("LastBatchTime", "[(0, 20)]",
                                           "list")

        # variables
        self.status = StringVar()
        self.useBatchTimeVar = BooleanVar()

        # frames
        self.parametersF = ParameterFrame(self)
        self.fileStorageFrame = FileStorageFrame(self)
        self.saveToFrame = SaveToFrame(self, parent="processor")
        self.optionFrame = OptionFrame(self, text="Options")
        self.timeLabFrame = ttk.Labelframe(self, text="Time")
        self.timeLabFrame.root = self
        self.timeFrame = TimeFrame(self.timeLabFrame)

        # buttons
        self.process = ttk.Button(self,
                                  text="Process Files",
                                  command=self.processFun,
                                  state="disabled")
        self.useBatchTime = ttk.Checkbutton(self.timeLabFrame,
                                            text="Use batch time",
                                            variable=self.useBatchTimeVar,
                                            command=self.toggledUseBatchTime)
        self.setBatchTimeBut = ttk.Button(self.timeLabFrame,
                                          text="Set",
                                          width=3,
                                          command=lambda: SetBatchTime(self))

        # labels
        self.statusBar = ttk.Label(self, textvariable=self.status)

        # adding to grid
        self.parametersF.grid(column=0,
                              row=3,
                              columnspan=4,
                              sticky=(N, W),
                              padx=4)
        self.fileStorageFrame.grid(column=3, row=0, pady=5, padx=4)
        self.timeLabFrame.grid(column=1, row=5, padx=30, sticky=(N, W))
        self.timeFrame.grid(column=0, row=0, columnspan=2, sticky=(E, W))

        self.process.grid(column=3, row=7, sticky=(S, E), padx=4, pady=3)
        self.useBatchTime.grid(column=0, row=1, pady=5)
        self.setBatchTimeBut.grid(column=1, row=1, pady=5)

        self.statusBar.grid(column=0,
                            row=7,
                            columnspan=3,
                            sticky=(N, S, E, W),
                            padx=6,
                            pady=3)

        self.saveToFrame.grid(column=1,
                              row=1,
                              columnspan=3,
                              sticky=(N, S, E, W),
                              padx=6,
                              pady=2)

        self.optionFrame.grid(column=0, row=5, sticky=(N, E), padx=6)

        # what should be enlarged
        self.columnconfigure(2, weight=1)
        self.rowconfigure(2, weight=3)
        self.rowconfigure(4, weight=2)
        self.rowconfigure(6, weight=2)

    def processCheck(self):
        "checks whether all inputs are valid - helper function for processFun"

        if self.saveToFrame.saveToVar.get() == "":
            raise Exception("You have to choose an output file!")

        if len(self.fileStorage) == 0:
            raise Exception("You haven't chosen any file!")

        try:
            startTime = float(self.timeFrame.startTimeVar.get())
        except Exception:
            raise Exception("Start time has to be a number!")
        try:
            time = float(self.timeFrame.timeVar.get())
        except Exception:
            raise Exception("Stop time has to be a number!")

        if startTime >= time:
            raise Exception("Start time must be smaller than stop time!")

        if time < 0 or startTime < 0:
            raise Exception("Time has to be set to a positive value")

        if not os.path.exists(
                os.path.split(self.saveToFrame.saveToVar.get())[0]):
            if os.path.split(self.saveToFrame.saveToVar.get())[0]:
                raise Exception("Pathname of the output file doesn't exist!")

        if len(self.filesToProcess) == 0:
            raise Exception("There is no file selected for processing!")

    def processFun(self):
        "processes chosen files and saves the results in the save-to file"
        # files to be processed
        self.filesToProcess = [file for file in self.fileStorage if \
                               self.optionFrame.processFile(file)]

        # checking for mistakes
        try:
            self.processCheck()
        except Exception as e:
            self.bell()
            self.status.set(e)
            return

        # progressWindow
        if len(self.filesToProcess) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self,
                                                 len(self.filesToProcess))

        # selected methods
        methods = []
        for method in Parameters().parameters:
            if eval("self.parametersF.%sVar.get()" %
                    (method[0].replace(" ", ""))):
                methods.append(method[0])

        # initializations
        output = self.saveToFrame.saveToVar.get()
        startTime = float(self.timeFrame.startTimeVar.get())
        time = float(self.timeFrame.timeVar.get())
        separator = optionGet("ResultSeparator", ",", "str")
        batchTime = self.selectedBatchTime if self.useBatchTimeVar.get(
        ) else None
        self.log = Log(methods,
                       startTime,
                       time,
                       self.filesToProcess,
                       self.fileStorage,
                       self.optionFrame.removeReflectionsWhere.get(),
                       output,
                       batchTime=batchTime)
        self.someProblem = False
        developer = optionGet("Developer", False, 'bool')

        # results header
        if self.useBatchTimeVar.get():
            info = [
                method[0] for method in Parameters().parameters
                if method[2] == "info"
            ]
            results = ["File"]
            for method in methods:
                if method in info:
                    results.append(method)
                else:
                    results.extend([
                        method + " ({}-{})".format(start, end)
                        for start, end in self.selectedBatchTime
                    ])
            results = separator.join(results)
        else:
            results = separator.join(["File"] + methods)
        if self.optionFrame.saveTags.get():
            results += separator + "Tags"
        if self.optionFrame.saveComments.get():
            results += separator + "Comment"

        # computing of results
        for file in self.filesToProcess:
            # loading of cm object
            if methods:
                try:
                    if file in self.fileStorage.pairedfiles:
                        cm = CM(file, nameR=self.fileStorage.pairedfiles[file])
                    else:
                        cm = CM(file, nameR="auto")

                    if self.optionFrame.removeReflections(file):
                        cm.removeReflections(
                            points=self.fileStorage.reflections.get(
                                file, None))
                except Exception as e:
                    if developer:
                        print(e)
                    filename = returnName(filename=file,
                                          allFiles=self.fileStorage.arenafiles)
                    results += "\n" + filename + "{}NA".format(
                        separator) * len(methods)
                    self.log.failedToLoad.append(file)
                    self.someProblem = True
                    continue

            result = []
            for method in Parameters().parameters:
                if method[0] in methods:
                    if self.useBatchTimeVar.get():
                        for startTime, time in self.selectedBatchTime:
                            try:
                                if method[2] == "custom":
                                    exec("from Stuff.Parameters import {}".
                                         format(method[5]))
                                result.append(eval(method[1]))
                                if method[2] == "info":
                                    break
                            except Exception as e:
                                if developer:
                                    print(e)
                                result.append("NA")
                                self.log.methodProblems[method[0]].append(file)
                                self.someProblem = True
                    else:
                        try:
                            if method[2] == "custom":
                                exec("from Stuff.Parameters import {}".format(
                                    method[5]))
                            result.append(eval(method[1]))
                        except Exception as e:
                            if developer:
                                print(e)
                            result.append("NA")
                            self.log.methodProblems[method[0]].append(file)
                            self.someProblem = True

            result = separator.join(map(str, result))
            if methods:
                result = separator + result
            filename = returnName(filename=file,
                                  allFiles=self.fileStorage.arenafiles)
            results += "\n" + filename + result

            # tag inclusion in results
            if self.optionFrame.saveTags.get():
                if file in self.fileStorage.tagged:
                    results += separator + "1"
                else:
                    results += separator + "0"

            # comment inclusion in results
            if self.optionFrame.saveComments.get():
                results += separator + self.fileStorage.comments[file]

            # progress window update
            if len(self.filesToProcess) > 1:
                if self.stoppedProcessing:
                    writeResults(output, results)
                    self.log.stopped = file
                    self.log.writeLog()
                    self.status.set("Processing stopped")
                    return
                else:
                    self.progressWindow.addOne()

        # results and log writing, ending of processing
        writeResults(output, results)
        self.log.writeLog()
        self._setStatusEndProgressWindow()

        if self.someProblem:
            ProcessingProblemDialog(self, self.log.filename)
        elif self.optionFrame.showResults.get():
            os.startfile(output)

    def _setStatusEndProgressWindow(self):
        if len(self.filesToProcess) > 1:
            if self.someProblem:
                self.status.set("Files were processed.")
            else:
                self.status.set("Files were processed successfully.")
            self.progressWindow.destroy()
        else:
            if self.someProblem:
                self.status.set("File was processed.")
            else:
                self.status.set("File was processed successfully.")

    def toggledUseBatchTime(self):
        "called when batch time checkbutton is toggled; disables or enables changing time"
        if self.useBatchTimeVar.get():
            self.timeFrame.totalTime.state(["disabled"])
            self.timeFrame.startTime.state(["disabled"])
        else:
            self.timeFrame.totalTime.state(["!disabled"])
            self.timeFrame.startTime.state(["!disabled"])

    def checkProcessing(self):
        "method updating page after change of notebook tab"
        if self.fileStorage.arenafiles and self.saveToFrame.saveToVar.get():
            self.process.state(["!disabled"])
        else:
            self.process.state(["disabled"])

        if self.fileStorage.arenafiles or self.fileStorage.wrongfiles:
            self.fileStorageFrame.removeFiles.state(["!disabled"])
        else:
            self.fileStorageFrame.removeFiles.state(["disabled"])

        self.fileStorageFrame.chosenVar.set(len(self.fileStorage))
        self.fileStorageFrame.nonMatchingVar.set(
            len(self.fileStorage.wrongfiles))
Example #5
0
class Processor(ttk.Frame):
    "represents 'Process' page in the main window notebook"
    def __init__(self, root):
        super().__init__(root)

        self["padding"] = (10, 10, 12, 12)
        self.root = root # potreba pouze pro progressbar


        # variables    
        self.status = StringVar()
              
        # frames
        self.parametersF = ParameterFrame(self)
        self.fileStorageFrame= FileStorageFrame(self)
        self.saveToFrame = SaveToFrame(self, parent = "processor")
        self.timeFrame = TimeFrame(self)
        self.optionFrame = OptionFrame(self, text = "Options")
        
        # buttons
        self.process = ttk.Button(self, text = "Process Files", command = self.processFun,
                                  state = "disabled")

        # labels
        self.statusBar = ttk.Label(self, textvariable = self.status)

        
        # adding to grid
        self.parametersF.grid(column = 0, row = 3, columnspan = 4, sticky = (N, W), padx = 4)
        self.fileStorageFrame.grid(column = 3, row = 0, pady = 5, padx = 4)
        self.timeFrame.grid(column = 1, row = 5, padx = 30, pady = 15, sticky = (N, W))
        
        self.process.grid(column = 3, row = 7, sticky = (S, E), padx = 4, pady = 3)

        self.statusBar.grid(column = 0, row = 7, columnspan = 3, sticky = (N, S, E, W), padx = 6,
                            pady = 3)
       
        self.saveToFrame.grid(column = 1, row = 1, columnspan = 3, sticky = (N, S, E, W),
                              padx = 6, pady = 2)

        self.optionFrame.grid(column = 0, row = 5, sticky = (N, E), padx = 6)
  

        # what should be enlarged
        self.columnconfigure(2, weight = 1)
        self.rowconfigure(2, weight = 3)
        self.rowconfigure(4, weight = 2)
        self.rowconfigure(6, weight = 2)


    def processCheck(self):
        "checks whether all inputs are valid - helper function for processFun"

        if self.saveToFrame.saveToVar.get() == "":
            raise Exception("You have to choose an output file!")
        
        if len(self.root.fileStorage.arenafiles) == 0:
            raise Exception("You haven't chosen any file!")

        try:
            startTime = float(self.timeFrame.startTimeVar.get())
        except Exception:
            raise Exception("Start time has to be a number!")
        try:
            time = float(self.timeFrame.timeVar.get())
        except Exception:
            raise Exception("Stop time has to be a number!")
        
        if startTime >= time:
            raise Exception("Start time must be smaller than stop time!")

        if time < 0 or startTime < 0:
            raise Exception("Time has to be set to a positive value")

        if not os.path.exists(os.path.split(self.saveToFrame.saveToVar.get())[0]):
            if os.path.split(self.saveToFrame.saveToVar.get())[0]:
                raise Exception("Pathname of the output file doesn't exist!")
      
        
    def processFun(self):
        "processes chosen files and saves the results in the save-to file"
        # checking for mistakes
        try:
            self.processCheck()
        except Exception as e:
            self.bell()
            self.status.set(e)
            return                   

        # files to be processed
        self.filesToProcess = [file for file in self.root.fileStorage.arenafiles if \
                               self.optionFrame.processFile(file)]

        # progressWindow and check for number of files for processing
        if len(self.filesToProcess) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.filesToProcess))
        elif len(self.filesToProcess) == 0:
            self.bell()
            self.status.set("There is no file selected for processing!")
            return

        # selected methods          
        methods = []
        for method in Parameters().parameters:
            if eval("self.parametersF.%sVar.get()" % (method[0].replace(" ", ""))):
                methods.append(method[0])
           
        output = self.saveToFrame.saveToVar.get()
        startTime = float(self.timeFrame.startTimeVar.get())
        time = float(self.timeFrame.timeVar.get())
        separator = optionGet("ResultSeparator", ",", "str")
        
        results = separator.join(["File"] + methods)
        
        if self.optionFrame.saveTags.get():
            results += separator + "Tags"

        self.log = Log(methods, startTime, time, self.filesToProcess, self.root.fileStorage,
                       self.optionFrame.removeReflectionsWhere.get(), output)
        self.someProblem = False

        developer = optionGet("Developer", False, 'bool')
        
        for file in self.filesToProcess:
            # loading of cm object
            if methods:
                try:
                    if file in self.root.fileStorage.pairedfiles:
                        cm = CM(file, nameR = self.root.fileStorage.pairedfiles[file])
                    else:
                        cm = CM(file, nameR = "auto")

                    if self.optionFrame.removeReflections(file):
                        cm.removeReflections(points = self.root.fileStorage.reflections.get(file,
                                                                                            None))
                except Exception as e:
                    if developer:
                        print(e)   
                    filename = returnName(filename = file, allFiles =
                                          self.root.fileStorage.arenafiles) 
                    results += "\n" + filename + "{}NA".format(separator) * len(methods)
                    self.log.failedToLoad.append(file)
                    self.someProblem = True
                    continue
                    
            
            result = []
            for method in Parameters().parameters:
                if method[0] in methods:
                    try:
                        if method[2] == "custom":
                            exec("from Stuff.Parameters import {}".format(method[5]))
                        result.append(eval(method[1]))
                    except Exception as e:
                        if developer:
                            print(e)   
                        result.append("NA")
                        self.log.methodProblems[method[0]].append(file)
                        self.someProblem = True


            result = separator.join(map(str, result))
            if methods:
                result = separator + result               
            filename = returnName(filename = file, allFiles = self.root.fileStorage.arenafiles)      
            results += "\n" + filename + result
            
            if self.optionFrame.saveTags.get(): # tag inclusion in results
                if file in self.root.fileStorage.tagged:
                    results += separator + "1"
                else:
                    results += separator + "0"
            
            if len(self.filesToProcess) > 1:
                if self.stoppedProcessing:
                    writeResults(output, results)
                    self.log.stopped = file
                    self.log.writeLog()
                    self.status.set("Processing stopped")
                    return
                else:
                    self.progressWindow.addOne()


        writeResults(output, results)
        self.log.writeLog()

        # change of status bar and closing of progressWindow
        if len(self.filesToProcess) > 1:
            if self.someProblem:
                self.status.set("Files were processed.")
            else:
                self.status.set("Files were processed successfully.")
            self.progressWindow.destroy()
        else:
            if self.someProblem:
                self.status.set("File was processed.")
            else:
                self.status.set("File was processed successfully.")

        # removal of files from fileStorage if selected
        if self.optionFrame.clearFilesAfterProcessing.get():
            for file in self.filesToProcess:
                self.root.fileStorage.arenafiles.remove(file)
            self.fileStorageFrame.chosenVar.set(len(self.root.fileStorage.arenafiles))
            if not self.root.fileStorage.arenafiles:
                self.fileStorageFrame.removeFiles.state(["disabled"]) 
                self.process.state(["disabled"])

        if self.someProblem:
            ProcessingProblemDialog(self, self.log.filename)


    def checkProcessing(self):
        "method updating page after change of notebook tab"
        if self.root.fileStorage.arenafiles and self.saveToFrame.saveToVar.get():
            self.process.state(["!disabled"])
        else:
            self.process.state(["disabled"])

        if self.root.fileStorage.arenafiles or self.root.fileStorage.wrongfiles:
            self.fileStorageFrame.removeFiles.state(["!disabled"])
        else:
            self.fileStorageFrame.removeFiles.state(["disabled"])        

        self.fileStorageFrame.chosenVar.set(len(self.root.fileStorage.arenafiles))
        self.fileStorageFrame.nonMatchingVar.set(len(self.root.fileStorage.wrongfiles))
Example #6
0
class Controller(ttk.Frame):
    "represents 'Control' page in the main window notebook"
    def __init__(self, root):
        super().__init__(root)

        self["padding"] = (7, 7, 9, 9)

        self.root = root
        self.fileStorage = self.root.fileStorage

        # file selection
        self.saveToVar = StringVar()
        self.saveToVar.set(True)
        self.fileStorageFrame = FileStorageFrame(self)
        self.fileStorageFrame.grid(column = 1, row = 0, columnspan = 2, sticky = (N, S, E, W),
                                   pady = 5, padx = 2)

        # statusbar
        self.status = StringVar()
        
        self.statusBar = ttk.Label(self, textvariable = self.status)
        self.statusBar.grid(column = 0, row = 4, columnspan = 2, sticky = (S, E, W))
        
        # control button
        self.process = ttk.Button(self, text = "Control", command = self.controlFun)
        self.process.grid(column = 2, row = 4, sticky = E)
        self.process.state(["disabled"])
        
        # report
        self.reportFrame = ttk.LabelFrame(self, text = "Report")
        self.reportFrame.grid(column = 0, row = 0, rowspan = 4, sticky = (N, S, E, W), padx = 5)
        self.reportFrame.columnconfigure(0, weight = 1)
        self.reportFrame.rowconfigure(0, weight = 1)

        self.upFrame = ttk.Frame(self.reportFrame)
        self.upFrame.grid(column = 0, row = 0, columnspan = 2, sticky = (N, S, E, W))
        self.upFrame.columnconfigure(0, weight = 1)
        self.upFrame.rowconfigure(0, weight = 1)
        
        self.contentTree = ttk.Treeview(self.upFrame, selectmode = "none")
        self.contentTree.grid(column = 0, row = 0, sticky = (N, S, E, W))
        self.contentTree["columns"] = ("description", "importance", "tag")
        self.contentTree.column("#0", width = 250, anchor = "w")
        self.contentTree.heading("#0", text = "Problem",
                                 command = lambda: self.orderReport("name"))
        self.contentTree.column("description", width = 200, anchor = "e")
        self.contentTree.heading("description", text = "Description",
                                 command = lambda: self.orderReport("description"))
        self.contentTree.column("importance", width = 60, anchor = "e")
        self.contentTree.heading("importance", text = "Importance",
                                 command = lambda: self.orderReport("importance"))
        self.contentTree.column("tag", width = 10, anchor = "center")
        self.contentTree.heading("tag", text = "Tag", command = lambda: self.orderReport("tag"))
        self.scrollbar = ttk.Scrollbar(self.upFrame, orient = VERTICAL,
                                       command = self.contentTree.yview)
        self.scrollbar.grid(column = 1, row = 0, sticky = (N, S, E))
        self.contentTree.configure(yscrollcommand = self.scrollbar.set)
        
        self.saveToFrame = SaveToFrame(self.reportFrame, label = False)
        self.saveToFrame.grid(column = 0, row = 1, sticky = (E, W))

        self.saveBut = ttk.Button(self.reportFrame, text = "Save", command = self.saveFun)
        self.saveBut.grid(column = 1, row = 1, sticky = E, padx = 2)
    
        self.controlReport = ControlReport(self)
                       
        self.contentTree.tag_bind("file", "<Double-1>", lambda e: self.treeDoubleClick(e))
        self.contentTree.tag_bind("file", "<3>", lambda e: self.filePopUp(e))
        self.contentTree.tag_bind("ok", "<3>", lambda e: self.okPopUp(e))
        self.contentTree.tag_bind("control", "<3>", lambda e: self.controlPopUp(e))
        self.contentTree.tag_configure("comment", background = commentColor())
        
        # method selection frame                
        self.controlFrame = ControlFrame(self)
        self.controlFrame.grid(column = 1, row = 1, columnspan = 2, sticky = (N, W), padx = 10,
                               pady = 55)

        # time frame
        self.timeFrame = TimeFrame(self)
        self.timeFrame.grid(column = 1, row = 2, columnspan = 2, sticky = (N, W), padx = 10)


        self.columnconfigure(0, weight = 1)
        self.rowconfigure(2, weight = 1)


    def orderReport(self, byWhat):
        "orders control report"
        opened = {}
        for child in self.contentTree.get_children():
            opened[child] = self.contentTree.item(child, "open")
            self.contentTree.delete(child)
        self.controlReport.orderedBy = byWhat
        self.controlReport.updateTree()
        for child in self.contentTree.get_children():
            if child in opened:
                self.contentTree.item(child, open=opened[child])


    def refresh(self):
        "refreshes the tree after adding a comment"
        self.controlReport.clear(clearAll = False)
        self.controlReport.updateTree()
        

    def filePopUp(self, event):
        "pop-up menu for file item in the tree"
        menu = Menu(self, tearoff = 0)
        item = self.contentTree.identify("item", event.x, event.y)
        name = item.rstrip("0123456789")
        if name in self.fileStorage.tagged:
            menu.add_command(label = "Remove tag", command = lambda: self.removeTag(name))
        else:
            menu.add_command(label = "Add tag", command = lambda: self.addTag(name))
        menu.add_command(label = "Add comment", command = lambda: Comment(self, name))
        menu.add_separator()
        menu.add_command(label = "Open arena file", command = lambda: self.openFile("arena", name))
        menu.add_command(label = "Open room file", command = lambda: self.openFile("room", name))
        menu.post(event.x_root, event.y_root)
        
    def controlPopUp(self, event):
        "pop-up menu for control item in the tree"
        menu = Menu(self, tearoff = 0)
        item = self.contentTree.identify("item", event.x, event.y)
        menu.add_command(label = "Add tags to all problem files",
                         command = lambda: self.addMoreTags(item, specified = ["Problem"]))
        menu.add_command(label = "Add tags to all files with at least a warning",
                         command = lambda: self.addMoreTags(item, specified =
                                                            ["Problem", "Warning"]))
        menu.add_command(label = "Add tags to all files with at least a concern",
                         command = lambda: self.addMoreTags(item, specified =
                                                            ["Problem", "Warning", "Concern"]))
        menu.add_separator()
        menu.add_command(label = "Remove tags from all problem files",
                         command = lambda: self.removeMoreTags(item, specified = ["Problem"]))
        menu.add_command(label = "Remove tags from all files with at least a warning",
                         command = lambda: self.removeMoreTags(item, specified =
                                                               ["Problem", "Warning"]))
        menu.add_command(label = "Remove tags from all files with at least a concern",
                         command = lambda: self.removeMoreTags(item, specified =
                                                               ["Problem", "Warning", "Concern"]))
        menu.post(event.x_root, event.y_root)
        
    def okPopUp(self, event):
        "pop-up menu for rest of files item in the tree"
        menu = Menu(self, tearoff = 0)
        item = self.contentTree.identify("item", event.x, event.y)
        menu.add_command(label = "Add tags to all OK files",
                         command = lambda: self.addMoreTags(item))
        menu.add_command(label = "Remove tags from all OK files",
                         command = lambda: self.removeMoreTags(item))
        menu.post(event.x_root, event.y_root)
        
    def openFile(self, frame, arenafile):
        "opens selected file; called from filePopUp"
        if frame == "room":
            if arenafile in self.fileStorage.pairedfiles:
                roomfile = self.fileStorage.pairedfiles[arenafile]
            else:
                if "Arena" in os.path.basename(arenafile):
                    splitName = os.path.split(arenafile)
                    roomfile = os.path.join(splitName[0], splitName[1].replace("Arena", "Room"))                    
                elif "arena" in os.path.basename(arenafile):
                    splitName = os.path.split(arenafile)
                    roomfile = os.path.join(splitName[0], splitName[1].replace("arena", "room"))
            if roomfile:
                os.startfile(roomfile)
            else:
                self.bell()
        elif frame == "arena":
            os.startfile(arenafile)

    def addTag(self, name):
        "adds tag to a single file"
        for num in range(len(self.controlReport.controls)):
            self.contentTree.set(name + str(num), "tag", "x")
        self.fileStorage.tag(name)
        
    def removeTag(self, name):
        "removes tag from a single file"
        for num in range(len(self.controlReport.controls)):
            self.contentTree.set(name + str(num), "tag", " ")
        self.fileStorage.tagged.remove(name)
        
    def addMoreTags(self, item, specified = False):
        "adds tags to more files; specified used for importance, e.g. ['Problem', 'Warning']"
        num = self.contentTree.index(item)
        for child in self.contentTree.get_children(item):
            name = child.rstrip("0123456789")
            if specified and not self.contentTree.set(name + str(num), "importance") in specified:
                next
            else:
                self.addTag(name)

    def removeMoreTags(self, item, specified = False):
        "removes tags to more files; specified used for importance, e.g. ['Problem', 'Warning']"
        num = self.contentTree.index(item)
        for child in self.contentTree.get_children(item):
            name = child.rstrip("0123456789")
            if specified and not self.contentTree.set(name + str(num), "importance") in specified:
                next
            else:
                if name in self.fileStorage.tagged:
                    self.removeTag(name)    

       
    def treeDoubleClick(self, event):
        "shows tracks in a ShowTracks toplevel window"
        item = self.contentTree.identify("item", event.x, event.y)
        name = item.rstrip("0123456789")
        tracks = self.controlReport.files # <- zmenit aby zobrazovalo serazene
        if item:
            showTracks = ShowTracks(self, nameA = name, tracks = tracks, controlled = True)


    def controlFun(self):
        "processes selected files, clears report, shows results in a report"

        # progressbar
        if len(self.fileStorage) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.fileStorage),
                                                 text = "controlled")

        # initialization
        controls = self.controlFrame.controlsGet() # selected controls
        self.controlReport.clear()  # clears report
        self.controlReport.addControls(controls) # adds selected controls to ControlReport

        self.problemOccured = False
        # processing      
        for file in self.fileStorage:
            tag = "x" if file in self.fileStorage.tagged else " "
            try:
                if file in self.fileStorage.pairedfiles:
                    cm = CM(file, nameR = self.fileStorage.pairedfiles[file])
                else:
                    cm = CM(file, nameR = "auto")
            except Exception:
                self.problemOccured = True
                for control in controls:
                    self.controlReport.addFile((control[0], [file, "Failed to load!", "Problem",
                                                             9999999, tag]))
            else:
                for control in controls:
                    try:
                        assessment = self.assessImportance(cm = cm, control = control, file = file)
                        assessment[1].append(tag)
                    except Exception:
                        self.problemOccured = True
                        assessment = (control[0], [file, "Failed to compute!", "Problem",
                                                   9999998, tag])
                    self.controlReport.addFile(assessment)
                     
            if len(self.fileStorage) > 1:
                if self.stoppedProcessing:
                    return
                else:
                    self.progressWindow.addOne()
                    
        self.controlReport.updateTree()

        # progressbar and status
        if len(self.fileStorage) > 1:
            self.progressWindow.destroy()
            if self.problemOccured:
                self.status.set("Files were not processed successfully!")
                self.bell()
            else:
                self.status.set("Files were processed successfully.")
        else:
            if self.problemOccured:
                self.status.set("File was not processed successfully!")
                self.bell()
            else:
                self.status.set("File was processed successfully.")          
        

    def assessImportance(self, cm, control, file):
        "method needed for evaluation of importance of results from CM class' control methods"
        method = control[0]
        startTime, time = int(self.timeFrame.startTimeVar.get()), int(self.timeFrame.timeVar.get())
        results = eval("cm.{}".format(control[1]))
        description = ""
        importance = ""
        value = 0
        
        if method == "Reflections":
            if results[1] * 3 + results[0] > 5:
                importance = "Problem"
            elif results[1] * 3 + results[0] > 2:
                importance = "Warning"
            elif results[0] > 0:
                importance = "Concern"
            else:
                importance = "OK"
            if results[1] != 1:
                description = "{} points problematic, {} of concern".format(results[1], results[0])
            else:
                description = "{} point problematic, {} of concern".format(results[1], results[0])
            value = results[1]

            self.fileStorage.saveReflections(file = file, points = results[2] + results[3])
            
        elif method == "Outside Points":
            if results > 250:
                importance = "Problem"
            elif results > 50:
                importance = "Warning"
            elif results > 5:
                importance = "Concern"
            else:
                importance = "OK"
            if results != 1:
                description = "{} points outside of arena".format(results)
            else:
                description = "1 point outside of arena"
            value = results

        elif method == "Bad Points":
            if results > 10:
                importance = "Problem"
            elif results > 5:
                importance = "Warning"
            elif results > 2:
                importance = "Concern"
            else:
                importance = "OK"
            description = "{0:.2f}% bad points".format(results)
            value = results            
                    
        return (method, [cm.nameA, description, importance, value])

            
    def saveFun(self):
        "writes results from controlReport to selected file"
        output = self.saveToFrame.saveToVar.get()
        if not self.controlReport.files:
            self.bell()
            self.status.set("No results prepared for saving.")
            return
        if not output:
            self.bell()
            self.status.set("You have to select a name of a file.")
            return
        
        separator = optionGet("ResultSeparator", ",", "str")
        results = separator.join(["File"] + self.controlReport.controls)
        for file in self.controlReport.files:
            filename = returnName(filename = file, allFiles = self.controlReport.files)
            result = [filename]
            for control in self.controlReport.controls:
                result += [i[3] for i in self.controlReport.results[control] if i[0] == file]
            results += "\n" + separator.join(map(str, result))
     
        writeResults(output, results)
        self.status.set("Results were saved.")
        

    def checkProcessing(self):
        "method updating page after change of notebook tab"
        self.controlReport.clear(clearAll = False)
        self.controlReport.updateTree()
        
        if self.fileStorage.arenafiles:
            self.process.state(["!disabled"])
        else:
            self.process.state(["disabled"])

        if self.fileStorage.arenafiles or self.fileStorage.wrongfiles:
            self.fileStorageFrame.removeFiles.state(["!disabled"])
        else:
            self.fileStorageFrame.removeFiles.state(["disabled"])

        self.fileStorageFrame.chosenVar.set(len(self.fileStorage))
        self.fileStorageFrame.nonMatchingVar.set(len(self.fileStorage.wrongfiles))