Make use of open_file from #84

Refactor done in #80 essentially reverted changes
Re-fixes #81, also re-fixes windows opening behind TagStudio
This commit is contained in:
Xarvex
2024-05-01 03:26:36 -05:00
parent f9fc28d5ec
commit c58590a221
4 changed files with 48 additions and 85 deletions

View File

@@ -23,7 +23,7 @@ from src.core.ts_core import *
from src.core.utils.web import *
from src.core.utils.fs import *
from src.core.library import *
from src.qt.helpers.file_opener import file_open
from src.qt.helpers.file_opener import open_file
WHITE_FG = '\033[37m'
WHITE_BG = '\033[47m'
@@ -2273,8 +2273,7 @@ class CliDriver:
elif (com[0].lower() == 'open' or com[0].lower() == 'o'):
# for match in self.lib.missing_matches[filename]:
# fn = f'{os.path.normpath(self.lib.library_dir + "/" + match + "/" + entry_1.filename)}'
# if os.path.isfile(fn):
# os.startfile(fn)
# open_file(fn)
file_open(dupe[0])
file_open(dupe[1])
# clear()

View File

@@ -1,4 +1,3 @@
from .open_file import open_file
from .file_opener import FileOpenerHelper, FileOpenerLabel
from .file_opener import open_file, FileOpenerHelper, FileOpenerLabel
from .function_iterator import FunctionIterator
from .custom_runnable import CustomRunnable
from .custom_runnable import CustomRunnable

View File

@@ -5,8 +5,9 @@
import logging
import os
import subprocess
import shutil
import sys
from os.path import isfile
import traceback
from PySide6.QtWidgets import QLabel
@@ -17,17 +18,46 @@ INFO = f'[INFO]'
logging.basicConfig(format="%(message)s", level=logging.INFO)
def file_open(filepath: str, check_first: bool = True):
if check_first and not os.path.isfile(filepath):
logging.error(f'File not found: {filepath}')
return False
if os.name == 'nt':
os.startfile(filepath)
elif sys.platform == 'darwin':
subprocess.Popen(['open', filepath])
else:
subprocess.call(["xdg-open", filepath])
def open_file(path: str, file_manager: bool = False):
logging.info(f'Opening file: {path}')
if not os.path.exists(path):
logging.error(f'File not found: {path}')
return
try:
if sys.platform == "win32":
normpath = os.path.normpath(path)
if file_manager:
command_name = "explorer"
command_args = [f"/select,{normpath}"]
else:
command_name = "start"
# first parameter is for title, NOT filepath
command_args = ["", normpath]
subprocess.Popen([command_name] + command_args, shell=True, close_fds=True, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_BREAKAWAY_FROM_JOB)
else:
if sys.platform == "darwin":
command_name = "open"
command_args = [path]
if file_manager:
# will reveal in Finder
command_args.append("-R")
else:
if file_manager:
command_name = "dbus-send"
# might not be guaranteed to launch default?
command_args = ["--session", "--dest=org.freedesktop.FileManager1", "--type=method_call",
"/org/freedesktop/FileManager1", "org.freedesktop.FileManager1.ShowItems",
f"array:string:file://{path}", "string:"]
else:
command_name = "xdg-open"
command_args = [path]
command = shutil.which(command_name)
if command is not None:
subprocess.Popen([command] + command_args, close_fds=True)
else:
logging.info(f"Could not find {command_name} on system PATH")
except:
traceback.print_exc()
class FileOpenerHelper:
@@ -38,26 +68,10 @@ class FileOpenerHelper:
self.filepath = filepath
def open_file(self):
logging.info(f'Opening file: {self.filepath}')
file_open(self.filepath)
open_file(self.filepath)
def open_explorer(self):
if not os.path.exists(self.filepath):
logging.error(f'File not found: {self.filepath}')
return
logging.info(f'Opening file: {self.filepath}')
if os.name == 'nt': # Windows
command = f'explorer /select,"{self.filepath}"'
subprocess.run(command, shell=True)
elif sys.platform == 'darwin':
subprocess.Popen(['open', '-R', self.filepath])
else: # macOS and Linux
command = f'nautilus --select "{self.filepath}"' # Adjust for your Linux file manager if different
if subprocess.run(command, shell=True).returncode == 0:
file_loc = os.path.dirname(self.filepath)
file_loc = os.path.normpath(file_loc)
os.startfile(file_loc)
open_file(self.filepath, True)
class FileOpenerLabel(QLabel):

View File

@@ -1,49 +0,0 @@
# Copyright (C) 2024 Travis Abendshien (CyanVoxel).
# Licensed under the GPL-3.0 License.
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio
import logging
import os
import sys
import traceback
import shutil
import subprocess
def open_file(path: str, file_manager: bool = False):
try:
if sys.platform == "win32":
normpath = os.path.normpath(path)
if file_manager:
command_name = "explorer"
command_args = [f"/select,{normpath}"]
else:
command_name = "start"
# first parameter is for title, NOT filepath
command_args = ["", normpath]
subprocess.Popen([command_name] + command_args, shell=True, close_fds=True, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.CREATE_BREAKAWAY_FROM_JOB)
else:
if sys.platform == "darwin":
command_name = "open"
command_args = [path]
if file_manager:
# will reveal in Finder
command_args.append("-R")
else:
if file_manager:
command_name = "dbus-send"
# might not be guaranteed to launch default?
command_args = ["--session", "--dest=org.freedesktop.FileManager1", "--type=method_call",
"/org/freedesktop/FileManager1", "org.freedesktop.FileManager1.ShowItems",
f"array:string:file://{path}", "string:"]
else:
command_name = "xdg-open"
command_args = [path]
command = shutil.which(command_name)
if command is not None:
subprocess.Popen([command] + command_args, close_fds=True)
else:
logging.info(f"Could not find {command_name} on system PATH")
except:
traceback.print_exc()