Skip to content

Commit 9cfa17d

Browse files
committed
FIX #212, FIX #213
1 parent 892a624 commit 9cfa17d

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

apstools/utils.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
import xlrd
6060
import zipfile
6161

62+
from .filewriters import _rebuild_scan_command
63+
6264

6365
logger = logging.getLogger(__name__)
6466

@@ -182,7 +184,7 @@ def itemizer(fmt, items):
182184
return [fmt % k for k in items]
183185

184186

185-
def list_recent_scans(num=20, keys=[], printing=True, db=None):
187+
def list_recent_scans(num=20, keys=[], printing=True, show_command=False, db=None):
186188
"""
187189
make a table of the most recent scans
188190
@@ -192,8 +194,20 @@ def list_recent_scans(num=20, keys=[], printing=True, db=None):
192194
Make the table include the ``num`` most recent scans.
193195
keys : [str] (default: ``[]``)
194196
Include these additional keys from the start document.
197+
198+
Two special keys are supported:
199+
200+
* ``command`` : shows the scan command.
201+
(note: This command is reconstructed from keys in the start
202+
document so it will not be exactly as the user typed.
203+
Also, it will be truncated so that it is no more than 40 characters.)
204+
* ``exit_status`` : from the stop document
205+
195206
printing : bool (default: ``True``)
196207
If True, print the table to stdout
208+
show_command : bool (default: ``False``)
209+
If True, show the (reconstructed) full command,
210+
but truncate it to no more than 40 characters)
197211
db : object (default: ``db`` from the IPython shell)
198212
Instance of ``databroker.Broker()``
199213
@@ -223,7 +237,10 @@ def list_recent_scans(num=20, keys=[], printing=True, db=None):
223237
global_db = None
224238
db = db or global_db
225239

226-
labels = "scan_id plan_name".split() + keys
240+
if show_command:
241+
labels = "scan_id command".split() + keys
242+
else:
243+
labels = "scan_id plan_name".split() + keys
227244

228245
table = pyRestTable.Table()
229246
table.labels = "short_uid date/time".split() + labels
@@ -232,7 +249,20 @@ def list_recent_scans(num=20, keys=[], printing=True, db=None):
232249
row = [
233250
h.start["uid"][:7],
234251
datetime.datetime.fromtimestamp(h.start['time']),
235-
] + [h.start.get(k, "") for k in labels]
252+
]
253+
for k in labels:
254+
if k == "command":
255+
command = _rebuild_scan_command(h.start)
256+
command = command[command.find(" "):].strip()
257+
maxlen = 40
258+
if len(command) > maxlen:
259+
suffix = " ..."
260+
command = command[:maxlen-len(suffix)] + suffix
261+
row.append(command)
262+
elif k == "exit_status":
263+
row.append(h.stop.get(k, ""))
264+
else:
265+
row.append(h.start.get(k, ""))
236266
table.addRow(row)
237267

238268
if printing:

tests/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,24 @@ def test_show_ophyd_symbols(self):
172172
if k not in wont_show:
173173
self.assertTrue(k in rr, msg)
174174
self.assertEqual(num, len(table.rows))
175+
176+
def test_list_recent_scans(self):
177+
from tests.test_export_json import get_db
178+
db = get_db()
179+
headers = db(plan_name="count")
180+
headers = list(headers)[0:1]
181+
self.assertEqual(len(headers), 1)
182+
table = APS_utils.list_recent_scans(
183+
keys=["exit_status",],
184+
show_command=True,
185+
printing=False,
186+
num=10,
187+
db=db,
188+
)
189+
self.assertIsNotNone(table)
190+
self.assertEqual(len(table.labels), 5, "asked for 2 extra columns (total 5)")
191+
self.assertEqual(len(table.rows), 10, "asked for 10 rows")
192+
self.assertLessEqual(len(table.rows[1][3]), 40, "command row should be 40 char or less")
175193

176194

177195
def suite(*args, **kw):

0 commit comments

Comments
 (0)