Skip to content

Commit f1082e4

Browse files
authored
fix #173 : when the editor is called from a script, displays the filename and line number in the status bar
1 parent 18e1240 commit f1082e4

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

larray_editor/api.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import sys
44
import traceback
5+
from inspect import getframeinfo
56
from collections import OrderedDict
67

7-
from qtpy.QtWidgets import QApplication, QMainWindow
8+
from qtpy.QtWidgets import QApplication
89
import larray as la
910

1011
from larray_editor.editor import REOPEN_LAST_FILE, MappingEditor, ArrayEditor
@@ -65,7 +66,7 @@ def get_title(obj, depth=0, maxnames=3):
6566
return ', '.join(names)
6667

6768

68-
def edit(obj=None, title='', minvalue=None, maxvalue=None, readonly=False, depth=0):
69+
def edit(obj=None, title='', minvalue=None, maxvalue=None, readonly=False, depth=0, display_caller_info=True):
6970
"""
7071
Opens a new editor window.
7172
@@ -87,6 +88,9 @@ def edit(obj=None, title='', minvalue=None, maxvalue=None, readonly=False, depth
8788
Whether or not editing array values is forbidden. Defaults to False.
8889
depth : int, optional
8990
Stack depth where to look for variables. Defaults to 0 (where this function was called).
91+
display_caller_info: bool, optional
92+
Whether or not to display the filename and line number where the Editor has been called.
93+
Defaults to True.
9094
9195
Examples
9296
--------
@@ -109,8 +113,13 @@ def edit(obj=None, title='', minvalue=None, maxvalue=None, readonly=False, depth
109113
else:
110114
parent = _app.activeWindow()
111115

116+
caller_frame = sys._getframe(depth + 1)
117+
if display_caller_info:
118+
caller_info = getframeinfo(caller_frame)
119+
else:
120+
caller_info = None
121+
112122
if obj is None:
113-
caller_frame = sys._getframe(depth + 1)
114123
global_vars = caller_frame.f_globals
115124
local_vars = caller_frame.f_locals
116125
obj = OrderedDict()
@@ -126,10 +135,11 @@ def edit(obj=None, title='', minvalue=None, maxvalue=None, readonly=False, depth
126135
if obj is REOPEN_LAST_FILE or isinstance(obj, (str, la.Session)):
127136
dlg = MappingEditor(parent)
128137
assert minvalue is None and maxvalue is None
129-
setup_ok = dlg.setup_and_check(obj, title=title, readonly=readonly)
138+
setup_ok = dlg.setup_and_check(obj, title=title, readonly=readonly, caller_info=caller_info)
130139
else:
131140
dlg = ArrayEditor(parent)
132-
setup_ok = dlg.setup_and_check(obj, title=title, readonly=readonly, minvalue=minvalue, maxvalue=maxvalue)
141+
setup_ok = dlg.setup_and_check(obj, title=title, readonly=readonly, minvalue=minvalue, maxvalue=maxvalue,
142+
caller_info=caller_info)
133143

134144
if setup_ok:
135145
dlg.show()
@@ -138,7 +148,7 @@ def edit(obj=None, title='', minvalue=None, maxvalue=None, readonly=False, depth
138148
restore_except_hook()
139149

140150

141-
def view(obj=None, title='', depth=0):
151+
def view(obj=None, title='', depth=0, display_caller_info=True):
142152
"""
143153
Opens a new viewer window. Arrays are loaded in readonly mode and their content cannot be modified.
144154
@@ -153,6 +163,9 @@ def view(obj=None, title='', depth=0):
153163
object).
154164
depth : int, optional
155165
Stack depth where to look for variables. Defaults to 0 (where this function was called).
166+
display_caller_info: bool, optional
167+
Whether or not to display the filename and line number where the Editor has been called.
168+
Defaults to True.
156169
157170
Examples
158171
--------
@@ -164,7 +177,7 @@ def view(obj=None, title='', depth=0):
164177
>>> # will open a viewer showing only a1
165178
>>> view(a1) # doctest: +SKIP
166179
"""
167-
edit(obj, title=title, readonly=True, depth=depth + 1)
180+
edit(obj, title=title, readonly=True, depth=depth + 1, display_caller_info=display_caller_info)
168181

169182

170183
def compare(*args, **kwargs):
@@ -182,6 +195,9 @@ def compare(*args, **kwargs):
182195
namespace which correspond to the passed objects.
183196
depth : int, optional
184197
Stack depth where to look for variables. Defaults to 0 (where this function was called).
198+
display_caller_info: bool, optional
199+
Whether or not to display the filename and line number where the Editor has been called.
200+
Defaults to True.
185201
186202
Examples
187203
--------
@@ -195,13 +211,20 @@ def compare(*args, **kwargs):
195211
title = kwargs.pop('title', '')
196212
names = kwargs.pop('names', None)
197213
depth = kwargs.pop('depth', 0)
214+
display_caller_info = kwargs.pop('display_caller_info', True)
198215
_app = QApplication.instance()
199216
if _app is None:
200217
_app = qapplication()
201218
parent = None
202219
else:
203220
parent = _app.activeWindow()
204221

222+
caller_frame = sys._getframe(depth + 1)
223+
if display_caller_info:
224+
caller_info = getframeinfo(caller_frame)
225+
else:
226+
caller_info = None
227+
205228
if any(isinstance(a, la.Session) for a in args):
206229
from larray_editor.comparator import SessionComparator
207230
dlg = SessionComparator(parent)
@@ -221,7 +244,7 @@ def get_name(i, obj, depth=0):
221244
else:
222245
assert isinstance(names, list) and len(names) == len(args)
223246

224-
if dlg.setup_and_check(args, names=names, title=title):
247+
if dlg.setup_and_check(args, names=names, title=title, caller_info=caller_info):
225248
dlg.show()
226249
_app.exec_()
227250

larray_editor/editor.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(self, parent=None, editable=False, file_menu=False, help_menu=False
7373
if editable:
7474
self.edit_undo_stack = QUndoStack(self)
7575

76-
def setup_and_check(self, data, title='', readonly=False, **kwargs):
76+
def setup_and_check(self, data, title='', readonly=False, caller_info=None, **kwargs):
7777
"""Return False if data is not supported, True otherwise"""
7878
# set icon
7979
icon = ima.icon('larray')
@@ -88,7 +88,11 @@ def setup_and_check(self, data, title='', readonly=False, **kwargs):
8888
self._title = title
8989
self.setWindowTitle(title)
9090

91-
# display status message
91+
# permanently display caller info in the status bar
92+
if caller_info is not None:
93+
caller_info = 'launched from file {} at line {}'.format(caller_info.filename, caller_info.lineno)
94+
self.statusBar().addPermanentWidget(QLabel(caller_info))
95+
# display welcome message
9296
self.statusBar().showMessage("Welcome to the {}".format(self.name), 4000)
9397

9498
# set central widget
@@ -99,7 +103,7 @@ def setup_and_check(self, data, title='', readonly=False, **kwargs):
99103
self._setup_and_check(widget, data, title, readonly, **kwargs)
100104

101105
# resize
102-
self.resize(800, 600)
106+
self.resize(1000, 600)
103107
# This is more or less the minimum space required to display a 1D array
104108
self.setMinimumSize(300, 180)
105109
return True
@@ -303,7 +307,7 @@ def __init__(self, parent=None):
303307

304308
self.setup_menu_bar()
305309

306-
def _setup_and_check(self, widget, data, title, readonly):
310+
def _setup_and_check(self, widget, data, title, readonly, **kwargs):
307311
"""Setup MappingEditor"""
308312
layout = QVBoxLayout()
309313
widget.setLayout(layout)

larray_editor/start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def main():
99
if os.name == 'nt':
1010
sys.stdout = open(os.devnull, "w")
1111
sys.stderr = open(os.path.join(os.getenv("TEMP"), "stderr-" + os.path.basename(sys.argv[0])), "w")
12-
edit(*sys.argv[1:])
12+
edit(*sys.argv[1:], display_caller_info=False)
1313

1414

1515
if __name__ == '__main__':

0 commit comments

Comments
 (0)