def __init__(self, view): self.view = view self.settings = Settings()
class MainFrameController(object): def __init__(self, view): self.view = view self.settings = Settings() def on_init(self): pass def get_recently_opened(self): return self.settings.get_recently_opened() # # File menu actions # def on_file_new(self, event): try: self._file_new(self._get_file_path("New")) except NoFilePathSelected: pass def on_open(self, event): try: self._open(self._get_file_path("Open")) except NoFilePathSelected: pass def on_open_recent(self, event): self.open_path_if_exists(self.view.GetPath(event.GetId())) def on_save(self, event): try: self._save(self.path) except NoTestFound: pass def on_save_as(self, event): try: self._save(self._get_file_path("Save As")) except NoTestFound: pass except NoFilePathSelected: pass def on_app_exit(self, event): pass # # Test menu actions # def on_test_new(self, event): return self._test_new() def on_remove_test(self, event): test = self._get_test() self.view.RemoveTest(test) self._save(self.path) def on_test_run(self, event): try: test = self._get_test() self._execute_test(test.get_argv()) except NoTestFound: pass def on_test_run_selection(self, event): try: test = self._get_test() selection = self._get_selection() self._save_to_tempfile(selection) self._execute_test(test.get_argv_for_selection_run(TEMPFILE)) except NoTestFound: pass except NoSelectionFound: pass def on_test_edit(self, event): try: test = self._get_test() d = TestEditorDialog(self.view, "Edit Test", test) if d.ShowModal() == wx.ID_OK: self.view.UpdateTest(test) except NoTestFound: pass # # Manuscript menu actions # def on_effective_manuscript(self, event): try: test = self._get_test() self._execute_test(test.get_argv_for_inspection()) self.on_open_log(None) except NoTestFound: pass def on_edit(self, event): path = self.view.GetTest().get_manuscript() editor = os.getenv('EDITOR') if editor: os.system("%s %s" % (editor, path)) else: os.system(path) # # Log menu actions # def on_open_log(self, event): try: test = self._get_test() self._display_logfile(test.get_log_path()) except NoTestFound: pass def on_open_temp_log(self, event): self._display_logfile(os.getcwd()) # # Other actions # def open_path_if_exists(self, path): if os.path.exists(path): self._open(path) def on_test_selection_changed(self, event): lbx = event.GetEventObject() test = lbx.GetClientData(lbx.GetSelection()) self.view.DisplayTest(test) def _save_to_tempfile(self, text): f = open(TEMPFILE, "w") f.write(text.encode("utf-8")) f.close() def _get_test(self): test = self.view.GetTest() if test is None: wx.MessageBox("You have to select a test!") raise NoTestFound() return test def _get_selection(self): selection = self.view.GetSelection() if selection is None or selection.strip() == "": wx.MessageBox("You have to make a selection!") raise NoSelectionFound() return self._remove_empty_lines(selection) def _remove_empty_lines(self, text): lines = [line for line in text.split("\n") if line.strip() != ""] return "\n".join(lines) def _execute_test(self, args): self.view.ResetProgress() process = Popen(args, stdout=PIPE) process.communicate() self.view.SetSuccess(process.returncode == 0) def _save(self, path): tests = self.view.GetAllTests() root = ET.Element("autopilottests") for test in tests: atest = ET.SubElement(root, "autopilottest") ET.SubElement(atest, "name").text = test.name ET.SubElement(atest, "app").text = test.app_under_test ET.SubElement(atest, "manuscript").text = os.path.join( test.manuscript_paths, test.start_manuscript) ET.SubElement(atest, "exit").text = "%s" % test.exit_when_done ET.SubElement(atest, "delay").text = test.delay ET.SubElement(atest, "debug").text = "%s" % test.debug ET.SubElement(atest, "log").text = "%s" % test.log_dialogs ET.SubElement(atest, "inspect").text = "%s" % test.inspect ET.SubElement(atest, "placeholders").text = "%s" % test.placeholders tree = ET.ElementTree(root) tree.write(path) def _file_new(self, path): self.path = path self.view.ClearAllTests() if self.on_test_new(None): self.on_save(None) self._open(path) def _open(self, path): self.view.ClearAllTests() self.path = path e = xml.etree.ElementTree.parse(path).getroot() for xmltest in e.findall('autopilottest'): test = AutopilotTest() test.set_name(xmltest.find("name").text) test.set_app_under_test(xmltest.find("app").text) test.set_start_manuscript(xmltest.find("manuscript").text) test.set_delay(xmltest.find("delay").text) test.set_exit_when_done(xmltest.find("exit").text == "True") test.set_debug(xmltest.find("debug").text == "True") test.set_log_dialogs(xmltest.find("log").text == "True") test.set_inspect(xmltest.find("inspect").text == "True") test.set_placeholders(xmltest.find("placeholders").text) self.view.NewTest(test) self.view.SelectFirstTest() self.view.DisplaySelectedTest() if self.settings.save_recently_opened(path): self.view.SetRecentlyOpened(path) def _display_logfile(self, path): with open(os.path.join(path, LOGFILE)) as f: log = f.read().decode("utf-8") self.view.DisplayLog(log) def _get_file_path(self, heading): d = wx.FileDialog(self.view, heading) if d.ShowModal() == wx.ID_OK: return d.GetPath() raise NoFilePathSelected() def _test_new(self): test = AutopilotTest() d = TestEditorDialog(self.view, "New Test", test) if d.ShowModal() == wx.ID_OK: self.view.NewTest(test) self._save(self.path) return True return False
class MainFrameController(object): def __init__(self, view): self.view = view self.settings = Settings() def on_init(self): pass def get_recently_opened(self): return self.settings.get_recently_opened() # # File menu actions # def on_file_new(self, event): try: self._file_new(self._get_file_path("New")) except NoFilePathSelected: pass def on_open(self, event): try: self._open(self._get_file_path("Open")) except NoFilePathSelected: pass def on_open_recent(self, event): self.open_path_if_exists(self.view.GetPath(event.GetId())) def on_save(self, event): try: self._save(self.path) except NoTestFound: pass def on_save_as(self, event): try: self._save(self._get_file_path("Save As")) except NoTestFound: pass except NoFilePathSelected: pass def on_app_exit(self, event): pass # # Test menu actions # def on_test_new(self, event): return self._test_new() def on_remove_test(self, event): test = self._get_test() self.view.RemoveTest(test) self._save(self.path) def on_test_run(self, event): try: test = self._get_test() self._execute_test(test.get_argv()) except NoTestFound: pass def on_test_run_selection(self, event): try: test = self._get_test() selection = self._get_selection() self._save_to_tempfile(selection) self._execute_test(test.get_argv_for_selection_run(TEMPFILE)) except NoTestFound: pass except NoSelectionFound: pass def on_test_edit(self, event): try: test = self._get_test() d = TestEditorDialog(self.view, "Edit Test", test) if d.ShowModal() == wx.ID_OK: self.view.UpdateTest(test) except NoTestFound: pass # # Manuscript menu actions # def on_effective_manuscript(self, event): try: test = self._get_test() self._execute_test(test.get_argv_for_inspection()) self.on_open_log(None) except NoTestFound: pass def on_edit(self, event): path = self.view.GetTest().get_manuscript() editor = os.getenv('EDITOR') if editor: os.system("%s %s" % (editor, path)) else: os.system(path) # # Log menu actions # def on_open_log(self, event): try: test = self._get_test() self._display_logfile(test.get_log_path()) except NoTestFound: pass def on_open_temp_log(self, event): self._display_logfile(os.getcwd()) # # Other actions # def open_path_if_exists(self, path): if os.path.exists(path): self._open(path) def on_test_selection_changed(self, event): lbx = event.GetEventObject() test = lbx.GetClientData(lbx.GetSelection()) self.view.DisplayTest(test) def _save_to_tempfile(self, text): f = open(TEMPFILE, "w") f.write(text.encode("utf-8")) f.close() def _get_test(self): test = self.view.GetTest() if test is None: wx.MessageBox("You have to select a test!") raise NoTestFound() return test def _get_selection(self): selection = self.view.GetSelection() if selection is None or selection.strip() == "": wx.MessageBox("You have to make a selection!") raise NoSelectionFound() return self._remove_empty_lines(selection) def _remove_empty_lines(self, text): lines = [line for line in text.split("\n") if line.strip() != ""] return "\n".join(lines) def _execute_test(self, args): self.view.ResetProgress() process = Popen(args, stdout=PIPE) process.communicate() self.view.SetSuccess(process.returncode == 0) def _save(self, path): tests = self.view.GetAllTests() root = ET.Element("autopilottests") for test in tests: atest = ET.SubElement(root, "autopilottest") ET.SubElement(atest, "name").text = test.name ET.SubElement(atest, "app").text = test.app_under_test ET.SubElement(atest, "manuscript").text = os.path.join(test.manuscript_paths, test.start_manuscript) ET.SubElement(atest, "exit").text = "%s" % test.exit_when_done ET.SubElement(atest, "delay").text = test.delay ET.SubElement(atest, "debug").text = "%s" % test.debug ET.SubElement(atest, "log").text = "%s" % test.log_dialogs ET.SubElement(atest, "inspect").text = "%s" % test.inspect ET.SubElement(atest, "placeholders").text = "%s" % test.placeholders tree = ET.ElementTree(root) tree.write(path) def _file_new(self, path): self.path = path self.view.ClearAllTests() if self.on_test_new(None): self.on_save(None) self._open(path) def _open(self, path): self.view.ClearAllTests() self.path = path e = xml.etree.ElementTree.parse(path).getroot() for xmltest in e.findall('autopilottest'): test = AutopilotTest() test.set_name(xmltest.find("name").text) test.set_app_under_test(xmltest.find("app").text) test.set_start_manuscript(xmltest.find("manuscript").text) test.set_delay(xmltest.find("delay").text) test.set_exit_when_done(xmltest.find("exit").text == "True") test.set_debug(xmltest.find("debug").text == "True") test.set_log_dialogs(xmltest.find("log").text == "True") test.set_inspect(xmltest.find("inspect").text == "True") test.set_placeholders(xmltest.find("placeholders").text) self.view.NewTest(test) self.view.SelectFirstTest() self.view.DisplaySelectedTest() if self.settings.save_recently_opened(path): self.view.SetRecentlyOpened(path) def _display_logfile(self, path): with open(os.path.join(path, LOGFILE)) as f: log = f.read().decode("utf-8") self.view.DisplayLog(log) def _get_file_path(self, heading): d = wx.FileDialog(self.view, heading) if d.ShowModal() == wx.ID_OK: return d.GetPath() raise NoFilePathSelected() def _test_new(self): test = AutopilotTest() d = TestEditorDialog(self.view, "New Test", test) if d.ShowModal() == wx.ID_OK: self.view.NewTest(test) self._save(self.path) return True return False