History

perf scripts python: exported-sql-viewer.py: Add copy to clipboard

Add support for copying to clipboard. Two menu options are added to copy the selected rows / columns with normal spacing, or as comma-separated-values. In the case of trees, only entire rows can be copied. Comitter testing: $ python ~acme/libexec/perf-core/scripts/python/exported-sql-viewer.py ~/c/adrian.hunter/simple-retpoline.db Select the lines, press control+C and on the same terminal, press control+shift+V and voilà: Call Path Object Count Time (ns) Time (%) Branch Count Branch Count (%) ▼ 14503:14503 ▼ _start ld-2.28.so 1 156267 100.0 10602 100.0 unknown unknown 1 2276 1.5 1 0.0 ▼ _dl_start ld-2.28.so 1 137047 87.7 10088 95.2 ▶ unknown unknown 4 4127 3.0 4 0.0 _dl_setup_hash ld-2.28.so 1 0 0.0 1 0.0 ▶ _dl_sysdep_start ld-2.28.so 1 131342 95.8 9981 98.9 ▼ _dl_init ld-2.28.so 1 9142 5.9 326 3.1 ▼ call_init.part.0 ld-2.28.so 3 9133 99.9 319 97.9 ▶ _init libc-2.28.so 1 6877 75.3 110 34.5 ▶ check_stdfiles_vtables libc-2.28.so 1 76 0.8 2 0.6 ▶ init_cacheinfo libc-2.28.so 1 1991 21.8 197 61.8 ▶ _start simple-retpoline 1 7457 4.8 182 1.7

Adrian Hunter committed 7 years ago

Commit 96c43b91 parent 3ac641f1 file · +217 −0Browse files

Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all

Unified · Split

tools/perf/scripts/python/exported-sql-viewer.py+217−0
@@ -884,6 +884,8 @@ class TreeWindowBase(QMdiSubWindow): · 883 unmodified lines
884884 self.find_bar = None
885885
886886 self.view = QTreeView()
887+ self.view.setSelectionMode(QAbstractItemView.ContiguousSelection)
888+ self.view.CopyCellsToClipboard = CopyTreeCellsToClipboard
887889
888890 def DisplayFound(self, ids):
889891 if not len(ids):
@@ -1652,6 +1654,8 @@ class BranchWindow(QMdiSubWindow): · 762 unmodified lines
16521654
16531655 self.view = QTreeView()
16541656 self.view.setUniformRowHeights(True)
1657+ self.view.setSelectionMode(QAbstractItemView.ContiguousSelection)
1658+ self.view.CopyCellsToClipboard = CopyTreeCellsToClipboard
16551659 self.view.setModel(self.model)
16561660
16571661 self.ResizeColumnsToContents()
@@ -2264,6 +2268,207 @@ class ResizeColumnsToContentsBase(QObject): · 606 unmodified lines
22642268 self.data_model.rowsInserted.disconnect(self.UpdateColumnWidths)
22652269 self.ResizeColumnsToContents()
22662270
2271+# Convert value to CSV
2272+
2273+def ToCSValue(val):
2274+ if '"' in val:
2275+ val = val.replace('"', '""')
2276+ if "," in val or '"' in val:
2277+ val = '"' + val + '"'
2278+ return val
2279+
2280+# Key to sort table model indexes by row / column, assuming fewer than 1000 columns
2281+
2282+glb_max_cols = 1000
2283+
2284+def RowColumnKey(a):
2285+ return a.row() * glb_max_cols + a.column()
2286+
2287+# Copy selected table cells to clipboard
2288+
2289+def CopyTableCellsToClipboard(view, as_csv=False, with_hdr=False):
2290+ indexes = sorted(view.selectedIndexes(), key=RowColumnKey)
2291+ idx_cnt = len(indexes)
2292+ if not idx_cnt:
2293+ return
2294+ if idx_cnt == 1:
2295+ with_hdr=False
2296+ min_row = indexes[0].row()
2297+ max_row = indexes[0].row()
2298+ min_col = indexes[0].column()
2299+ max_col = indexes[0].column()
2300+ for i in indexes:
2301+ min_row = min(min_row, i.row())
2302+ max_row = max(max_row, i.row())
2303+ min_col = min(min_col, i.column())
2304+ max_col = max(max_col, i.column())
2305+ if max_col > glb_max_cols:
2306+ raise RuntimeError("glb_max_cols is too low")
2307+ max_width = [0] * (1 + max_col - min_col)
2308+ for i in indexes:
2309+ c = i.column() - min_col
2310+ max_width[c] = max(max_width[c], len(str(i.data())))
2311+ text = ""
2312+ pad = ""
2313+ sep = ""
2314+ if with_hdr:
2315+ model = indexes[0].model()
2316+ for col in range(min_col, max_col + 1):
2317+ val = model.headerData(col, Qt.Horizontal)
2318+ if as_csv:
2319+ text += sep + ToCSValue(val)
2320+ sep = ","
2321+ else:
2322+ c = col - min_col
2323+ max_width[c] = max(max_width[c], len(val))
2324+ width = max_width[c]
2325+ align = model.headerData(col, Qt.Horizontal, Qt.TextAlignmentRole)
2326+ if align & Qt.AlignRight:
2327+ val = val.rjust(width)
2328+ text += pad + sep + val
2329+ pad = " " * (width - len(val))
2330+ sep = " "
2331+ text += "\n"
2332+ pad = ""
2333+ sep = ""
2334+ last_row = min_row
2335+ for i in indexes:
2336+ if i.row() > last_row:
2337+ last_row = i.row()
2338+ text += "\n"
2339+ pad = ""
2340+ sep = ""
2341+ if as_csv:
2342+ text += sep + ToCSValue(str(i.data()))
2343+ sep = ","
2344+ else:
2345+ width = max_width[i.column() - min_col]
2346+ if i.data(Qt.TextAlignmentRole) & Qt.AlignRight:
2347+ val = str(i.data()).rjust(width)
2348+ else:
2349+ val = str(i.data())
2350+ text += pad + sep + val
2351+ pad = " " * (width - len(val))
2352+ sep = " "
2353+ QApplication.clipboard().setText(text)
2354+
2355+def CopyTreeCellsToClipboard(view, as_csv=False, with_hdr=False):
2356+ indexes = view.selectedIndexes()
2357+ if not len(indexes):
2358+ return
2359+
2360+ selection = view.selectionModel()
2361+
2362+ first = None
2363+ for i in indexes:
2364+ above = view.indexAbove(i)
2365+ if not selection.isSelected(above):
2366+ first = i
2367+ break
2368+
2369+ if first is None:
2370+ raise RuntimeError("CopyTreeCellsToClipboard internal error")
2371+
2372+ model = first.model()
2373+ row_cnt = 0
2374+ col_cnt = model.columnCount(first)
2375+ max_width = [0] * col_cnt
2376+
2377+ indent_sz = 2
2378+ indent_str = " " * indent_sz
2379+
2380+ expanded_mark_sz = 2
2381+ if sys.version_info[0] == 3:
2382+ expanded_mark = "\u25BC "
2383+ not_expanded_mark = "\u25B6 "
2384+ else:
2385+ expanded_mark = unicode(chr(0xE2) + chr(0x96) + chr(0xBC) + " ", "utf-8")
2386+ not_expanded_mark = unicode(chr(0xE2) + chr(0x96) + chr(0xB6) + " ", "utf-8")
2387+ leaf_mark = " "
2388+
2389+ if not as_csv:
2390+ pos = first
2391+ while True:
2392+ row_cnt += 1
2393+ row = pos.row()
2394+ for c in range(col_cnt):
2395+ i = pos.sibling(row, c)
2396+ if c:
2397+ n = len(str(i.data()))
2398+ else:
2399+ n = len(str(i.data()).strip())
2400+ n += (i.internalPointer().level - 1) * indent_sz
2401+ n += expanded_mark_sz
2402+ max_width[c] = max(max_width[c], n)
2403+ pos = view.indexBelow(pos)
2404+ if not selection.isSelected(pos):
2405+ break
2406+
2407+ text = ""
2408+ pad = ""
2409+ sep = ""
2410+ if with_hdr:
2411+ for c in range(col_cnt):
2412+ val = model.headerData(c, Qt.Horizontal, Qt.DisplayRole).strip()
2413+ if as_csv:
2414+ text += sep + ToCSValue(val)
2415+ sep = ","
2416+ else:
2417+ max_width[c] = max(max_width[c], len(val))
2418+ width = max_width[c]
2419+ align = model.headerData(c, Qt.Horizontal, Qt.TextAlignmentRole)
2420+ if align & Qt.AlignRight:
2421+ val = val.rjust(width)
2422+ text += pad + sep + val
2423+ pad = " " * (width - len(val))
2424+ sep = " "
2425+ text += "\n"
2426+ pad = ""
2427+ sep = ""
2428+
2429+ pos = first
2430+ while True:
2431+ row = pos.row()
2432+ for c in range(col_cnt):
2433+ i = pos.sibling(row, c)
2434+ val = str(i.data())
2435+ if not c:
2436+ if model.hasChildren(i):
2437+ if view.isExpanded(i):
2438+ mark = expanded_mark
2439+ else:
2440+ mark = not_expanded_mark
2441+ else:
2442+ mark = leaf_mark
2443+ val = indent_str * (i.internalPointer().level - 1) + mark + val.strip()
2444+ if as_csv:
2445+ text += sep + ToCSValue(val)
2446+ sep = ","
2447+ else:
2448+ width = max_width[c]
2449+ if c and i.data(Qt.TextAlignmentRole) & Qt.AlignRight:
2450+ val = val.rjust(width)
2451+ text += pad + sep + val
2452+ pad = " " * (width - len(val))
2453+ sep = " "
2454+ pos = view.indexBelow(pos)
2455+ if not selection.isSelected(pos):
2456+ break
2457+ text = text.rstrip() + "\n"
2458+ pad = ""
2459+ sep = ""
2460+
2461+ QApplication.clipboard().setText(text)
2462+
2463+def CopyCellsToClipboard(view, as_csv=False, with_hdr=False):
2464+ view.CopyCellsToClipboard(view, as_csv, with_hdr)
2465+
2466+def CopyCellsToClipboardHdr(view):
2467+ CopyCellsToClipboard(view, False, True)
2468+
2469+def CopyCellsToClipboardCSV(view):
2470+ CopyCellsToClipboard(view, True, True)
2471+
22672472 # Table window
22682473
22692474 class TableWindow(QMdiSubWindow, ResizeColumnsToContentsBase):
@@ -2282,6 +2487,8 @@ class TableWindow(QMdiSubWindow, ResizeColumnsToContentsBase): · 12 unmodified lines
22822487 self.view.verticalHeader().setVisible(False)
22832488 self.view.sortByColumn(-1, Qt.AscendingOrder)
22842489 self.view.setSortingEnabled(True)
2490+ self.view.setSelectionMode(QAbstractItemView.ContiguousSelection)
2491+ self.view.CopyCellsToClipboard = CopyTableCellsToClipboard
22852492
22862493 self.ResizeColumnsToContents()
22872494
@@ -2398,6 +2605,8 @@ class TopCallsWindow(QMdiSubWindow, ResizeColumnsToContentsBase): · 110 unmodified lines
23982605 self.view.setModel(self.model)
23992606 self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
24002607 self.view.verticalHeader().setVisible(False)
2608+ self.view.setSelectionMode(QAbstractItemView.ContiguousSelection)
2609+ self.view.CopyCellsToClipboard = CopyTableCellsToClipboard
24012610
24022611 self.ResizeColumnsToContents()
24032612
@@ -2735,6 +2944,8 @@ class MainWindow(QMainWindow): · 331 unmodified lines
27352944 file_menu.addAction(CreateExitAction(glb.app, self))
27362945
27372946 edit_menu = menu.addMenu("&Edit")
2947+ edit_menu.addAction(CreateAction("&Copy", "Copy to clipboard", self.CopyToClipboard, self, QKeySequence.Copy))
2948+ edit_menu.addAction(CreateAction("Copy as CS&V", "Copy to clipboard as CSV", self.CopyToClipboardCSV, self))
27382949 edit_menu.addAction(CreateAction("&Find...", "Find items", self.Find, self, QKeySequence.Find))
27392950 edit_menu.addAction(CreateAction("Fetch &more records...", "Fetch more records", self.FetchMoreRecords, self, [QKeySequence(Qt.Key_F8)]))
27402951 edit_menu.addAction(CreateAction("&Shrink Font", "Make text smaller", self.ShrinkFont, self, [QKeySequence("Ctrl+-")]))
@@ -2767,6 +2978,12 @@ class MainWindow(QMainWindow): · 26 unmodified lines
27672978 except:
27682979 pass
27692980
2981+ def CopyToClipboard(self):
2982+ self.Try(CopyCellsToClipboardHdr)
2983+
2984+ def CopyToClipboardCSV(self):
2985+ self.Try(CopyCellsToClipboardCSV)
2986+
27702987 def Find(self):
27712988 win = self.mdi_area.activeSubWindow()
27722989 if win:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t