This repository has been archived by the owner on May 7, 2023. It is now read-only.
/
memoryprofiler.py
162 lines (127 loc) · 5.69 KB
/
memoryprofiler.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
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
# -*- coding: utf-8 -*-
#
# Copyright © 2013 Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see LICENSE.txt for details)
"""Memory profiler Plugin."""
# Third party imports
from qtpy.QtCore import Qt, Signal
from qtpy.QtWidgets import QVBoxLayout, QGroupBox, QLabel
# Need running QApplication before importing runconfig
from spyder.utils.qthelpers import qapplication
MAIN_APP = qapplication()
from spyder.api.plugins import SpyderPluginWidget
from spyder.api.preferences import PluginConfigPage
from spyder.config.base import get_translation
from spyder.utils.qthelpers import get_icon, create_action
# Local imports
from .widgets.memoryprofiler import (MemoryProfilerWidget,
is_memoryprofiler_installed)
_ = get_translation("memory_profiler", dirname="spyder_memory_profiler")
class MemoryProfilerConfigPage(PluginConfigPage):
"""
Widget with configuration options for memory profiler.
"""
def setup_page(self):
settings_group = QGroupBox(_("Settings"))
use_color_box = self.create_checkbox(
_("Use deterministic colors to differentiate functions"),
'use_colors', default=True)
results_group = QGroupBox(_("Results"))
results_label1 = QLabel(_("Memory profiler plugin results "
"(the output of memory_profiler)\n"
"is stored here:"))
results_label1.setWordWrap(True)
# Warning: do not try to regroup the following QLabel contents with
# widgets above -- this string was isolated here in a single QLabel
# on purpose: to fix Issue 863 of Profiler plugon
results_label2 = QLabel(MemoryProfilerWidget.DATAPATH)
results_label2.setTextInteractionFlags(Qt.TextSelectableByMouse)
results_label2.setWordWrap(True)
settings_layout = QVBoxLayout()
settings_layout.addWidget(use_color_box)
settings_group.setLayout(settings_layout)
results_layout = QVBoxLayout()
results_layout.addWidget(results_label1)
results_layout.addWidget(results_label2)
results_group.setLayout(results_layout)
vlayout = QVBoxLayout()
vlayout.addWidget(settings_group)
vlayout.addWidget(results_group)
vlayout.addStretch(1)
self.setLayout(vlayout)
class MemoryProfiler(SpyderPluginWidget):
"""Memory profiler."""
CONF_SECTION = 'memoryprofiler'
CONF_DEFAULTS = [(CONF_SECTION, {'use_colors': True})]
CONFIGWIDGET_CLASS = MemoryProfilerConfigPage
def __init__(self, parent=None):
SpyderPluginWidget.__init__(self, parent)
# Create widget and add to dockwindow
self.widget = MemoryProfilerWidget(self.main)
layout = QVBoxLayout()
layout.addWidget(self.widget)
self.setLayout(layout)
def update_pythonpath(self):
"""
Update the PYTHONPATH used when running the line_profiler.
This function is called whenever the Python path set in Spyder changes.
It synchronizes the PYTHONPATH in the line_profiler widget with the
PYTHONPATH in Spyder.
"""
self.widget.spyder_pythonpath = self.main.get_spyder_pythonpath()
# --- SpyderPluginWidget API ----------------------------------------------
def get_plugin_title(self):
"""Return widget title."""
return _("Memory profiler")
def get_plugin_icon(self):
"""Return widget icon."""
return get_icon('profiler.png')
def get_focus_widget(self):
"""
Return the widget to give focus to when this plugin's dockwidget is
raised on top-level.
"""
return self.widget.datatree
def get_plugin_actions(self):
"""Return a list of actions related to plugin."""
return []
def on_first_registration(self):
"""Action to be performed on first plugin registration."""
self.main.tabify_plugins(self.main.help, self)
self.dockwidget.hide()
def register_plugin(self):
"""Register plugin in Spyder's main window."""
super(MemoryProfiler, self).register_plugin()
# Spyder PYTHONPATH
self.update_pythonpath()
self.main.sig_pythonpath_changed.connect(self.update_pythonpath)
self.widget.datatree.edit_goto.connect(self.main.editor.load)
self.widget.redirect_stdio.connect(self.main.redirect_internalshell_stdio)
memoryprofiler_act = create_action(self,
_("Profile memory line by line"),
icon=self.get_plugin_icon(),
shortcut="Ctrl+Shift+F10",
triggered=self.run_memoryprofiler)
memoryprofiler_act.setEnabled(is_memoryprofiler_installed())
self.main.run_menu_actions += [memoryprofiler_act]
self.main.editor.pythonfile_dependent_actions += [memoryprofiler_act]
def refresh_plugin(self):
"""Refresh memory profiler widget."""
pass
def closing_plugin(self, cancelable=False):
"""Perform actions before parent main window is closed."""
return True
def apply_plugin_settings(self, options):
"""Apply configuration file's plugin settings."""
pass
# --- Public API ----------------------------------------------------------
def run_memoryprofiler(self):
"""Run memory profiler."""
self.analyze(self.main.editor.get_current_filename())
def analyze(self, filename):
"""Reimplement analyze method."""
if self.dockwidget:
self.switch_to_plugin()
self.widget.analyze(
filename, use_colors=self.get_option('use_colors', True))