Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
perf: cache command (text editor) (#76)
NTFSvolume75ed7c23addb
diff --git a/cyberdrop_dl/ui/program_ui.py b/cyberdrop_dl/ui/program_ui.pyindex 068220f67..c741a913e 100644--- a/cyberdrop_dl/ui/program_ui.py+++ b/cyberdrop_dl/ui/program_ui.py@@ -17,9 +17,9 @@ from cyberdrop_dl.dependencies import browser_cookie3from cyberdrop_dl.ui.prompts import user_promptsfrom cyberdrop_dl.ui.prompts.basic_prompts import ask_dir_path, enter_to_continuefrom cyberdrop_dl.ui.prompts.defaults import DONE_CHOICE, EXIT_CHOICE+from cyberdrop_dl.utils import text_editorfrom cyberdrop_dl.utils.cookie_management import clear_cookiesfrom cyberdrop_dl.utils.sorting import Sorter-from cyberdrop_dl.utils.text_editor import open_in_text_editorfrom cyberdrop_dl.utils.utilities import clear_termif TYPE_CHECKING:@@ -228,7 +228,7 @@ class ProgramUI:def _open_in_text_editor(self, file_path: Path, *, reload_config: bool = True):try:- open_in_text_editor(file_path)+ text_editor.open(file_path)except ValueError as e:self.print_error(str(e))returndiff --git a/cyberdrop_dl/utils/text_editor.py b/cyberdrop_dl/utils/text_editor.pyindex bd8530865..bc139ab9b 100644--- a/cyberdrop_dl/utils/text_editor.py+++ b/cyberdrop_dl/utils/text_editor.py@@ -1,85 +1,85 @@from __future__ import annotationsimport functools+import loggingimport os-import platformimport shutilimport subprocess+import sysfrom pathlib import Path-import rich-_TEXT_EDITORS = "micro", "nano", "vim" # Ordered by preference-_USING_SSH = "SSH_CONNECTION" in os.environ-_USING_DESKTOP_ENVIROMENT = any(var in os.environ for var in ("DISPLAY", "WAYLAND_DISPLAY"))+_SSH = "SSH_CONNECTION" in os.environ+_HAS_DESKTOP_ENVIROMENT = any(var in os.environ for var in ("DISPLAY", "WAYLAND_DISPLAY"))_CUSTOM_EDITOR = os.environ.get("EDITOR")+_YAML_MIMETYPE = "application/yaml"+_TEXT_MIMETYPE = "text/plain"+logger = logging.getLogger(__name__)-def open_in_text_editor(file_path: Path) -> bool | None:- """Opens file in OS text editor."""-- if _CUSTOM_EDITOR:- path = shutil.which(_CUSTOM_EDITOR)- if not path:- msg = f"Editor '{_CUSTOM_EDITOR}' from env bar $EDITOR is not available"- raise ValueError(msg)- cmd = path, file_path-- elif platform.system() == "Darwin":- cmd = "open", "-a", "TextEdit", file_path- elif platform.system() == "Windows":- cmd = "notepad.exe", file_path+def open(file_path: Path) -> None:+ """Opens file in the OS's text editor."""- elif _USING_DESKTOP_ENVIROMENT and not _USING_SSH and _xdg_set_default_if_none(file_path):- cmd = "xdg-open", file_path-- elif fallback_editor := _find_text_editor():- cmd = fallback_editor, file_path- else:+ cmd = _editor_cmd()+ if not cmd:msg = "No default text editor found"raise ValueError(msg)+ cmd = *cmd, file_pathbin_path, *rest = cmdif Path(bin_path).stem == "micro":cmd = bin_path, "-keymenu", "true", *rest- rich.print(f"Opening '{file_path}' with '{bin_path}'...")- subprocess.call(cmd, stderr=subprocess.DEVNULL)+ logger.info(f"Opening '{file_path}' with '{bin_path}'...")+ _ = subprocess.call(cmd, stderr=subprocess.DEVNULL)@functools.cache+def _editor_cmd() -> tuple[str, ...] | None:+ if _CUSTOM_EDITOR:+ path = shutil.which(_CUSTOM_EDITOR)+ if path:+ return (path,)+ msg = f"Editor '{_CUSTOM_EDITOR}' from env bar $EDITOR is not available. Ignoring"+ logger.warning(msg)++ if sys.platform == "darwin":+ return "open", "-a", "TextEdit"++ if sys.platform == "win32":+ return ("notepad.exe",)++ if _HAS_DESKTOP_ENVIROMENT and not _SSH and _set_xdg_yaml_default_if_none():+ return ("xdg-open",)++ if fallback_editor := _find_text_editor():+ return (fallback_editor,)++def _find_text_editor() -> str | None:for editor in _TEXT_EDITORS:if bin_path := shutil.which(editor):return bin_path[email protected]_cache-def _xdg_set_default_if_none(file: Path) -> bool:+def _set_xdg_yaml_default_if_none() -> bool:"""- Ensures a file's MIME type has a default XDG app, falling back to whatever app is currently set for 'text/plain'-- Required to open YAML files as most of the time they have no default+ Ensures YAML's MIME type has a default XDG app, falling back to whatever app is currently set for 'text/plain'Returns `True` if a default app is now associated, `False` if setting the default failed"""- def xdg_mime_query(arg: str, *args: str) -> str:- cmd = "xdg-mime", "query", arg, *args+ def xdg_query_default(mimetype: str) -> str:+ cmd = "xdg-mime", "query", "default", mimetypeprocess = subprocess.run(cmd, capture_output=True, text=True, check=False)return process.stdout.strip()- mimetype = xdg_mime_query("filetype", str(file))- if not mimetype:- return False-- has_default = xdg_mime_query("default", mimetype)- if has_default:+ if xdg_query_default(_YAML_MIMETYPE):return True- default_text_app = xdg_mime_query("default", "text/plain")+ default_text_app = xdg_query_default(_TEXT_MIMETYPE)if not default_text_app:return False- return subprocess.call(["xdg-mime", "default", default_text_app, mimetype]) == 0+ return subprocess.call(["xdg-mime", "default", default_text_app, _YAML_MIMETYPE]) == 0