6 changed files with 214 additions and 24 deletions
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
||||
# |
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
# of this software and associated documentation files (the "Software"), to deal |
||||
# in the Software without restriction, including without limitation the rights |
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
# copies of the Software, and to permit persons to whom the Software is |
||||
# furnished to do so, subject to the following conditions: |
||||
# |
||||
# The above copyright notice and this permission notice shall be included in all |
||||
# copies or substantial portions of the Software. |
||||
# |
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
# SOFTWARE. |
||||
|
||||
from TermTk.TTkCore.constant import TTkK |
||||
from TermTk.TTkCore.signal import pyTTkSlot, pyTTkSignal |
||||
from TermTk.TTkWidgets.TTkModelView.tree import TTkTree |
||||
from TermTk.TTkWidgets.TTkModelView.filetreewidget import TTkFileTreeWidget |
||||
|
||||
class TTkFileTree(TTkTree): |
||||
__slots__ = ('_fileTreeWidget', |
||||
# Forwarded Methods |
||||
'openPath', |
||||
# Forwarded Signals |
||||
'fileClicked', 'folderClicked', 'fileDoubleClicked', 'folderDoubleClicked') |
||||
|
||||
def __init__(self, *args, **kwargs): |
||||
wkwargs = kwargs.copy() |
||||
if 'parent' in wkwargs: wkwargs.pop('parent') |
||||
self._fileTreeWidget = TTkFileTreeWidget(*args, **wkwargs) |
||||
|
||||
TTkTree.__init__(self, *args, **kwargs, treeWidget=self._fileTreeWidget) |
||||
self._name = kwargs.get('name' , 'TTkFileTree' ) |
||||
|
||||
# Forward Signals |
||||
self.fileClicked = self._fileTreeWidget.fileClicked |
||||
self.folderClicked = self._fileTreeWidget.folderClicked |
||||
self.fileDoubleClicked = self._fileTreeWidget.fileDoubleClicked |
||||
self.folderDoubleClicked = self._fileTreeWidget.folderDoubleClicked |
||||
|
||||
# Forward Methods |
||||
self.openPath = self._fileTreeWidget.openPath |
||||
@ -0,0 +1,140 @@
|
||||
#!/usr/bin/env python3 |
||||
|
||||
# MIT License |
||||
# |
||||
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
||||
# |
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy |
||||
# of this software and associated documentation files (the "Software"), to deal |
||||
# in the Software without restriction, including without limitation the rights |
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||
# copies of the Software, and to permit persons to whom the Software is |
||||
# furnished to do so, subject to the following conditions: |
||||
# |
||||
# The above copyright notice and this permission notice shall be included in all |
||||
# copies or substantial portions of the Software. |
||||
# |
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||
# SOFTWARE. |
||||
|
||||
import os |
||||
import re |
||||
import datetime |
||||
|
||||
from TermTk.TTkCore.color import TTkColor |
||||
|
||||
from TermTk.TTkCore.constant import TTkK |
||||
from TermTk.TTkCore.log import TTkLog |
||||
from TermTk.TTkCore.cfg import TTkCfg |
||||
from TermTk.TTkCore.string import TTkString |
||||
from TermTk.TTkWidgets.TTkModelView.treewidget import TTkTreeWidget |
||||
from TermTk.TTkWidgets.TTkModelView.filetreewidgetitem import TTkFileTreeWidgetItem |
||||
from TermTk.TTkCore.signal import pyTTkSlot, pyTTkSignal |
||||
|
||||
class TTkFileTreeWidget(TTkTreeWidget): |
||||
__slots__ = ('_path', '_filter', |
||||
# Signals |
||||
'fileClicked', 'folderClicked', 'fileDoubleClicked', 'folderDoubleClicked') |
||||
def __init__(self, *args, **kwargs): |
||||
# Signals |
||||
self.fileClicked = pyTTkSignal(TTkFileTreeWidgetItem) |
||||
self.folderClicked = pyTTkSignal(TTkFileTreeWidgetItem) |
||||
self.fileDoubleClicked = pyTTkSignal(TTkFileTreeWidgetItem) |
||||
self.folderDoubleClicked = pyTTkSignal(TTkFileTreeWidgetItem) |
||||
TTkTreeWidget.__init__(self, *args, **kwargs) |
||||
self._name = kwargs.get('name' , 'TTkFileTreeWidget' ) |
||||
self._path = kwargs.get('path','.') |
||||
self._filter = '*' |
||||
|
||||
def setFilter(self, filter): |
||||
self._filter = filter |
||||
# TODO: Avoid to refer directly '_rootItem' |
||||
TTkFileTreeWidgetItem.setFilter(self._rootItem, filter) |
||||
|
||||
def openPath(self, path): |
||||
self._path = path |
||||
|
||||
self.clear() |
||||
for i in TTkFileTreeWidget._getFileItems(path): |
||||
self.addTopLevelItem(i) |
||||
self.setFilter(self._filter) |
||||
|
||||
def _getFileItems(path): |
||||
path = os.path.abspath(path) |
||||
if not os.path.exists(path): return [] |
||||
dir_list = os.listdir(path) |
||||
ret = [] |
||||
for n in dir_list: |
||||
nodePath = os.path.join(path,n) |
||||
|
||||
def _getStat(_path): |
||||
info = os.stat(_path) |
||||
time = datetime.datetime.fromtimestamp(info.st_ctime).strftime('%Y-%m-%d %H:%M:%S') |
||||
if info.st_size > (1024*1024*1024): |
||||
size = f"{info.st_size/(1024*1024*1024):.2f} GB" |
||||
if info.st_size > (1024*1024): |
||||
size = f"{info.st_size/(1024*1024):.2f} MB" |
||||
elif info.st_size > 1024: |
||||
size = f"{info.st_size/1024:.2f} KB" |
||||
else: |
||||
size = f"{info.st_size} bytes" |
||||
return time, size, info.st_ctime, info.st_size |
||||
|
||||
if os.path.isdir(nodePath): |
||||
if os.path.exists(nodePath): |
||||
time, _, rawTime, _ = _getStat(nodePath) |
||||
color = TTkCfg.theme.folderNameColor |
||||
else: |
||||
time, _, rawTime, _ = "" |
||||
color = TTkCfg.theme.failNameColor |
||||
|
||||
if os.path.islink(nodePath): |
||||
name = TTkString()+TTkCfg.theme.linkNameColor+n+'/'+TTkColor.RST+' -> '+TTkCfg.theme.folderNameColor+os.readlink(nodePath) |
||||
typef = "Folder Link" |
||||
else: |
||||
name = TTkString()+color+n+'/' |
||||
typef = "Folder" |
||||
|
||||
ret.append(TTkFileTreeWidgetItem( |
||||
[ name, "", typef, time], |
||||
raw = [ n , -1 , typef , rawTime ], |
||||
path=nodePath, |
||||
type=TTkFileTreeWidgetItem.DIR, |
||||
icon=TTkString() + TTkCfg.theme.folderIconColor + TTkCfg.theme.fileIcon.folderClose + TTkColor.RST, |
||||
childIndicatorPolicy=TTkK.ShowIndicator)) |
||||
|
||||
elif os.path.isfile(nodePath) or os.path.islink(nodePath): |
||||
if os.path.exists(nodePath): |
||||
time, size, rawTime, rawSize = _getStat(nodePath) |
||||
if os.access(nodePath, os.X_OK): |
||||
color = TTkCfg.theme.executableColor |
||||
typef="Exec" |
||||
else: |
||||
color = TTkCfg.theme.fileNameColor |
||||
typef="File" |
||||
else: |
||||
time, size, rawTime, rawSize = "", "", 0, 0 |
||||
color = TTkCfg.theme.failNameColor |
||||
typef="Broken" |
||||
|
||||
if os.path.islink(nodePath): |
||||
name = TTkString()+TTkCfg.theme.linkNameColor+n+TTkColor.RST+' -> '+color+os.readlink(nodePath) |
||||
typef += " Link" |
||||
else: |
||||
name = TTkString()+color+n |
||||
|
||||
_, ext = os.path.splitext(n) |
||||
if ext: ext = f"{ext[1:]} " |
||||
ret.append(TTkFileTreeWidgetItem( |
||||
[ name, size, typef, time], |
||||
raw = [ n , rawSize , typef , rawTime ], |
||||
path=nodePath, |
||||
type=TTkFileTreeWidgetItem.FILE, |
||||
icon=TTkString() + TTkCfg.theme.fileIconColor + TTkCfg.theme.fileIcon.getIcon(n) + TTkColor.RST, |
||||
childIndicatorPolicy=TTkK.DontShowIndicator)) |
||||
return ret |
||||
Loading…
Reference in new issue