-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCellsTestService.py
More file actions
179 lines (135 loc) · 5.91 KB
/
CellsTestService.py
File metadata and controls
179 lines (135 loc) · 5.91 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Copyright 2017-2021 object_database Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import traceback
import textwrap
import urllib
import sys
import logging
from inspect import getsourcelines
from object_database.service_manager.ServiceBase import ServiceBase
from typed_python.Codebase import Codebase
import object_database.web.cells as cells
import object_database as object_database
from object_database.web.CellsTestPage import CellsTestPage
from object_database import Schema
schema = Schema("core.web.CellsTestService")
@schema.define
class Counter:
value = int
_pagesCache = {}
def getPages():
if _pagesCache:
return _pagesCache
# force us to actually import everything in object database
odbCodebase = Codebase.FromRootlevelModule(object_database)
# these are all the cell_demo cells
for name, value in odbCodebase.allModuleLevelValues():
if (
isinstance(value, type)
and issubclass(value, CellsTestPage)
and value is not CellsTestPage
):
try:
instance = value()
_pagesCache.setdefault(instance.category(), {})[value.__name__] = instance
except Exception:
traceback.print_exc()
return _pagesCache
class CellsTestService(ServiceBase):
gbRamUsed = 0
coresUsed = 0
@staticmethod
def serviceHeaderToggles(serviceObject, instance=None, queryArgs=None):
"""Return a collection of widgets we want to stick in the top of the service display.
If None, then we get a raw service display with nothing else."""
if queryArgs is not None and queryArgs.get("noHarness"):
return None
return []
@staticmethod
def serviceDisplay(serviceObject, instance=None, objType=None, queryArgs=None):
queryArgs = queryArgs or {}
if "category" in queryArgs and "name" in queryArgs:
page = getPages()[queryArgs["category"]][queryArgs["name"]]
sourcePageContents = textwrap.dedent(
"".join(getsourcelines(page.cell.__func__)[0])
)
if queryArgs.get("noHarness"):
# we've been asked to produce the environment without a code editor
# or the rest of the harness page to be rendered.
locals = {}
exec(sourcePageContents, sys.modules[type(page).__module__].__dict__, locals)
cell = locals["cell"](page).tagged("demo_root")
logging.info("Loading demo with tree\n%s", cell.treeToString())
return cell
edState = cells.SlotEditorState(sourcePageContents)
contentsToEvaluate = cells.Slot(sourcePageContents)
def actualDisplay():
if contentsToEvaluate.get() is not None:
try:
locals = {}
exec(
contentsToEvaluate.get(),
sys.modules[type(page).__module__].__dict__,
locals,
)
return locals["cell"](page).tagged("demo_root")
except Exception:
return cells.Traceback(traceback.format_exc())
return page.cell.tagged("demo_root")
def onEnter(event):
contentsToEvaluate.set(edState.getCurrentState())
ed = cells.Editor(editorState=edState) + cells.KeyAction(
"ctrlKey+Enter", onEnter, stopPropagation=True, preventDefault=True
)
description = page.text()
else:
page = None
description = ""
ed = cells.Card("pick something")
def actualDisplay():
return cells.Card(cells.Text("nothing to display"), padding=10)
resultArea = cells.Subscribed(actualDisplay)
inputArea = cells.Card(cells.Text(description), padding=2) + (
cells.SplitView([(selectionPanel(page), 3), (ed, 6)])
)
return cells.ResizablePanel(resultArea, inputArea, split="horizontal")
def doWork(self, shouldStop):
while not shouldStop.is_set():
shouldStop.wait(100.0)
def reload():
"""Force the process to kill itself. When you refresh,
it'll be the new code."""
import os
os._exit(0)
def selectionPanel(page):
filterBox = cells.SingleLineTextBox("", onEnter=lambda text: substringFilter.set(text))
substringFilter = cells.Slot("")
def getAvailableCells():
availableCells = []
for _, category in sorted(getPages().items()):
for _, item in sorted(category.items()):
displayName = "{}.{}".format(item.category(), item.name())
url = "CellsTestService?{}".format(
urllib.parse.urlencode(dict(category=item.category(), name=item.name()))
)
clickable = cells.Clickable(displayName, url, makeBold=item is page)
# ignore case when filtering
if substringFilter.get().lower() in displayName.lower():
availableCells.append(clickable)
return availableCells
reloadInput = cells.Button(cells.Octicon("sync"), reload)
header = cells.HorizontalSequence([reloadInput, filterBox])
return cells.VScrollable(
cells.Sequence([header, cells.Subscribed(lambda: cells.Sequence(getAvailableCells()))])
)