Added file handler logic; switched to using GdkPixBuff
This commit is contained in:
parent
48aaa7d486
commit
5595122a6d
|
@ -1,10 +1,9 @@
|
||||||
# Python imports
|
# Python imports
|
||||||
import json
|
import threading, subprocess, time, json
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
# Lib imports
|
# Lib imports
|
||||||
|
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from . import Window
|
from . import Window
|
||||||
|
|
||||||
|
|
|
@ -29,15 +29,28 @@ class Path:
|
||||||
|
|
||||||
self.load_directory()
|
self.load_directory()
|
||||||
|
|
||||||
|
|
||||||
def set_path(self, path):
|
def set_path(self, path):
|
||||||
|
if path == self.get_path():
|
||||||
|
return
|
||||||
|
|
||||||
|
if os.path.isdir(path):
|
||||||
self.path = list( filter(None, path.replace("\\", "/").split('/')) )
|
self.path = list( filter(None, path.replace("\\", "/").split('/')) )
|
||||||
self.load_directory()
|
self.load_directory()
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def set_path_with_sub_path(self, sub_path):
|
def set_path_with_sub_path(self, sub_path):
|
||||||
path = os.path.join(self.get_home(), sub_path)
|
path = os.path.join(self.get_home(), sub_path)
|
||||||
|
if path == self.get_path():
|
||||||
|
return False
|
||||||
|
|
||||||
|
if os.path.isdir(path):
|
||||||
self.path = list( filter(None, path.replace("\\", "/").split('/')) )
|
self.path = list( filter(None, path.replace("\\", "/").split('/')) )
|
||||||
self.load_directory()
|
self.load_directory()
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def set_to_home(self):
|
def set_to_home(self):
|
||||||
home = os.path.expanduser("~") + self.subpath
|
home = os.path.expanduser("~") + self.subpath
|
||||||
|
|
|
@ -11,17 +11,20 @@ from random import randint
|
||||||
|
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from .utils import Settings, Launcher
|
from .utils import Settings, Launcher, FileHandler
|
||||||
from .icons import Icon
|
from .icons import Icon
|
||||||
from . import Path
|
from . import Path
|
||||||
|
|
||||||
|
|
||||||
class View(Settings, Launcher, Icon, Path):
|
class View(Settings, FileHandler, Launcher, Icon, Path):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self. logger = None
|
self. logger = None
|
||||||
self.id_length = 10
|
self.id_length = 10
|
||||||
|
|
||||||
self.id = ""
|
self.id = ""
|
||||||
|
self.wid = None
|
||||||
|
self.dir_watcher = None
|
||||||
|
self.hide_hidden = self.HIDE_HIDDEN_FILES
|
||||||
self.files = []
|
self.files = []
|
||||||
self.dirs = []
|
self.dirs = []
|
||||||
self.vids = []
|
self.vids = []
|
||||||
|
@ -44,6 +47,18 @@ class View(Settings, Launcher, Icon, Path):
|
||||||
def get_tab_id(self):
|
def get_tab_id(self):
|
||||||
return self.id
|
return self.id
|
||||||
|
|
||||||
|
def set_wid(self, _wid):
|
||||||
|
self.wid = _wid
|
||||||
|
|
||||||
|
def get_wid(self):
|
||||||
|
return self.wid
|
||||||
|
|
||||||
|
def set_dir_watcher(self, watcher):
|
||||||
|
self.dir_watcher = watcher
|
||||||
|
|
||||||
|
def get_dir_watcher(self):
|
||||||
|
return self.dir_watcher
|
||||||
|
|
||||||
def load_directory(self):
|
def load_directory(self):
|
||||||
path = self.get_path()
|
path = self.get_path()
|
||||||
self.dirs = [".", ".."]
|
self.dirs = [".", ".."]
|
||||||
|
@ -59,7 +74,7 @@ class View(Settings, Launcher, Icon, Path):
|
||||||
|
|
||||||
for f in listdir(path):
|
for f in listdir(path):
|
||||||
file = join(path, f)
|
file = join(path, f)
|
||||||
if self.HIDE_HIDDEN_FILES:
|
if self.hide_hidden:
|
||||||
if f.startswith('.'):
|
if f.startswith('.'):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
|
@ -3,16 +3,12 @@ import os, subprocess, threading, hashlib
|
||||||
from os.path import isfile
|
from os.path import isfile
|
||||||
|
|
||||||
# Gtk imports
|
# Gtk imports
|
||||||
import gi
|
from gi.repository import GdkPixbuf
|
||||||
gi.require_version('Gtk', '3.0')
|
|
||||||
|
|
||||||
from gi.repository import Gtk
|
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from .mixins import *
|
from .mixins import *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def threaded(fn):
|
def threaded(fn):
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
||||||
|
@ -34,17 +30,10 @@ class Icon(DesktopIconMixin, VideoIconMixin):
|
||||||
thumbnl = self.create_scaled_image(full_path, self.VIDEO_ICON_WH)
|
thumbnl = self.create_scaled_image(full_path, self.VIDEO_ICON_WH)
|
||||||
elif full_path.lower().endswith( ('.desktop',) ): # .desktop file parsing
|
elif full_path.lower().endswith( ('.desktop',) ): # .desktop file parsing
|
||||||
thumbnl = self.parse_desktop_files(full_path)
|
thumbnl = self.parse_desktop_files(full_path)
|
||||||
else: # System icons
|
|
||||||
thumbnl = self.get_system_thumbnail(full_path, self.SYS_ICON_WH[0])
|
|
||||||
|
|
||||||
if thumbnl == None: # If no icon whatsoever, return internal default
|
|
||||||
thumbnl = Gtk.Image.new_from_file(self.DEFAULT_ICON)
|
|
||||||
|
|
||||||
return thumbnl
|
return thumbnl
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Icon generation issue:")
|
return None
|
||||||
print( repr(e) )
|
|
||||||
return Gtk.Image.new_from_file(self.DEFAULT_ICON)
|
|
||||||
|
|
||||||
def create_thumbnail(self, dir, file):
|
def create_thumbnail(self, dir, file):
|
||||||
full_path = dir + "/" + file
|
full_path = dir + "/" + file
|
||||||
|
@ -56,20 +45,20 @@ class Icon(DesktopIconMixin, VideoIconMixin):
|
||||||
|
|
||||||
thumbnl = self.create_scaled_image(hash_img_pth, self.VIDEO_ICON_WH)
|
thumbnl = self.create_scaled_image(hash_img_pth, self.VIDEO_ICON_WH)
|
||||||
if thumbnl == None: # If no icon whatsoever, return internal default
|
if thumbnl == None: # If no icon whatsoever, return internal default
|
||||||
thumbnl = Gtk.Image.new_from_file(self.DEFAULT_ICONS + "/video.png")
|
thumbnl = GdkPixbuf.Pixbuf.new_from_file(self.DEFAULT_ICONS + "/video.png")
|
||||||
|
|
||||||
return thumbnl
|
return thumbnl
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Thumbnail generation issue:")
|
print("Thumbnail generation issue:")
|
||||||
print( repr(e) )
|
print( repr(e) )
|
||||||
return Gtk.Image.new_from_file(self.DEFAULT_ICONS + "/video.png")
|
return GdkPixbuf.Pixbuf.new_from_file(self.DEFAULT_ICONS + "/video.png")
|
||||||
|
|
||||||
|
|
||||||
def create_scaled_image(self, path, wxh):
|
def create_scaled_image(self, path, wxh):
|
||||||
try:
|
try:
|
||||||
pixbuf = Gtk.Image.new_from_file(path).get_pixbuf()
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
|
||||||
scaled_pixbuf = pixbuf.scale_simple(wxh[0], wxh[1], 2) # 2 = BILINEAR and is best by default
|
scaled_pixbuf = pixbuf.scale_simple(wxh[0], wxh[1], 2) # 2 = BILINEAR and is best by default
|
||||||
return Gtk.Image.new_from_pixbuf(scaled_pixbuf)
|
return scaled_pixbuf
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Image Scaling Issue:")
|
print("Image Scaling Issue:")
|
||||||
print( repr(e) )
|
print( repr(e) )
|
||||||
|
@ -77,11 +66,11 @@ class Icon(DesktopIconMixin, VideoIconMixin):
|
||||||
|
|
||||||
def create_from_file(self, path):
|
def create_from_file(self, path):
|
||||||
try:
|
try:
|
||||||
return Gtk.Image.new_from_file(path)
|
return GdkPixbuf.Pixbuf.new_from_file(path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Image from file Issue:")
|
print("Image from file Issue:")
|
||||||
print( repr(e) )
|
print( repr(e) )
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def return_generic_icon(self):
|
def return_generic_icon(self):
|
||||||
return Gtk.Image.new_from_file(self.DEFAULT_ICON)
|
return GdkPixbuf.Pixbuf.new_from_file(self.DEFAULT_ICON)
|
||||||
|
|
|
@ -14,6 +14,9 @@ from .xdg.DesktopEntry import DesktopEntry
|
||||||
|
|
||||||
|
|
||||||
class DesktopIconMixin:
|
class DesktopIconMixin:
|
||||||
|
# NOTE: !!!IMPORTANT!!! This method is NOT thread safe and causes seg faults
|
||||||
|
# when ran from another thread! It took me a while to hunt this info down.
|
||||||
|
# Gio is the culprit. Pull this into a main Gtk thread if actually needed.
|
||||||
def get_system_thumbnail(self, filename, size):
|
def get_system_thumbnail(self, filename, size):
|
||||||
try:
|
try:
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
|
||||||
|
import os, shutil, subprocess, threading
|
||||||
|
|
||||||
|
|
||||||
|
class FileHandler:
|
||||||
|
def create_file(self, nFile, type):
|
||||||
|
try:
|
||||||
|
if TYPE == "dir":
|
||||||
|
os.mkdir(nFile)
|
||||||
|
elif TYPE == "file":
|
||||||
|
open(nFile, 'a').close()
|
||||||
|
except Exception as e:
|
||||||
|
print("An error occured creating the file/dir:")
|
||||||
|
print(repr(e))
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def update_file(self, oFile, nFile):
|
||||||
|
try:
|
||||||
|
print(f"Renaming: {oFile} --> {nFile}")
|
||||||
|
os.rename(oFile, nFile)
|
||||||
|
except Exception as e:
|
||||||
|
print("An error occured renaming the file:")
|
||||||
|
print(repr(e))
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def delete_file(self, toDeleteFile):
|
||||||
|
try:
|
||||||
|
print(f"Deleting: {toDeleteFile}")
|
||||||
|
if os.path.exists(toDeleteFile):
|
||||||
|
if os.path.isfile(toDeleteFile):
|
||||||
|
os.remove(toDeleteFile)
|
||||||
|
elif os.path.isdir(toDeleteFile):
|
||||||
|
shutil.rmtree(toDeleteFile)
|
||||||
|
else:
|
||||||
|
print("An error occured deleting the file:")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
print("The folder/file does not exist")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
print("An error occured deleting the file:")
|
||||||
|
print(repr(e))
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def move_file(self, fFile, tFile):
|
||||||
|
try:
|
||||||
|
print(f"Moving: {fFile} --> {tFile}")
|
||||||
|
if os.path.exists(fFile) and os.path.exists(tFile):
|
||||||
|
if not tFile.endswith("/"):
|
||||||
|
tFile += "/"
|
||||||
|
|
||||||
|
shutil.move(fFile, tFile)
|
||||||
|
else:
|
||||||
|
print("The folder/file does not exist")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
print("An error occured moving the file:")
|
||||||
|
print(repr(e))
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def copy_file(self,fFile, tFile, symlinks=False, ignore=None):
|
||||||
|
try:
|
||||||
|
if os.path.isdir(fFile):
|
||||||
|
shutil.copytree(fFile, tFile, symlinks, ignore)
|
||||||
|
else:
|
||||||
|
shutil.copy2(fFile, tFile)
|
||||||
|
except Exception as e:
|
||||||
|
print("An error occured copying the file:")
|
||||||
|
print(repr(e))
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
|
@ -57,6 +57,7 @@ class Settings:
|
||||||
|
|
||||||
subpath = settings["base_of_home"]
|
subpath = settings["base_of_home"]
|
||||||
HIDE_HIDDEN_FILES = True if settings["hide_hidden_files"] == "true" else False
|
HIDE_HIDDEN_FILES = True if settings["hide_hidden_files"] == "true" else False
|
||||||
|
FFMPG_THUMBNLR = FFMPG_THUMBNLR if settings["thumbnailer_path"] == "" else settings["thumbnailer_path"]
|
||||||
go_past_home = True if settings["go_past_home"] == "true" else False
|
go_past_home = True if settings["go_past_home"] == "true" else False
|
||||||
lock_folder = True if settings["lock_folder"] == "true" else False
|
lock_folder = True if settings["lock_folder"] == "true" else False
|
||||||
locked_folders = settings["locked_folders"].split("::::")
|
locked_folders = settings["locked_folders"].split("::::")
|
||||||
|
@ -79,7 +80,7 @@ class Settings:
|
||||||
fpdf = ('.pdf')
|
fpdf = ('.pdf')
|
||||||
|
|
||||||
|
|
||||||
# Dire structure check
|
# Dir structure check
|
||||||
if path.isdir(REMUX_FOLDER) == False:
|
if path.isdir(REMUX_FOLDER) == False:
|
||||||
os.mkdir(REMUX_FOLDER)
|
os.mkdir(REMUX_FOLDER)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue