-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheditor.py
More file actions
1595 lines (1394 loc) · 67.9 KB
/
Copy patheditor.py
File metadata and controls
1595 lines (1394 loc) · 67.9 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import importlib
import io
import logging
import os
import re
import sys
from collections.abc import Sequence
from contextlib import redirect_stdout
from pathlib import Path
from typing import Union
# Python3.8 switched from a Selector to a Proactor based event loop for asyncio but they do not offer the same
# features, which breaks Tornado and all projects depending on it, including Jupyter consoles
# ref: https://github.com/larray-project/larray-editor/issues/208
if sys.platform.startswith("win") and sys.version_info >= (3, 8):
import asyncio
try:
from asyncio import WindowsProactorEventLoopPolicy, WindowsSelectorEventLoopPolicy
except ImportError:
# not affected
pass
else:
if type(asyncio.get_event_loop_policy()) is WindowsProactorEventLoopPolicy:
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
import matplotlib
# explicitly request Qt backend (fixes #278)
matplotlib.use('QtAgg')
import matplotlib.axes
import matplotlib.figure
import numpy as np
import larray as la
from qtpy.QtCore import Qt, QUrl, QSettings
from qtpy.QtGui import QDesktopServices, QKeySequence
from qtpy.QtWidgets import (QMainWindow, QWidget, QListWidget, QListWidgetItem, QSplitter, QFileDialog, QPushButton,
QDialogButtonBox, QShortcut,
QHBoxLayout, QVBoxLayout, QGridLayout, QLineEdit,
QCheckBox, QComboBox, QMessageBox, QDialog,
QInputDialog, QLabel, QGroupBox, QRadioButton,
QTabWidget)
try:
from qtpy.QtWidgets import QUndoStack
except ImportError:
# PySide6 provides QUndoStack in QtGui
# unsure qtpy has been fixed yet (see https://github.com/spyder-ide/qtpy/pull/366 for the fix for QUndoCommand)
from qtpy.QtGui import QUndoStack
from larray_editor.traceback_tools import StackSummary
from larray_editor.utils import (_,
create_action,
show_figure,
ima,
commonpath,
DEPENDENCIES,
get_versions,
get_documentation_url,
URLS,
RecentlyUsedList,
logger, list_drives)
from larray_editor.arraywidget import ArrayEditorWidget
from larray_editor import arrayadapter
from larray_editor.commands import EditSessionArrayCommand, EditCurrentArrayCommand
from larray_editor.sql import SQLWidget
try:
from qtconsole.rich_jupyter_widget import RichJupyterWidget
from qtconsole.inprocess import QtInProcessKernelManager
from IPython import get_ipython
ipython_instance = get_ipython()
# Having several instances of IPython of different types in the same
# process are not supported. We use
# ipykernel.inprocess.ipkernel.InProcessInteractiveShell
# and qtconsole and notebook use
# ipykernel.zmqshell.ZMQInteractiveShell, so this cannot work.
# For now, we simply fallback to not using IPython if we are run
# from IPython (whether qtconsole or notebook). The correct solution is
# probably to run the IPython console in a different process but I do not
# know what would be the consequences. I fear it could be slow to transfer
# the session data to the other process.
if ipython_instance is None:
qtconsole_available = True
else:
qtconsole_available = False
except ImportError:
qtconsole_available = False
REOPEN_LAST_FILE = object()
ASSIGNMENT_PATTERN = re.compile(r'[^\[\]]+[^=]=[^=].+')
# This will match for:
# * variable = expr
# * variable[key] = expr
# * variable.attribute = expr
# * variable.attribute[key] = expr
# and their inplace ops counterpart
UPDATE_VARIABLE_PATTERN = re.compile(
r'(?P<variable>\w+)'
r'(?P<attribute>\.\w+)?'
r'(?P<subset>\[.+\])?'
r'\s*'
r'(?P<inplaceop>[-+*/%&|^><]|//|\*\*|>>|<<)?'
r'=\s*[^=].*'
) # = expr
HISTORY_VARS_PATTERN = re.compile(r'_i?\d+')
opened_secondary_windows = []
# TODO: remember its size
# like MappingEditor via self.set_window_size_and_geometry()
class EditorWindow(QWidget):
default_width = 800
default_height = 600
# This is more or less the minimum space required to display a 1D array
minimum_width = 300
minimum_height = 180
name = "Editor"
def __init__(self, data, title=None, readonly=False):
# for QWidget to act as a window, parent must be None
super().__init__(parent=None)
layout = QVBoxLayout()
self.setLayout(layout)
header_layout = self.setup_header_layout()
if header_layout is not None:
layout.addLayout(header_layout)
array_widget = ArrayEditorWidget(self, data=data, readonly=readonly)
self.array_widget = array_widget
layout.addWidget(array_widget)
icon = ima.icon('larray')
if icon is not None:
self.setWindowIcon(icon)
if title is None:
title = self.name
self.setWindowTitle(title)
# TODO: somehow determine better width
self.resize(self.default_width, self.default_height)
def setup_header_layout(self):
return None
def closeEvent(self, event):
logger.debug('EditorWindow.closeEvent()')
if self in opened_secondary_windows:
opened_secondary_windows.remove(self)
super().closeEvent(event)
self.array_widget.close()
class FileExplorerWindow(EditorWindow):
name = "File Explorer"
def create_drive_button_clicked_callback(self, drive):
def callback():
path = Path(drive)
if not path.exists():
msg = f"The {drive} drive is currently unavailable !"
QMessageBox.critical(self, "Error", msg)
return
self.array_widget.set_data(path)
return callback
def setup_header_layout(self):
drives = list_drives()
if not drives:
return None
layout = QHBoxLayout()
for drive in drives:
if drive.endswith('\\'):
drive = drive[:-1]
button = QPushButton(drive)
button.clicked.connect(
self.create_drive_button_clicked_callback(drive)
)
layout.addWidget(button)
layout.addStretch()
return layout
class AbstractEditorWindow(QMainWindow):
"""Abstract Editor Window"""
name = "Editor"
editable = False
file_menu = False
help_menu = False
default_width = 1000
default_height = 600
# This is more or less the minimum space required to display a 1D array
minimum_width = 300
minimum_height = 180
def __init__(self, title='', readonly=False, caller_info=None, parent=None):
QMainWindow.__init__(self, parent)
# Destroying the C++ object right after closing the dialog box,
# otherwise it may be garbage-collected in another QThread
# (e.g. the editor's analysis thread in Spyder), thus leading to
# a segmentation fault on UNIX or an application crash on Windows
self.setAttribute(Qt.WA_DeleteOnClose)
self.data = None
if self.editable:
self.edit_undo_stack = QUndoStack(self)
self.settings_group_name = self.name.lower().replace(' ', '_')
self.widgets_to_save_to_settings = {}
# set icon
icon = ima.icon('larray')
if icon is not None:
self.setWindowIcon(icon)
# set title
if not title:
title = _(self.name)
if readonly:
title += ' (' + _('read only') + ')'
self._title = title
self.setWindowTitle(title)
# permanently display caller info in the status bar
if caller_info is not None:
caller_info = f'launched from file {caller_info.filename} at line {caller_info.lineno}'
self.statusBar().addPermanentWidget(QLabel(caller_info))
# display welcome message
self.statusBar().showMessage(f"Welcome to the {self.name}", 4000)
# set central widget
widget = QWidget()
self.setCentralWidget(widget)
def setup_menu_bar(self):
"""Setup menu bar"""
menu_bar = self.menuBar()
if self.file_menu:
self._setup_file_menu(menu_bar)
if self.editable:
self._setup_edit_menu(menu_bar)
if self.help_menu:
self._setup_help_menu(menu_bar)
def _setup_file_menu(self, menu_bar):
file_menu = menu_bar.addMenu('&File')
file_menu.addAction(create_action(self, _('&Quit'), shortcut="Ctrl+Q", triggered=self.close))
def _setup_edit_menu(self, menu_bar):
if qtconsole_available:
edit_menu = menu_bar.addMenu('&Edit')
# UNDO
undo_action = self.edit_undo_stack.createUndoAction(self, "&Undo")
undo_action.setShortcuts(QKeySequence.Undo)
undo_action.triggered.connect(self.update_title)
edit_menu.addAction(undo_action)
# REDO
redo_action = self.edit_undo_stack.createRedoAction(self, "&Redo")
redo_action.setShortcuts(QKeySequence.Redo)
redo_action.triggered.connect(self.update_title)
edit_menu.addAction(redo_action)
def _setup_help_menu(self, menu_bar):
help_menu = menu_bar.addMenu('&Help')
# ============= #
# DOCUMENTATION #
# ============= #
help_menu.addAction(create_action(self, _('Online &Documentation'), shortcut="Ctrl+H",
triggered=self.open_documentation))
help_menu.addAction(create_action(self, _('Online &Tutorial'), triggered=self.open_tutorial))
help_menu.addAction(create_action(self, _('Online Objects and Functions (API) &Reference'),
triggered=self.open_api_documentation))
# ==================== #
# ISSUES/GOOGLE GROUPS #
# ==================== #
help_menu.addSeparator()
report_issue_menu = help_menu.addMenu("Report &Issue...")
report_issue_menu.addAction(create_action(self, _('Report &Editor Issue...'),
triggered=self.report_issue('editor')))
report_issue_menu.addAction(create_action(self, _('Report &LArray Issue...'),
triggered=self.report_issue('larray')))
report_issue_menu.addAction(create_action(self, _('Report &LArray Eurostat Issue...'),
triggered=self.report_issue('larray_eurostat')))
help_menu.addAction(create_action(self, _('&Users Discussion...'), triggered=self.open_users_group))
help_menu.addAction(create_action(self, _('New Releases And &Announces Mailing List...'),
triggered=self.open_announce_group))
# =============== #
# ABOUT #
# =============== #
help_menu.addSeparator()
help_menu.addAction(create_action(self, _('&About'), triggered=self.about))
def open_documentation(self):
QDesktopServices.openUrl(QUrl(get_documentation_url('doc_index')))
def open_tutorial(self):
QDesktopServices.openUrl(QUrl(get_documentation_url('doc_tutorial')))
def open_api_documentation(self):
QDesktopServices.openUrl(QUrl(get_documentation_url('doc_api')))
def report_issue(self, package):
def _report_issue(*args, **kwargs):
from urllib.parse import quote
versions = get_versions(package)
issue_template = """\
## Description
**What steps will reproduce the problem?**
1.
2.
3.
**What is the expected output? What do you see instead?**
**Please provide any additional information below**
## Version and main components
* Python {python} on {system} {bitness:d}bits
"""
issue_template += f"* {package} {{{package}}}\n"
for dep in DEPENDENCIES[package]:
issue_template += f"* {dep} {{{dep}}}\n"
issue_template = issue_template.format(**versions)
url = QUrl(URLS[f'new_issue_{package}'])
from qtpy.QtCore import QUrlQuery
query = QUrlQuery()
query.addQueryItem("body", quote(issue_template))
url.setQuery(query)
QDesktopServices.openUrl(url)
return _report_issue
def open_users_group(self):
QDesktopServices.openUrl(QUrl(URLS['users_group']))
def open_announce_group(self):
QDesktopServices.openUrl(QUrl(URLS['announce_group']))
def about(self):
"""About Editor"""
kwargs = get_versions('editor')
kwargs.update(URLS)
message = """\
<p><b>LArray Editor</b> {editor}
<br>The Graphical User Interface for LArray
<p>Licensed under the terms of the <a href="{GPL3}">GNU General Public License Version 3</a>.
<p>Developed and maintained by the <a href="{fpb}">Federal Planning Bureau</a> (Belgium).
<p>
<p><b>Versions of underlying libraries</b>
<ul>
<li>Python {python} on {system} {bitness:d}bits</li>
"""
for dep in DEPENDENCIES['editor']:
message += f"<li>{dep} {kwargs[dep]}</li>\n"
message += "</ul>"
QMessageBox.about(self, _("About LArray Editor"), message.format(**kwargs))
def _update_title(self, title, value, name):
if title is None:
title = []
if value is not None:
# TODO: the type-specific information added to the title should be
# computed by a method on the adapter
# (self.array_widget.data_adapter)
if hasattr(value, 'dtype'):
try:
dtype_str = f' [{value.dtype.name}]'
except Exception:
dtype_str = ''
else:
dtype_str = ''
if hasattr(value, 'shape'):
def format_int(value: int):
if value >= 10_000:
return f'{value:_}'
else:
return str(value)
if isinstance(value, la.Array):
shape = [f'{display_name} ({format_int(len(axis))})'
for display_name, axis in zip(value.axes.display_names, value.axes)]
else:
try:
shape = [format_int(length) for length in value.shape]
except Exception:
shape = []
shape_str = ' x '.join(shape)
else:
shape_str = ''
# name + shape + dtype
value_info = shape_str + dtype_str
if name and value_info:
title.append(name + ': ' + value_info)
elif name:
title.append(name)
elif value_info:
title.append(value_info)
# extra info
title.append(self._title)
# set title
self.setWindowTitle(' - '.join(title))
def get_value(self):
"""Return modified array -- this is *not* a copy"""
# It is import to avoid accessing Qt C++ object as it has probably
# already been destroyed, due to the Qt.WA_DeleteOnClose attribute
return self.data
def save_widgets_state_and_geometry(self):
settings = QSettings()
settings.beginGroup(self.settings_group_name)
settings.setValue('geometry', self.saveGeometry())
settings.setValue('state', self.saveState())
for widget_name, widget in self.widgets_to_save_to_settings.items():
settings.beginGroup(f'widget/{widget_name}')
if hasattr(widget, 'save_to_settings'):
widget.save_to_settings(settings)
elif hasattr(widget, 'saveState'):
settings.setValue('state', widget.saveState())
settings.endGroup()
settings.endGroup()
def restore_widgets_state_and_geometry(self):
settings = QSettings()
settings.beginGroup(self.settings_group_name)
geometry = settings.value('geometry')
if geometry:
self.restoreGeometry(geometry)
state = settings.value('state')
if state:
self.restoreState(state)
for widget_name, widget in self.widgets_to_save_to_settings.items():
settings.beginGroup(f'widget/{widget_name}')
if hasattr(widget, 'load_from_settings'):
widget.load_from_settings(settings)
elif hasattr(widget, 'restoreState'):
widget_state = settings.value('state')
if widget_state:
widget.restoreState(widget_state)
settings.endGroup()
settings.endGroup()
return (geometry is not None) or (state is not None)
def set_window_size_and_geometry(self):
if not self.restore_widgets_state_and_geometry():
self.resize(self.default_width, self.default_height)
self.setMinimumSize(self.minimum_width, self.minimum_height)
def update_title(self):
raise NotImplementedError()
def void_formatter(obj, p, cycle):
"""
p: PrettyPrinter
has a .text() method to output text.
cycle: bool
Indicates whether the object is part of a reference cycle.
"""
adapter_creator = arrayadapter.get_adapter_creator(obj)
if isinstance(adapter_creator, str):
# the string is an error message => we cannot handle that object
# => use normal formatting
# we can get in this case if we registered a void_formatter for a type
# (such as Sequence) for which we handle some instances of the type
# but not all
p.text(repr(obj))
else:
# we already display the object in the grid
# => do not print it in the console
return
class MappingEditorWindow(AbstractEditorWindow):
"""Session Editor Dialog"""
name = "Session Editor"
editable = True
file_menu = True
help_menu = True
def __init__(self, data, title='', readonly=False, caller_info=None,
parent=None, stack_pos=None, add_larray_functions=False,
python_console=True, sql_console=None):
AbstractEditorWindow.__init__(self, title=title, readonly=readonly, caller_info=caller_info,
parent=parent)
if sql_console is None:
# This was meant to test whether users actually imported polars
# in their script instead of just testing whether polars is present
# in their environment but, in practice, this currently only does
# the later because: larray_editor unconditionally imports larray
# which imports xlwings when available, which imports polars when
# available.
sql_console = 'polars' in sys.modules
logger.debug("polars module is present, enabling SQL console")
elif sql_console:
if importlib.util.find_spec('polars') is None:
raise RuntimeError("SQL console is not available because "
"the 'polars' module is not available")
self.current_file = None
self.current_array = None
self.current_expr_text = None
self.expressions = {}
self.ipython_kernel = None
self._unsaved_modifications = False
# to handle recently opened data/script files
self.recent_data_files = RecentlyUsedList("recentFileList", self, self.open_recent_file)
self.recent_saved_scripts = RecentlyUsedList("recentSavedScriptList")
self.recent_loaded_scripts = RecentlyUsedList("recentLoadedScriptList")
self.setup_menu_bar()
widget = self.centralWidget()
layout = QVBoxLayout()
widget.setLayout(layout)
self._listwidget = QListWidget(self)
# this is a bit more reliable than currentItemChanged which is not emitted when no item was selected before
self._listwidget.itemSelectionChanged.connect(self.on_selection_changed)
self._listwidget.itemDoubleClicked.connect(self.display_item_in_new_window)
self._listwidget.setMinimumWidth(45)
del_item_shortcut = QShortcut(QKeySequence(Qt.Key_Delete), self._listwidget)
del_item_shortcut.activated.connect(self.delete_current_item)
self.data = la.Session()
self.array_widget = ArrayEditorWidget(self, readonly=readonly)
self.array_widget.dataChanged.connect(self.push_changes)
# FIXME: this is currently broken as it fires for each scroll
# we either need to fix model_data.dataChanged (but that might
# be needed for display) or find another way to add a star to
# the window title *only* when the user actually changed
# something
# self.array_widget.model_data.dataChanged.connect(self.update_title)
if sql_console:
sql_widget = SQLWidget(self)
self.widgets_to_save_to_settings['sql_console'] = sql_widget
else:
sql_widget = None
self.sql_widget = sql_widget
if python_console:
if qtconsole_available:
# silence a warning on Python 3.11 (see issue #263)
if "PYDEVD_DISABLE_FILE_VALIDATION" not in os.environ:
os.environ["PYDEVD_DISABLE_FILE_VALIDATION"] = "1"
# Create an in-process kernel
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel(show_banner=False)
kernel = kernel_manager.kernel
self.ipython_kernel = kernel
text_formatter = kernel.shell.display_formatter.formatters['text/plain']
for type_ in arrayadapter.REGISTERED_ADAPTERS:
text_formatter.for_type(type_, void_formatter)
kernel.shell.push({
'__editor__': self
})
if add_larray_functions:
kernel.shell.run_cell('from larray import *')
self.ipython_cell_executed()
kernel_client = kernel_manager.client()
kernel_client.start_channels()
ipython_widget = RichJupyterWidget()
ipython_widget.kernel_manager = kernel_manager
ipython_widget.kernel_client = kernel_client
ipython_widget.executed.connect(self.ipython_cell_executed)
ipython_widget._display_banner = False
self.eval_box = ipython_widget
self.eval_box.setMinimumHeight(20)
right_panel_widget = QSplitter(Qt.Vertical)
right_panel_widget.addWidget(self.array_widget)
if sql_console:
tab_widget = QTabWidget(self)
tab_widget.addTab(self.eval_box, 'Python Console')
tab_widget.addTab(sql_widget, 'SQL Console')
right_panel_widget.addWidget(tab_widget)
else:
right_panel_widget.addWidget(self.eval_box)
right_panel_widget.setSizes([90, 10])
self.widgets_to_save_to_settings['right_panel_widget'] = right_panel_widget
else:
# cannot easily use a QTextEdit because it has no returnPressed signal
self.eval_box = QLineEdit()
self.eval_box.returnPressed.connect(self.line_edit_update)
right_panel_layout = QVBoxLayout()
right_panel_layout.addWidget(self.array_widget)
right_panel_layout.addWidget(self.eval_box)
# you cant add a layout directly in a splitter, so we have to wrap
# it in a widget
right_panel_widget = QWidget()
right_panel_widget.setLayout(right_panel_layout)
elif sql_console:
right_panel_widget = QSplitter(Qt.Vertical)
right_panel_widget.addWidget(self.array_widget)
right_panel_widget.addWidget(sql_widget)
right_panel_widget.setSizes([90, 10])
self.widgets_to_save_to_settings['right_panel_widget'] = right_panel_widget
main_splitter = QSplitter(Qt.Horizontal)
debug = isinstance(data, StackSummary)
if debug:
self._stack_frame_widget = QListWidget(self)
stack_frame_widget = self._stack_frame_widget
stack_frame_widget.itemSelectionChanged.connect(self.on_stack_frame_changed)
stack_frame_widget.setMinimumWidth(60)
for frame_summary in data:
funcname = frame_summary.name
filename = os.path.basename(frame_summary.filename)
listitem = QListWidgetItem(stack_frame_widget)
listitem.setText(f"{funcname}, {filename}:{frame_summary.lineno}")
# we store the frame summary object in the user data of the list
listitem.setData(Qt.UserRole, frame_summary)
listitem.setToolTip(frame_summary.line)
row = stack_pos if stack_pos is not None else len(data) - 1
stack_frame_widget.setCurrentRow(row)
left_panel_widget = QSplitter(Qt.Vertical)
left_panel_widget.addWidget(self._listwidget)
left_panel_widget.addWidget(stack_frame_widget)
left_panel_widget.setSizes([500, 200])
data = self.data
else:
left_panel_widget = self._listwidget
main_splitter.addWidget(left_panel_widget)
main_splitter.addWidget(right_panel_widget)
main_splitter.setSizes([180, 620])
main_splitter.setCollapsible(1, False)
self.widgets_to_save_to_settings['main_splitter'] = main_splitter
layout.addWidget(main_splitter)
# check if reopen last opened file
if data is REOPEN_LAST_FILE:
if len(self.recent_data_files.files) > 0:
data = self.recent_data_file.files[0]
else:
data = la.Session()
# load file if any
if isinstance(data, (str, Path)):
if os.path.isfile(data):
self._open_file(data)
else:
QMessageBox.critical(self, "Error", f"File {data} could not be found")
self.new()
elif not debug:
self._push_data(data)
self.set_window_size_and_geometry()
def _push_data(self, data):
self.data = data if isinstance(data, la.Session) else la.Session(data)
if self.ipython_kernel is not None:
# Avoid displaying objects we handle in IPython console.
# Sadly, we cannot do this for all objects we support without
# trying to import all the modules we support (which is clearly not
# desirable), because IPython has 3 limitations.
# 1) Its support for "string types" requires
# specifying the exact submodule a type is at (for example:
# pandas.core.frame.DataFrame instead of pandas.DataFrame).
# I do not think this is a maintainable approach for us (that is
# why the registering adapters using "string types" does not
# require that) so we use real/concrete types instead.
# 2) It only supports *exact* types, not subclasses, so we cannot
# just register a custom formatter for "object" and be done
# with it.
# 3) We cannot do this "just in time" by doing it in response
# to either ipython_widget executed or executing signals which
# both happen too late (the value is already displayed by the
# time those signals are fired)
# The combination of the above limitations mean that types
# imported via the console will NOT use the void_formatter :(.
text_formatter = self.ipython_kernel.shell.display_formatter.formatters['text/plain']
unique_types = {type(v) for v in self.data.values()}
for obj_type in unique_types:
adapter_creator = arrayadapter.get_adapter_creator_for_type(obj_type)
if adapter_creator is None:
# if None, it means we do not handle that type at all
# => do not touch its ipython formatter
continue
# Otherwise, we know the type is at least partially handled
# (at least some instances are displayed) so we register our
# void formatter and rely on it to fallback to repr() if
# a particular instance of a type is not handled.
try:
current_formatter = text_formatter.for_type(obj_type)
except KeyError:
current_formatter = None
if current_formatter is not void_formatter:
logger.debug(f"applying void_formatter for {obj_type}")
text_formatter.for_type(obj_type, void_formatter)
self.ipython_kernel.shell.push(dict(self.data.items()))
var_names = [k for k, v in self.data.items() if self._display_in_varlist(k, v)]
self.add_list_items(var_names)
self._listwidget.setCurrentRow(0)
if self.sql_widget is not None:
self.sql_widget.update_completer_options(self.data)
def on_stack_frame_changed(self):
selected = self._stack_frame_widget.selectedItems()
if selected:
assert len(selected) == 1
selected_item = selected[0]
assert isinstance(selected_item, QListWidgetItem)
frame_summary = selected_item.data(Qt.UserRole)
frame_globals, frame_locals = frame_summary.globals, frame_summary.locals
data = {k: frame_globals[k] for k in sorted(frame_globals.keys())}
data.update({k: frame_locals[k] for k in sorted(frame_locals.keys())})
# CHECK:
# * This clears the undo/redo stack, which is safer but is not ideal.
# When inspecting, for all frames except the last one the editor should be readonly (we should allow
# creating new temporary variables but not change existing ones).
# * Does changing the last frame values has any effect after quitting the editor?
# It would be nice if we could do that (possibly with a warning when quitting the debug window)
self._reset()
self._push_data(data)
def _reset(self):
self.data = la.Session()
self._listwidget.clear()
self.current_array = None
self.current_expr_text = None
self.edit_undo_stack.clear()
if self.ipython_kernel is not None:
self.ipython_kernel.shell.reset()
self.ipython_cell_executed()
else:
self.eval_box.setText('None')
self.line_edit_update()
def _setup_file_menu(self, menu_bar):
file_menu = menu_bar.addMenu('&File')
# ============= #
# NEW #
# ============= #
file_menu.addAction(create_action(self, _('&New'),
shortcut="Ctrl+N",
triggered=self.new))
file_menu.addSeparator()
# ============= #
# DATA #
# ============= #
file_menu.addSeparator()
open_tip = _('Load session from file')
file_menu.addAction(create_action(self, _('&Open Data'),
shortcut="Ctrl+O",
triggered=self.open_data,
statustip=open_tip))
save_tip = _('Save all arrays as a session in a file')
file_menu.addAction(create_action(self, _('&Save Data'),
shortcut="Ctrl+S",
triggered=self.save_data,
statustip=save_tip))
file_menu.addAction(create_action(self, _('Save Data &As'),
triggered=self.save_data_as,
statustip=save_tip))
recent_files_menu = file_menu.addMenu("Open &Recent Data")
for action in self.recent_data_files.actions:
recent_files_menu.addAction(action)
recent_files_menu.addSeparator()
recent_files_menu.addAction(create_action(self, _('&Clear List'),
triggered=self.recent_data_files.clear))
# ============= #
# EXAMPLES #
# ============= #
file_menu.addSeparator()
file_menu.addAction(create_action(self, _('&Load Example Dataset'),
triggered=self.load_example))
# ============= #
# EXPLORER #
# ============= #
file_menu.addSeparator()
file_menu.addAction(create_action(self, _('Open File &Explorer'),
triggered=self.open_explorer))
# ============= #
# SCRIPTS #
# ============= #
if qtconsole_available:
file_menu.addSeparator()
file_menu.addAction(create_action(self, _('&Load from Script'),
shortcut="Ctrl+Shift+O",
triggered=self.load_script,
statustip=_('Load script from file')))
file_menu.addAction(create_action(self, _('&Save Command History To Script'),
shortcut="Ctrl+Shift+S",
triggered=self.save_script,
statustip=_('Save command history in a file')))
# ============= #
# QUIT #
# ============= #
file_menu.addSeparator()
file_menu.addAction(create_action(self, _('&Quit'),
shortcut="Ctrl+Q",
triggered=self.close))
def push_changes(self, changes):
self.edit_undo_stack.push(EditSessionArrayCommand(self, self.current_expr_text, changes))
@property
def unsaved_modifications(self):
return self.edit_undo_stack.canUndo() or self._unsaved_modifications
@unsaved_modifications.setter
def unsaved_modifications(self, unsaved_modifications):
self._unsaved_modifications = unsaved_modifications
self.update_title()
def add_list_item(self, name):
listitem = QListWidgetItem(self._listwidget)
listitem.setText(name)
value = self.data[name]
if isinstance(value, la.Array):
listitem.setToolTip(str(value.info))
def add_list_items(self, names):
for name in names:
self.add_list_item(name)
def delete_list_item(self, to_delete):
deleted_items = self._listwidget.findItems(to_delete, Qt.MatchExactly)
# normally there should be exactly one item to delete (and we initially
# tested for len == 1) because there should not be any duplicate named
# items but if, for some reason, we do have some duplicates, it is
# better to delete them all than not delete any
if len(deleted_items) >= 1:
for deleted_item in deleted_items:
item_idx = self._listwidget.row(deleted_item)
self._listwidget.takeItem(item_idx)
def display_item_in_new_window(self, list_item):
assert isinstance(list_item, QListWidgetItem)
varname = str(list_item.text())
value = self.data[varname]
self.new_editor_window(value, title=varname)
@staticmethod
def new_editor_window(data, title: str=None, readonly: bool=False,
cls=EditorWindow):
window = cls(data, title=title, readonly=readonly)
window.show()
# this is necessary so that the window does not disappear immediately
opened_secondary_windows.append(window)
def select_list_item(self, to_display):
changed_items = self._listwidget.findItems(to_display, Qt.MatchExactly)
assert len(changed_items) == 1, \
f"len(changed_items) should be 1 but is {len(changed_items)}:\n{changed_items!r}"
prev_selected = self._listwidget.selectedItems()
assert len(prev_selected) <= 1
# if the currently selected item (value) need to be refreshed (e.g it was modified)
if prev_selected and prev_selected[0] == changed_items[0]:
# we need to update the array widget explicitly
self.set_current_array(self.data[to_display], to_display)
else:
self._listwidget.setCurrentItem(changed_items[0])
def update_mapping_and_varlist(self, value):
# XXX: use ordered set so that the order is non-random if the underlying container is ordered?
keys_before = set(self.data.keys())
keys_after = set(value.keys())
# Contains both new and keys for which the object id changed (but not deleted keys nor inplace modified keys).
# Inplace modified arrays should be already handled in ipython_cell_executed by the setitem_pattern.
changed_keys = [k for k in keys_after if value[k] is not self.data.get(k)]
# when a key is re-assigned, it can switch from being displayable to
# non-displayable or vice versa so computing displayable_keys_before via
# display_in_varlist() is NOT what we need. We need the keys which were
# actually displayed before
displayed_keys_before = {self._listwidget.item(i).text()
for i in range(self._listwidget.count())}
displayable_keys_after = {k for k in keys_after if self._display_in_varlist(k, value[k])}
deleted_displayable_keys = displayed_keys_before - displayable_keys_after
new_displayable_keys = displayable_keys_after - displayed_keys_before
# this can contain more keys than new_displayable_keys (because of existing keys which changed value)
changed_displayable_keys = [k for k in changed_keys if self._display_in_varlist(k, value[k])]
# 1) update session/mapping (whether displayable or not)
# a) deleted old keys
for k in keys_before - keys_after:
del self.data[k]
# b) add new/modify existing keys
for k in changed_keys:
self.data[k] = value[k]
# 2) update list widget
for k in deleted_displayable_keys:
self.delete_list_item(k)
self.add_list_items(new_displayable_keys)
# 3) mark session as dirty if needed
if len(changed_displayable_keys) > 0 or deleted_displayable_keys:
self.unsaved_modifications = True
# 4) update sql completer options if needed
if self.sql_widget is not None and (new_displayable_keys or deleted_displayable_keys):
self.sql_widget.update_completer_options(self.data)
# 5) return variable to display, if any (if there are more than one,
# return first)
return changed_displayable_keys[0] if changed_displayable_keys else None
def delete_current_item(self):
current_item = self._listwidget.currentItem()
name = str(current_item.text())
del self.data[name]
if self.ipython_kernel is not None:
self.ipython_kernel.shell.del_var(name)
self.unsaved_modifications = True
self._listwidget.takeItem(self._listwidget.row(current_item))
def line_edit_update(self):
import larray as la
last_input = self.eval_box.text()
if ASSIGNMENT_PATTERN.match(last_input):
context = self.data._objects.copy()
exec(last_input, la.__dict__, context)
varname = self.update_mapping_and_varlist(context)
if varname is not None:
self.select_list_item(varname)
self.expressions[varname] = last_input
else:
cur_output = eval(last_input, la.__dict__, self.data)
self.view_expr(cur_output, last_input)
def view_expr(self, array, expr_text):
self._listwidget.clearSelection()
self.set_current_array(array, expr_text)
def _display_in_varlist(self, k, v):
return (self._display_in_grid(v) and not k.startswith('__') and
# This is ugly (and larray specific) but I did not find an
# easy way to exclude that specific variable. I do not think
# it should be in larray top level namespace anyway.
k != 'EXAMPLE_EXCEL_TEMPLATES_DIR')
def _display_in_grid(self, v):
return not isinstance(arrayadapter.get_adapter_creator(v), str)
def ipython_cell_executed(self):
user_ns = self.ipython_kernel.shell.user_ns
ip_keys = {'In', 'Out', '_', '__', '___', '__builtin__', '_dh', '_ih', '_oh', '_sh', '_i', '_ii', '_iii',
'exit', 'get_ipython', 'quit'}
# '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__',
clean_ns = {k: v for k, v in user_ns.items()
if k not in ip_keys and not HISTORY_VARS_PATTERN.match(k)}
# user_ns['_i'] is not updated yet (refers to the -2 item)
# 'In' and '_ih' point to the same object (but '_ih' is supposed to be
# the non-overridden one)
cur_input_num = len(user_ns['_ih']) - 1
last_input = user_ns['_ih'][-1]
# In case of multi-line input, only care about the last line. This is
# not perfect as things like:
# arr3[1] = 42
# a = 1
# will not be picked up. But the setitem thing cannot be done perfectly
# anyway (any called function can modify any array), short of hashing
# the content of all variables and checking which ones actually
# changed, which would be too slow when working with large arrays.
# At least this is predictable.