WebFM/src/core/utils/shellfm/windows/view/View.py

200 lines
5.7 KiB
Python
Raw Normal View History

2021-02-06 04:52:46 +00:00
# Python imports
2022-03-18 22:18:13 +00:00
import hashlib, os, re
2021-02-06 04:52:46 +00:00
from os import listdir
2021-02-06 07:38:15 +00:00
from os.path import isdir, isfile, join
2021-02-06 04:52:46 +00:00
# Lib imports
2021-02-06 07:38:15 +00:00
2021-02-06 04:52:46 +00:00
# Application imports
2021-09-04 04:51:17 +00:00
from .utils import Settings, Launcher
from . import Path
2021-02-06 04:52:46 +00:00
class View(Settings, Launcher, Path):
2021-02-06 04:52:46 +00:00
def __init__(self):
self.files = []
2021-02-07 22:52:08 +00:00
self.dirs = []
2021-02-06 04:52:46 +00:00
self.vids = []
self.images = []
self.desktop = []
self.ungrouped = []
2021-07-27 22:46:56 +00:00
self.error_message = None
2021-02-08 00:44:35 +00:00
self.set_to_home()
2021-02-06 04:52:46 +00:00
def load_directory(self):
path = self.get_path()
2021-02-07 22:52:08 +00:00
self.dirs = []
2021-02-06 04:52:46 +00:00
self.vids = []
self.images = []
self.desktop = []
self.ungrouped = []
self.files = []
if not isdir(path):
2021-07-27 22:46:56 +00:00
self._set_error_message("Path can not be accessed.")
2021-02-06 04:52:46 +00:00
return ""
for f in listdir(path):
file = join(path, f)
2021-02-08 00:44:35 +00:00
if self.HIDE_HIDDEN_FILES:
2021-02-06 04:52:46 +00:00
if f.startswith('.'):
continue
if isfile(file):
lowerName = file.lower()
if lowerName.endswith(self.fvideos):
self.vids.append(f)
elif lowerName.endswith(self.fimages):
self.images.append(f)
elif lowerName.endswith((".desktop",)):
self.desktop.append(f)
else:
self.ungrouped.append(f)
else:
self.dirs.append(f)
2022-03-18 22:18:13 +00:00
self.dirs.sort(key=self._natural_keys)
self.vids.sort(key=self._natural_keys)
self.images.sort(key=self._natural_keys)
self.desktop.sort(key=self._natural_keys)
self.ungrouped.sort(key=self._natural_keys)
2021-02-06 04:52:46 +00:00
self.files = self.dirs + self.vids + self.images + self.desktop + self.ungrouped
2021-02-06 07:38:15 +00:00
def hashText(self, text):
return hashlib.sha256(str.encode(text)).hexdigest()[:18]
def hashSet(self, arry):
2022-03-18 22:18:13 +00:00
path = self.get_path()
2021-02-06 07:38:15 +00:00
data = []
for arr in arry:
2022-03-18 22:18:13 +00:00
file = f"{path}/{arr}"
size = "4K" if isdir(file) else self.sizeof_fmt(os.path.getsize(file))
data.append([arr, self.hashText(arr), size])
2021-02-06 07:38:15 +00:00
return data
2021-02-06 04:52:46 +00:00
2021-02-08 00:44:35 +00:00
def get_path_part_from_hash(self, hash):
2021-02-06 08:34:44 +00:00
files = self.get_files()
file = None
for f in files:
if hash == f[1]:
file = f[0]
break
return file
2021-02-06 08:34:44 +00:00
def get_files_formatted(self):
2021-02-07 22:52:08 +00:00
files = self.hashSet(self.files),
dirs = self.hashSet(self.dirs),
videos = self.get_videos(),
2021-02-07 22:52:08 +00:00
images = self.hashSet(self.images),
desktops = self.hashSet(self.desktop),
ungrouped = self.hashSet(self.ungrouped)
return {
2021-02-07 22:52:08 +00:00
'path_head': self.get_path(),
'list': {
'files': files,
'dirs': dirs,
'videos': videos,
'images': images,
'desktops': desktops,
'ungrouped': ungrouped
}
}
2021-02-08 00:44:35 +00:00
def is_folder_locked(self, hash):
if self.lock_folder:
path_parts = self.get_path().split('/')
file = self.get_path_part_from_hash(hash)
# Insure chilren folders are locked too.
lockedFolderInPath = False
for folder in self.locked_folders:
if folder in path_parts:
lockedFolderInPath = True
break
return (file in self.locked_folders or lockedFolderInPath)
else:
return False
2021-07-27 22:46:56 +00:00
def _set_error_message(self, text):
self.error_message = text
def unset_error_message(self):
self.error_message = None
def get_error_message(self):
return self.error_message
2021-02-07 22:52:08 +00:00
def get_current_directory(self):
return self.get_path()
2021-02-10 04:15:32 +00:00
def get_current_sub_path(self):
path = self.get_path()
home = self.get_home() + "/"
return path.replace(home, "")
2021-02-07 22:52:08 +00:00
def get_dot_dots(self):
return self.hashSet(['.', '..'])
2021-02-06 04:52:46 +00:00
def get_files(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.files)
2021-02-06 04:52:46 +00:00
def get_dirs(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.dirs)
2021-02-06 04:52:46 +00:00
def get_videos(self):
videos_set = self.hashSet(self.vids)
current_directory = self.get_current_directory()
for video in videos_set:
hashImgPth = join(self.ABS_THUMBS_PTH, video[1]) + ".jpg"
if not os.path.exists(hashImgPth) :
fullPath = join(current_directory, video[0])
self.logger.debug(f"Hash Path: {hashImgPth}\nFile Path: {fullPath}")
self.generateVideoThumbnail(fullPath, hashImgPth)
return videos_set
2021-02-06 04:52:46 +00:00
def get_images(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.images)
2021-02-06 04:52:46 +00:00
def get_desktops(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.desktop)
2021-02-06 04:52:46 +00:00
def get_ungrouped(self):
2021-02-06 07:38:15 +00:00
return self.hashSet(self.ungrouped)
2022-03-18 22:18:13 +00:00
def sizeof_fmt(self, num, suffix="B"):
for unit in ["", "K", "M", "G", "T", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
return f"{num:3.1f} {unit}{suffix}"
num /= 1024.0
return f"{num:.1f} Yi{suffix}"
def get_dir_size(self, sdir):
"""Get the size of a directory. Based on code found online."""
size = os.path.getsize(sdir)
for item in listdir(sdir):
item = join(sdir, item)
if isfile(item):
size = size + os.path.getsize(item)
elif isdir(item):
size = size + self.get_dir_size(item)
return size
def _atoi(self, text):
return int(text) if text.isdigit() else text
def _natural_keys(self, text):
return [ self._atoi(c) for c in re.split('(\d+)',text) ]