Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import utils
- from pathlib import Path
- from asciimatics.screen import Screen
- from asciimatics.scene import Scene
- from asciimatics.widgets import Frame, TextBox, Layout, PopUpDialog, CheckBox, Button
- from asciimatics.exceptions import ResizeScreenError, StopApplication
- RESULTS_FILE = (
- "/Users/jwr003/coding/pytest-fold/output_files_to_analyze/outputvfold.ansi"
- )
- class ResultsData:
- """
- Class to read in results from a 'pytest --fold' session (which inserts markers
- around each failed test), and tokenize the results into individual sections for
- display on the TUI
- """
- def __init__(self, path: Path = RESULTS_FILE) -> None:
- self.results_file = path
- self.sections = []
- def _tokenize_results(self) -> None:
- with open(self.results_file, "r") as results_file:
- results_lines = results_file.readlines()
- self.sections = utils.tokenize(results_lines)
- def get_results(self) -> list:
- self._tokenize_results()
- return self.sections
- class FoldedResultLayout(Layout):
- """
- The Layout for the FoldedResult has two columns:
- 1) a checkbox (height:1) to fold/unfold the result textbox
- 2) a folded textbox (height:1) to act as placeholder for when unfolded
- """
- def __init__(self) -> None:
- self.columns = [1, 99] # 1% chkbox, 99% textbox
- self.add_widget(CheckBox(text="Unfold", on_change=self.toggle_checkbox))
- self.add_widget(TextBox(height=1, readonly=True))
- def toggle_checkbox(self) -> None:
- print("In on_change")
- class UnfoldedResultLayout(Layout):
- """
- The Layout for the UnfoldedResult has two columns:
- 1) a checkbox (height:1) to fold/unfold the result textbox
- 2) a folded textbox (height:N) to display unfolded data
- """
- def __init__(self, textboxheight: int = 2, value: str = "No data!") -> None:
- self.columns = [1, 99] # 1% chkbox, 99% textbox
- self.add_widget(CheckBox(text="Fold", on_change=self.toggle_checkbox))
- self.add_widget(
- TextBox(height=textboxheight, linewrap=True, readonly=True, value=value)
- )
- def toggle_checkbox(self) -> None:
- print("In on_change")
- class QuitterLayout(Layout):
- def __init__(self, textboxheight: int = 2, value: str = "No data!") -> None:
- # self.screen = screen
- self.columns = [1, 99] # 1% chkbox, 99% textbox
- self.add_widget(TextBox(1))
- self.add_widget(Button("Quit", self._quit, label="To exit:"), 1)
- def _quit(self) -> None:
- popup = PopUpDialog(
- self._screen,
- "Quit?",
- ["Yes", "No"],
- has_shadow=True,
- on_close=self._quit_on_yes,
- )
- self._scene.add_effect(popup)
- class ResultsFrame(Frame):
- def __init__(self, screen: Screen) -> None:
- # self.screen = screen
- self.height = screen.height
- self.width = screen.width
- # Snarf data from results file, tokenize, then add Layoutr for the resulting
- # sections to the ResultsFrame
- results_data = ResultsData()
- sections = results_data.get_results()
- self.add_layout(
- UnfoldedResultLayout(value=sections[0])
- ) # First section, "header" info from Pytest
- for _ in range(1, len(sections) - 1):
- self.add_layout(FoldedResultLayout())
- self.add_layout(
- UnfoldedResultLayout(value=sections[-1])
- ) # Last section, "summary" info from Pytest
- self.add_layout(QuitterLayout())
- self.fix()
- @staticmethod
- def _quit_on_yes(selected) -> None:
- if selected == 0:
- raise StopApplication("User requested exit")
- def demo(screen: Screen) -> None:
- scenes = [Scene([ResultsFrame(screen)], duration=-1)]
- screen.play(scenes, stop_on_resize=True, start_scene=scenes[0], allow_int=True)
- def main():
- last_scene = None
- while True:
- try:
- Screen.wrapper(demo)
- quit()
- except ResizeScreenError as e:
- last_scene = e.scene
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement