Advertisement
javajeff13

Untitled

Jan 23rd, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.15 KB | None | 0 0
  1. import utils
  2. from pathlib import Path
  3. from asciimatics.screen import Screen
  4. from asciimatics.scene import Scene
  5. from asciimatics.widgets import Frame, TextBox, Layout, PopUpDialog, CheckBox, Button
  6. from asciimatics.exceptions import ResizeScreenError, StopApplication
  7.  
  8. RESULTS_FILE = (
  9.     "/Users/jwr003/coding/pytest-fold/output_files_to_analyze/outputvfold.ansi"
  10. )
  11.  
  12.  
  13. class ResultsData:
  14.     """
  15.    Class to read in results from a 'pytest --fold' session (which inserts markers
  16.    around each failed test), and tokenize the results into individual sections for
  17.    display on the TUI
  18.    """
  19.  
  20.     def __init__(self, path: Path = RESULTS_FILE) -> None:
  21.         self.results_file = path
  22.         self.sections = []
  23.  
  24.     def _tokenize_results(self) -> None:
  25.         with open(self.results_file, "r") as results_file:
  26.             results_lines = results_file.readlines()
  27.         self.sections = utils.tokenize(results_lines)
  28.  
  29.     def get_results(self) -> list:
  30.         self._tokenize_results()
  31.         return self.sections
  32.  
  33.  
  34. class FoldedResultLayout(Layout):
  35.     """
  36.    The Layout for the FoldedResult has two columns:
  37.    1) a checkbox (height:1) to fold/unfold the result textbox
  38.    2) a folded textbox (height:1) to act as placeholder for when unfolded
  39.    """
  40.  
  41.     def __init__(self) -> None:
  42.         self.columns = [1, 99]  # 1% chkbox, 99% textbox
  43.         self.add_widget(CheckBox(text="Unfold", on_change=self.toggle_checkbox))
  44.         self.add_widget(TextBox(height=1, readonly=True))
  45.  
  46.     def toggle_checkbox(self) -> None:
  47.         print("In on_change")
  48.  
  49.  
  50. class UnfoldedResultLayout(Layout):
  51.     """
  52.    The Layout for the UnfoldedResult has two columns:
  53.    1) a checkbox (height:1) to fold/unfold the result textbox
  54.    2) a folded textbox (height:N) to display unfolded data
  55.    """
  56.  
  57.     def __init__(self, textboxheight: int = 2, value: str = "No data!") -> None:
  58.         self.columns = [1, 99]  # 1% chkbox, 99% textbox
  59.         self.add_widget(CheckBox(text="Fold", on_change=self.toggle_checkbox))
  60.         self.add_widget(
  61.             TextBox(height=textboxheight, linewrap=True, readonly=True, value=value)
  62.         )
  63.  
  64.     def toggle_checkbox(self) -> None:
  65.         print("In on_change")
  66.  
  67.  
  68. class QuitterLayout(Layout):
  69.     def __init__(self, textboxheight: int = 2, value: str = "No data!") -> None:
  70.         # self.screen = screen
  71.         self.columns = [1, 99]  # 1% chkbox, 99% textbox
  72.         self.add_widget(TextBox(1))
  73.         self.add_widget(Button("Quit", self._quit, label="To exit:"), 1)
  74.  
  75.     def _quit(self) -> None:
  76.         popup = PopUpDialog(
  77.             self._screen,
  78.             "Quit?",
  79.             ["Yes", "No"],
  80.             has_shadow=True,
  81.             on_close=self._quit_on_yes,
  82.         )
  83.         self._scene.add_effect(popup)
  84.  
  85.  
  86. class ResultsFrame(Frame):
  87.     def __init__(self, screen: Screen) -> None:
  88.         # self.screen = screen
  89.         self.height = screen.height
  90.         self.width = screen.width
  91.  
  92.         # Snarf data from results file, tokenize, then add Layoutr for the resulting
  93.         # sections to the ResultsFrame
  94.         results_data = ResultsData()
  95.         sections = results_data.get_results()
  96.         self.add_layout(
  97.             UnfoldedResultLayout(value=sections[0])
  98.         )  # First section, "header" info from Pytest
  99.         for _ in range(1, len(sections) - 1):
  100.             self.add_layout(FoldedResultLayout())
  101.         self.add_layout(
  102.             UnfoldedResultLayout(value=sections[-1])
  103.         )  # Last section, "summary" info from Pytest
  104.         self.add_layout(QuitterLayout())
  105.  
  106.         self.fix()
  107.  
  108.     @staticmethod
  109.     def _quit_on_yes(selected) -> None:
  110.         if selected == 0:
  111.             raise StopApplication("User requested exit")
  112.  
  113.  
  114. def demo(screen: Screen) -> None:
  115.     scenes = [Scene([ResultsFrame(screen)], duration=-1)]
  116.     screen.play(scenes, stop_on_resize=True, start_scene=scenes[0], allow_int=True)
  117.  
  118.  
  119. def main():
  120.     last_scene = None
  121.     while True:
  122.         try:
  123.             Screen.wrapper(demo)
  124.             quit()
  125.         except ResizeScreenError as e:
  126.             last_scene = e.scene
  127.  
  128.  
  129. if __name__ == "__main__":
  130.     main()
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement