diff -r fb0ef164f536 -r 698ae46f40a4 eric6/UI/BrowserModel.py --- a/eric6/UI/BrowserModel.py Fri Apr 02 11:59:41 2021 +0200 +++ b/eric6/UI/BrowserModel.py Sat May 01 14:27:20 2021 +0200 @@ -10,6 +10,7 @@ import os import fnmatch import json +import contextlib from PyQt5.QtCore import ( QDir, QModelIndex, QAbstractItemModel, QFileSystemWatcher, Qt, QProcess, @@ -47,7 +48,7 @@ @param nopopulate flag indicating to not populate the model (boolean) """ - super(BrowserModel, self).__init__(parent) + super().__init__(parent) self.progDir = None @@ -76,10 +77,8 @@ if parent is None: parent = QModelIndex() - if parent.isValid(): - item = parent.internalPointer() - else: - item = self.rootItem + item = (parent.internalPointer() if parent.isValid() + else self.rootItem) return item.columnCount() + 1 @@ -173,10 +172,11 @@ ): return QModelIndex() - if not parent.isValid(): - parentItem = self.rootItem - else: - parentItem = parent.internalPointer() + parentItem = ( + parent.internalPointer() + if parent.isValid() else + self.rootItem + ) try: if not parentItem.isPopulated(): @@ -347,15 +347,16 @@ cnt = itm.childCount() self.beginInsertRows( self.createIndex(itm.row(), 0, itm), cnt, cnt) - if f.isDir(): - node = BrowserDirectoryItem( + node = ( + BrowserDirectoryItem( itm, Utilities.toNativeSeparators(f.absoluteFilePath()), False) - else: - node = BrowserFileItem( + if f.isDir() else + BrowserFileItem( itm, Utilities.toNativeSeparators(f.absoluteFilePath())) + ) self._addItem(node, itm) self.endInsertRows() @@ -511,10 +512,11 @@ if parent is None: parent = QModelIndex() - if not parent.isValid(): - parentItem = self.rootItem - else: - parentItem = parent.internalPointer() + parentItem = ( + parent.internalPointer() + if parent.isValid() else + self.rootItem + ) cnt = parentItem.childCount() self.beginInsertRows(parent, cnt, cnt) @@ -605,10 +607,11 @@ self.createIndex(parentItem.row(), 0, parentItem), 0, len(syspath) - 1) for p in syspath: - if os.path.isdir(p): - node = BrowserDirectoryItem(parentItem, p) - else: - node = BrowserFileItem(parentItem, p) + node = ( + BrowserDirectoryItem(parentItem, p) + if os.path.isdir(p) else + BrowserFileItem(parentItem, p) + ) self._addItem(node, parentItem) if repopulate: self.endInsertRows() @@ -650,12 +653,10 @@ # special treatment done later continue cl = dictionary[key] - try: + with contextlib.suppress(AttributeError): if cl.module == moduleName: node = BrowserClassItem(parentItem, cl, fileName) self._addItem(node, parentItem) - except AttributeError: - pass if "@@Coding@@" in keys and Preferences.getUI("BrowserShowCoding"): node = BrowserCodingItem( parentItem, @@ -860,7 +861,7 @@ self.endInsertRows() -class BrowserItem(object): +class BrowserItem: """ Class implementing the data structure for browser items. """ @@ -1044,12 +1045,7 @@ @param full flag indicating full pathname should be displayed (boolean) """ self._dirName = os.path.abspath(dinfo) - - if full: - dn = self._dirName - else: - dn = os.path.basename(self._dirName) - + dn = self._dirName if full else os.path.basename(self._dirName) BrowserItem.__init__(self, parent, dn) self.type_ = BrowserItemDirectory @@ -1073,11 +1069,7 @@ @param full flag indicating full pathname should be displayed (boolean) """ self._dirName = os.path.abspath(dinfo) - - if full: - dn = self._dirName - else: - dn = os.path.basename(self._dirName) + dn = self._dirName if full else os.path.basename(self._dirName) self.itemData[0] = dn def dirName(self): @@ -1105,9 +1097,11 @@ @param order sort order (Qt.SortOrder) (for special sorting) @return true, if this item is less than other (boolean) """ - if issubclass(other.__class__, BrowserFileItem): - if Preferences.getUI("BrowsersListFoldersFirst"): - return order == Qt.SortOrder.AscendingOrder + if ( + issubclass(other.__class__, BrowserFileItem) and + Preferences.getUI("BrowsersListFoldersFirst") + ): + return order == Qt.SortOrder.AscendingOrder return BrowserItem.lessThan(self, other, column, order) @@ -1161,6 +1155,7 @@ self._moduleName = '' + # TODO: add icon for eric graphics file pixName = "" if self.isPython3File(): pixName = "filePython" @@ -1306,7 +1301,8 @@ """ Public method to check, if this file is a Ruby script. - @return flag indicating a Ruby file (boolean) + @return flag indicating a Ruby file + @rtype bool """ return ( self.fileext == '.rb' or @@ -1317,7 +1313,8 @@ """ Public method to check, if this file is a Qt-Designer file. - @return flag indicating a Qt-Designer file (boolean) + @return flag indicating a Qt-Designer file + @rtype bool """ return self.fileext == '.ui' @@ -1325,7 +1322,8 @@ """ Public method to check, if this file is a Qt-Linguist file. - @return flag indicating a Qt-Linguist file (boolean) + @return flag indicating a Qt-Linguist file + @rtype bool """ return self.fileext in ['.ts', '.qm'] @@ -1333,7 +1331,8 @@ """ Public method to check, if this file is a Qt-Resources file. - @return flag indicating a Qt-Resources file (boolean) + @return flag indicating a Qt-Resources file + @rtype bool """ return self.fileext == '.qrc' @@ -1341,7 +1340,8 @@ """ Public method to check, if this file is an eric project file. - @return flag indicating an eric project file (boolean) + @return flag indicating an eric project file + @rtype bool """ return self.fileext in ('.epj', '.e4p') @@ -1349,7 +1349,8 @@ """ Public method to check, if this file is an eric multi project file. - @return flag indicating an eric project file (boolean) + @return flag indicating an eric project file + @rtype bool """ return self.fileext in ('.emj', '.e4m', '.e5m') @@ -1357,7 +1358,8 @@ """ Public method to check, if this file is a CORBA IDL file. - @return flag indicating a CORBA IDL file (boolean) + @return flag indicating a CORBA IDL file + @rtype bool """ return self.fileext == '.idl' @@ -1374,7 +1376,8 @@ """ Public method to check, if this file is a JavaScript file. - @return flag indicating a JavaScript file (boolean) + @return flag indicating a JavaScript file + @rtype bool """ return self.fileext == '.js' @@ -1382,7 +1385,8 @@ """ Public method to check, if this file is a pixmap file. - @return flag indicating a pixmap file (boolean) + @return flag indicating a pixmap file + @rtype bool """ return self.fileext[1:] in QImageReader.supportedImageFormats() @@ -1390,7 +1394,8 @@ """ Public method to check, if this file is a SVG file. - @return flag indicating a SVG file (boolean) + @return flag indicating a SVG file + @rtype bool """ return self.fileext == '.svg' @@ -1405,6 +1410,15 @@ (self.fileext == "" and self.sourceLanguage == "D") ) + def isEricGraphicsFile(self): + """ + Public method to check, if this is an eric graphics file. + + @return flag indicating an eric graphics file + @rtype bool + """ + return self.fileext in ('.egj', '.e5g') + def lessThan(self, other, column, order): """ Public method to check, if the item is less than the other one. @@ -1414,9 +1428,11 @@ @param order sort order (Qt.SortOrder) (for special sorting) @return true, if this item is less than other (boolean) """ - if not issubclass(other.__class__, BrowserFileItem): - if Preferences.getUI("BrowsersListFoldersFirst"): - return order == Qt.SortOrder.DescendingOrder + if ( + not issubclass(other.__class__, BrowserFileItem) and + Preferences.getUI("BrowsersListFoldersFirst") + ): + return order == Qt.SortOrder.DescendingOrder if issubclass(other.__class__, BrowserFileItem): sinit = os.path.basename(self._filename).startswith('__init__.py') @@ -1453,7 +1469,7 @@ except AttributeError: sname = sup supers.append(sname) - name = name + "({0})".format(", ".join(supers)) + name += "({0})".format(", ".join(supers)) BrowserItem.__init__(self, parent, name)