-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcore_viewer.py
133 lines (113 loc) · 4.97 KB
/
core_viewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import stat
from output_viewer.build import build_page, build_viewer
from output_viewer.index import (
OutputFile,
OutputGroup,
OutputIndex,
OutputPage,
OutputRow,
)
from output_viewer.utils import rechmod
class OutputViewer(object):
def __init__(self, path=".", index_name="Results"):
self.path = os.path.abspath(path)
self.index = OutputIndex(index_name)
self.cache = {} # dict of { OutputPage: { OutputGroup: [OutputRow] } }
self.page = None
self.group = None
self.row = None
def add_page(self, page_title, *args, **kwargs):
"""Add a page to the viewer's index"""
self.page = OutputPage(page_title, *args, **kwargs)
self.cache[self.page] = {}
self.index.addPage(self.page)
def set_page(self, page_title):
"""Sets the page with the title name as the current page"""
for output_page in self.cache:
if page_title == output_page.title:
self.page = output_page
return
raise RuntimeError("There is no page titled: %s" % page_title)
def add_group(self, group_name):
"""Add a group to the current page"""
if self.page is None:
raise RuntimeError("You must first insert a page with add_page()")
self.group = OutputGroup(group_name)
if self.group not in self.cache[self.page]:
self.cache[self.page][self.group] = [] # group doesn't have any rows yet
self.page.addGroup(self.group)
def set_group(self, group_name):
"""Sets the group with the title name as the current group"""
for output_group in self.cache[self.page]:
if group_name == output_group.title:
self.group = output_group
return
raise RuntimeError("There is no group titled: %s" % group_name)
def add_row(self, row_name):
"""Add a row with the title name to the current group"""
if self.group is None:
raise RuntimeError("You must first insert a group with add_group()")
self.row = OutputRow(row_name, [])
if self.row not in self.cache[self.page][self.group]:
self.cache[self.page][self.group].append(self.row)
self.page.addRow(self.row, len(self.page.groups) - 1) # type: ignore
def set_row(self, row_name):
"""Sets the row with the title name as the current row"""
for output_row in self.cache[self.page][self.group]:
if row_name == output_row.title:
self.row = output_row
return
raise RuntimeError("There is no row titled: %s" % row_name)
def add_cols(self, cols):
"""Add multiple string cols to the current row"""
self.row.columns.append(cols) # type: ignore
def add_col(self, col, is_file=False, **kwargs):
"""Add a single col to the current row. Set is_file to True if the col is a file path."""
if is_file:
self.row.columns.append(OutputFile(col, **kwargs)) # type: ignore
else:
self.row.columns.append(col) # type: ignore
def generate_page(self):
"""
Generate and return the location of the current HTML page.
"""
self.index.toJSON(os.path.join(self.path, "index.json"))
default_mask = stat.S_IMODE(os.stat(self.path).st_mode)
rechmod(self.path, default_mask)
if os.access(self.path, os.W_OK):
default_mask = stat.S_IMODE(
os.stat(self.path).st_mode
) # mode of files to be included
url = build_page(
self.page,
os.path.join(self.path, "index.json"),
default_mask=default_mask,
)
return url
raise RuntimeError("Error generating the page.")
def generate_viewer(self, prompt_user=True):
"""Generate the webpage and ask the user if they want to see it."""
self.index.toJSON(os.path.join(self.path, "index.json"))
default_mask = stat.S_IMODE(os.stat(self.path).st_mode)
rechmod(self.path, default_mask)
if os.access(self.path, os.W_OK):
default_mask = stat.S_IMODE(
os.stat(self.path).st_mode
) # mode of files to be included
build_viewer(
os.path.join(self.path, "index.json"),
diag_name="My Diagnostics",
default_mask=default_mask,
)
if os.path.exists(os.path.join(self.path, "index.html")):
if prompt_user:
user_prompt = "Would you like to open in a browser? y/[n]: "
should_open = input(user_prompt)
if should_open and should_open.lower()[0] == "y":
import webbrowser
index_path = os.path.join(self.path, "index.html")
url = "file://{path}".format(path=index_path)
webbrowser.open(url)
else:
raise RuntimeError("Failed to generate the viewer.")