WIP implimentig new files widget, updated settings, broke keybindings
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Utils module
|
||||
"""
|
@@ -0,0 +1,87 @@
|
||||
# Python imports
|
||||
import os
|
||||
import shutil
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
|
||||
|
||||
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 not 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
|
@@ -0,0 +1,116 @@
|
||||
# Python imports
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Apoplication imports
|
||||
|
||||
|
||||
class ShellFMLauncherException(Exception):
|
||||
...
|
||||
|
||||
|
||||
|
||||
class Launcher:
|
||||
def open_file_locally(self, file):
|
||||
lowerName = file.lower()
|
||||
command = []
|
||||
|
||||
if lowerName.endswith(self.fvideos):
|
||||
command = [self.media_app, file]
|
||||
elif lowerName.endswith(self.fimages):
|
||||
command = [self.image_app, file]
|
||||
elif lowerName.endswith(self.fmusic):
|
||||
command = [self.music_app, file]
|
||||
elif lowerName.endswith(self.foffice):
|
||||
command = [self.office_app, file]
|
||||
elif lowerName.endswith(self.fcode):
|
||||
command = [self.code_app, file]
|
||||
elif lowerName.endswith(self.ftext):
|
||||
command = [self.text_app, file]
|
||||
elif lowerName.endswith(self.fpdf):
|
||||
command = [self.pdf_app, file]
|
||||
elif lowerName.endswith("placeholder-until-i-can-get-a-use-pref-fm-flag"):
|
||||
command = [self.file_manager_app, file]
|
||||
else:
|
||||
command = ["xdg-open", file]
|
||||
|
||||
self.execute(command)
|
||||
|
||||
|
||||
def execute(self, command, start_dir=os.getenv("HOME"), use_shell=False):
|
||||
try:
|
||||
self.logger.debug(command)
|
||||
subprocess.Popen(command, cwd=start_dir, shell=use_shell, start_new_session=True, stdout=None, stderr=None, close_fds=True)
|
||||
except ShellFMLauncherException as e:
|
||||
self.logger.error(f"Couldn't execute: {command}")
|
||||
self.logger.error(e)
|
||||
|
||||
# TODO: Return std(out/in/err) handlers along with subprocess instead of sinking to null
|
||||
def execute_and_return_thread_handler(self, command, start_dir=os.getenv("HOME"), use_shell=False):
|
||||
try:
|
||||
DEVNULL = open(os.devnull, 'w')
|
||||
return subprocess.Popen(command, cwd=start_dir, shell=use_shell, start_new_session=False, stdout=DEVNULL, stderr=DEVNULL, close_fds=False)
|
||||
except ShellFMLauncherException as e:
|
||||
self.logger.error(f"Couldn't execute and return thread: {command}")
|
||||
self.logger.error(e)
|
||||
return None
|
||||
|
||||
@threaded
|
||||
def app_chooser_exec(self, app_info, uris):
|
||||
app_info.launch_uris_async(uris)
|
||||
|
||||
def remux_video(self, hash, file):
|
||||
remux_vid_pth = "{self.REMUX_FOLDER}/{hash}.mp4"
|
||||
self.logger.debug(remux_vid_pth)
|
||||
|
||||
if not os.path.isfile(remux_vid_pth):
|
||||
self.check_remux_space()
|
||||
|
||||
command = ["ffmpeg", "-i", file, "-hide_banner", "-movflags", "+faststart"]
|
||||
if file.endswith("mkv"):
|
||||
command += ["-codec", "copy", "-strict", "-2"]
|
||||
if file.endswith("avi"):
|
||||
command += ["-c:v", "libx264", "-crf", "21", "-c:a", "aac", "-b:a", "192k", "-ac", "2"]
|
||||
if file.endswith("wmv"):
|
||||
command += ["-c:v", "libx264", "-crf", "23", "-c:a", "aac", "-strict", "-2", "-q:a", "100"]
|
||||
if file.endswith("f4v") or file.endswith("flv"):
|
||||
command += ["-vcodec", "copy"]
|
||||
|
||||
command += [remux_vid_pth]
|
||||
try:
|
||||
proc = subprocess.Popen(command)
|
||||
proc.wait()
|
||||
except ShellFMLauncherException as e:
|
||||
self.logger.error(message)
|
||||
self.logger.error(e)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def check_remux_space(self):
|
||||
limit = self.remux_folder_max_disk_usage
|
||||
try:
|
||||
limit = int(limit)
|
||||
except ShellFMLauncherException as e:
|
||||
self.logger.debug(e)
|
||||
return
|
||||
|
||||
usage = self.get_remux_folder_usage(self.REMUX_FOLDER)
|
||||
if usage > limit:
|
||||
files = os.listdir(self.REMUX_FOLDER)
|
||||
for file in files:
|
||||
fp = os.path.join(self.REMUX_FOLDER, file)
|
||||
os.unlink(fp)
|
||||
|
||||
|
||||
def get_remux_folder_usage(self, start_path = "."):
|
||||
total_size = 0
|
||||
for dirpath, dirnames, filenames in os.walk(start_path):
|
||||
for f in filenames:
|
||||
fp = os.path.join(dirpath, f)
|
||||
if not os.path.islink(fp): # Skip if it is symbolic link
|
||||
total_size += os.path.getsize(fp)
|
||||
|
||||
return total_size
|
@@ -0,0 +1,101 @@
|
||||
# Python imports
|
||||
import json
|
||||
import os
|
||||
from os import path
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Apoplication imports
|
||||
|
||||
|
||||
class ShellFMSettingsException(Exception):
|
||||
...
|
||||
|
||||
|
||||
|
||||
class Settings:
|
||||
logger = None
|
||||
|
||||
# NOTE: app_name should be defined using python 'builtins'
|
||||
app_name_exists = False
|
||||
try:
|
||||
app_name
|
||||
app_name_exists = True
|
||||
except Exception as e:
|
||||
...
|
||||
|
||||
APP_CONTEXT = f"{app_name.lower()}" if app_name_exists else "shellfm"
|
||||
USR_APP_CONTEXT = f"/usr/share/{APP_CONTEXT}"
|
||||
USER_HOME = path.expanduser('~')
|
||||
CONFIG_PATH = f"{USER_HOME}/.config/{APP_CONTEXT}"
|
||||
CONFIG_FILE = f"{CONFIG_PATH}/settings.json"
|
||||
HIDE_HIDDEN_FILES = True
|
||||
|
||||
DEFAULT_ICONS = f"{CONFIG_PATH}/icons"
|
||||
DEFAULT_ICON = f"{DEFAULT_ICONS}/text.png"
|
||||
FFMPG_THUMBNLR = f"{CONFIG_PATH}/ffmpegthumbnailer" # Thumbnail generator binary
|
||||
BLENDER_THUMBNLR = f"{CONFIG_PATH}/blender-thumbnailer" # Blender thumbnail generator binary
|
||||
REMUX_FOLDER = f"{USER_HOME}/.remuxs" # Remuxed files folder
|
||||
|
||||
ICON_DIRS = ["/usr/share/icons", f"{USER_HOME}/.icons" "/usr/share/pixmaps"]
|
||||
BASE_THUMBS_PTH = f"{USER_HOME}/.thumbnails"
|
||||
ABS_THUMBS_PTH = f"{BASE_THUMBS_PTH}/normal"
|
||||
STEAM_ICONS_PTH = f"{BASE_THUMBS_PTH}/steam_icons"
|
||||
|
||||
if not os.path.exists(CONFIG_PATH) or not os.path.exists(CONFIG_FILE):
|
||||
msg = f"No config file located! Aborting loading ShellFM library...\nExpected: {CONFIG_FILE}"
|
||||
raise ShellFMSettingsException(msg)
|
||||
|
||||
if not path.isdir(REMUX_FOLDER):
|
||||
os.mkdir(REMUX_FOLDER)
|
||||
|
||||
if not path.isdir(BASE_THUMBS_PTH):
|
||||
os.mkdir(BASE_THUMBS_PTH)
|
||||
|
||||
if not path.isdir(ABS_THUMBS_PTH):
|
||||
os.mkdir(ABS_THUMBS_PTH)
|
||||
|
||||
if not path.isdir(STEAM_ICONS_PTH):
|
||||
os.mkdir(STEAM_ICONS_PTH)
|
||||
|
||||
if not os.path.exists(DEFAULT_ICONS):
|
||||
DEFAULT_ICONS = f"{USR_APP_CONTEXT}/icons"
|
||||
DEFAULT_ICON = f"{DEFAULT_ICONS}/text.png"
|
||||
|
||||
with open(CONFIG_FILE) as f:
|
||||
settings = json.load(f)
|
||||
config = settings["config"]
|
||||
|
||||
subpath = config["base_of_home"]
|
||||
STEAM_CDN_URL = config["steam_cdn_url"]
|
||||
FFMPG_THUMBNLR = FFMPG_THUMBNLR if config["thumbnailer_path"] == "" else config["thumbnailer_path"]
|
||||
BLENDER_THUMBNLR = BLENDER_THUMBNLR if config["blender_thumbnailer_path"] == "" else config["blender_thumbnailer_path"]
|
||||
HIDE_HIDDEN_FILES = True if config["hide_hidden_files"] in ["true", ""] else False
|
||||
go_past_home = True if config["go_past_home"] in ["true", ""] else False
|
||||
lock_folder = False if config["lock_folder"] in ["false", ""] else True
|
||||
locked_folders = config["locked_folders"].split("::::")
|
||||
mplayer_options = config["mplayer_options"].split()
|
||||
music_app = config["music_app"]
|
||||
media_app = config["media_app"]
|
||||
image_app = config["image_app"]
|
||||
office_app = config["office_app"]
|
||||
pdf_app = config["pdf_app"]
|
||||
code_app = config["code_app"]
|
||||
text_app = config["text_app"]
|
||||
terminal_app = config["terminal_app"]
|
||||
container_icon_wh = config["container_icon_wh"]
|
||||
video_icon_wh = config["video_icon_wh"]
|
||||
sys_icon_wh = config["sys_icon_wh"]
|
||||
file_manager_app = config["file_manager_app"]
|
||||
remux_folder_max_disk_usage = config["remux_folder_max_disk_usage"]
|
||||
|
||||
# Filters
|
||||
filters = settings["filters"]
|
||||
fmeshs = tuple(filters["meshs"])
|
||||
fcode = tuple(filters["code"])
|
||||
fvideos = tuple(filters["videos"])
|
||||
foffice = tuple(filters["office"])
|
||||
fimages = tuple(filters["images"])
|
||||
ftext = tuple(filters["text"])
|
||||
fmusic = tuple(filters["music"])
|
||||
fpdf = tuple(filters["pdf"])
|
Reference in New Issue
Block a user