4
4
import numpy as np
5
5
6
6
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
8
8
from larray_editor .arraywidget import ArrayEditorWidget
9
9
10
10
from qtpy .QtCore import Qt , QSettings , QUrl , Slot
@@ -465,26 +465,38 @@ def on_item_changed(self, curr, prev):
465
465
def update_title (self ):
466
466
array = self .current_array
467
467
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
481
478
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
+
488
500
# extra info
489
501
title += [self ._title ]
490
502
# set title
@@ -540,23 +552,30 @@ def new(self):
540
552
541
553
def _open_file (self , filepath ):
542
554
session = Session ()
543
- if '.csv' in filepath :
544
- filepath = [filepath ]
555
+ # a list => .csv files. Possibly a single .csv file.
545
556
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
550
569
else :
551
570
names = None
552
571
current_file_name = filepath
553
572
display_name = os .path .basename (filepath )
554
573
try :
555
574
session .load (filepath , names )
556
575
self ._reset ()
557
- self .set_current_file (current_file_name )
558
576
self ._add_arrays (session )
559
577
self ._listwidget .setCurrentRow (0 )
578
+ self .set_current_file (current_file_name )
560
579
self .unsaved_modifications = False
561
580
self .statusBar ().showMessage ("Loaded: {}" .format (display_name ), 4000 )
562
581
except Exception as e :
@@ -571,6 +590,8 @@ def open(self):
571
590
filepaths = res [0 ] if PYQT5 else res
572
591
if len (filepaths ) >= 1 :
573
592
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).
574
595
self ._open_file (filepaths )
575
596
elif len (filepaths ) == 1 :
576
597
self ._open_file (filepaths [0 ])
0 commit comments