Skip to content

Commit f345753

Browse files
committed
* fixed opening files via file->open. It crashed in update_title because it was called a first time with array=None, and the dtype = array.dtype.name line crashed.
* opening .csv files in the editor will create variables with only the filename without extension (instead of the variables named by the full path). Closes #90. * moved unsaved marker just before filename (this is more standard) * .csv files will display as "dir\fname.csv - axes_info" instead of dir\fname.csv - fname * other files will display as "fname.ext - array_name: axes_info" instead of using the full path * when there is no current file, use <new> as the filename
1 parent 8dc0f3a commit f345753

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

larray_editor/editor.py

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
from larray import LArray, Session, zeros
7-
from larray_editor.utils import PYQT5, _, create_action, show_figure, ima
7+
from larray_editor.utils import PYQT5, _, create_action, show_figure, ima, commonpath
88
from larray_editor.arraywidget import ArrayEditorWidget
99

1010
from qtpy.QtCore import Qt, QSettings, QUrl, Slot
@@ -465,26 +465,38 @@ def on_item_changed(self, curr, prev):
465465
def update_title(self):
466466
array = self.current_array
467467
name = self.current_array_name if self.current_array_name is not None else ''
468-
dtype = array.dtype.name
469-
unsave_marker = '*' if self._is_unsaved_modifications() else ''
470-
title = []
471-
if isinstance(array, LArray):
472-
# current file (if not None)
473-
if self.current_file is not None:
474-
if os.path.isdir(self.current_file):
475-
title = ['{}/{}.csv'.format(self.current_file, name)]
476-
else:
477-
title = [self.current_file]
478-
# array info
479-
shape = ['{} ({})'.format(display_name, len(axis))
480-
for display_name, axis in zip(array.axes.display_names, array.axes)]
468+
469+
unsaved_marker = '*' if self._is_unsaved_modifications() else ''
470+
if self.current_file is not None:
471+
basename = os.path.basename(self.current_file)
472+
if os.path.isdir(self.current_file):
473+
assert not name.endswith('.csv')
474+
fname = os.path.join(basename, '{}.csv'.format(name))
475+
name = ''
476+
else:
477+
fname = basename
481478
else:
482-
# if it's not an LArray, it must be a Numpy ndarray
483-
assert isinstance(array, np.ndarray)
484-
shape = [str(length) for length in array.shape]
485-
# name + shape + dtype
486-
array_info = ' x '.join(shape) + ' [{}]'.format(dtype)
487-
title += [unsave_marker + name + ': ' + array_info]
479+
fname = '<new>'
480+
title = ['{}{}'.format(unsaved_marker, fname)]
481+
482+
if array is not None:
483+
dtype = array.dtype.name
484+
# current file (if not None)
485+
if isinstance(array, LArray):
486+
# array info
487+
shape = ['{} ({})'.format(display_name, len(axis))
488+
for display_name, axis in zip(array.axes.display_names, array.axes)]
489+
else:
490+
# if it's not an LArray, it must be a Numpy ndarray
491+
assert isinstance(array, np.ndarray)
492+
shape = [str(length) for length in array.shape]
493+
# name + shape + dtype
494+
array_info = ' x '.join(shape) + ' [{}]'.format(dtype)
495+
if name:
496+
title += [name + ': ' + array_info]
497+
else:
498+
title += [array_info]
499+
488500
# extra info
489501
title += [self._title]
490502
# set title
@@ -540,23 +552,30 @@ def new(self):
540552

541553
def _open_file(self, filepath):
542554
session = Session()
543-
if '.csv' in filepath:
544-
filepath = [filepath]
555+
# a list => .csv files. Possibly a single .csv file.
545556
if isinstance(filepath, (list, tuple)):
546-
current_file_name = os.path.dirname(filepath[0])
547-
display_name = ','.join(os.path.basename(fpath) for fpath in filepath)
548-
names = filepath
549-
filepath = None
557+
fpaths = filepath
558+
if len(fpaths) == 1:
559+
common_fpath = os.path.dirname(fpaths[0])
560+
else:
561+
common_fpath = commonpath(fpaths)
562+
basenames = [os.path.basename(fpath) for fpath in fpaths]
563+
fnames = [os.path.relpath(fpath, common_fpath) for fpath in fpaths]
564+
565+
names = [os.path.splitext(fname)[0] for fname in fnames]
566+
current_file_name = common_fpath
567+
display_name = ','.join(basenames)
568+
filepath = common_fpath
550569
else:
551570
names = None
552571
current_file_name = filepath
553572
display_name = os.path.basename(filepath)
554573
try:
555574
session.load(filepath, names)
556575
self._reset()
557-
self.set_current_file(current_file_name)
558576
self._add_arrays(session)
559577
self._listwidget.setCurrentRow(0)
578+
self.set_current_file(current_file_name)
560579
self.unsaved_modifications = False
561580
self.statusBar().showMessage("Loaded: {}".format(display_name), 4000)
562581
except Exception as e:
@@ -571,6 +590,8 @@ def open(self):
571590
filepaths = res[0] if PYQT5 else res
572591
if len(filepaths) >= 1:
573592
if all(['.csv' in filepath for filepath in filepaths]):
593+
# this means that even a single .csv file will be passed as a list (so that we can add arrays
594+
# and save them as a directory).
574595
self._open_file(filepaths)
575596
elif len(filepaths) == 1:
576597
self._open_file(filepaths[0])

larray_editor/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
PY2 = sys.version[0] == '2'
2121

22+
if PY2:
23+
def commonpath(paths):
24+
return os.path.dirname(os.path.commonprefix(paths))
25+
else:
26+
commonpath = os.path.commonpath
27+
28+
2229
# Note: string and unicode data types will be formatted with '%s' (see below)
2330
SUPPORTED_FORMATS = {
2431
'object': '%s',

0 commit comments

Comments
 (0)