Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline open_with_system_default() #548

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions auto_editor/edit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import os
import sys
from subprocess import run
from typing import Any

from auto_editor.ffwrapper import FFmpeg, FileInfo, initFileInfo
Expand All @@ -15,7 +17,6 @@
from auto_editor.utils.chunks import Chunk, Chunks
from auto_editor.utils.cmdkw import ParserError, parse_with_palet, pAttr, pAttrs
from auto_editor.utils.container import Container, container_constructor
from auto_editor.utils.func import open_with_system_default
from auto_editor.utils.log import Log
from auto_editor.utils.types import Args

Expand Down Expand Up @@ -356,10 +357,21 @@ def pad_chunk(chunk: Chunk, total: int) -> Chunks:

if not args.no_open and export in ("default", "audio"):
if args.player is None:
if (ret := open_with_system_default(output)) is not None:
log.warning(ret)
if sys.platform == "win32":
try:
os.startfile(output)
except OSError:
log.warning(f"Could not find application to open file: {output}")
else:
try: # MacOS case
run(["open", output])
except Exception:
try: # WSL2 case
run(["cmd.exe", "/C", "start", output])
except Exception:
try: # Linux case
run(["xdg-open", output])
except Exception:
log.warning(f"Could not open output file: {output}")
else:
import subprocess
from shlex import split

subprocess.run(split(args.player) + [output])
run(__import__("shlex").split(args.player) + [output])
25 changes: 0 additions & 25 deletions auto_editor/utils/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,6 @@ def human_readable_time(time_in_secs: float) -> str:
return f"{time_in_secs} {units}"


def open_with_system_default(path: str) -> str | None:
import sys
from subprocess import run

if sys.platform == "win32":
from os import startfile

try:
startfile(path)
except OSError:
return f"Could not find application to open file: {path}"
else:
try: # MacOS case
run(["open", path])
except Exception:
try: # WSL2 case
run(["cmd.exe", "/C", "start", path])
except Exception:
try: # Linux case
run(["xdg-open", path])
except Exception:
return f"Could not open output file: {path}"
return None


def append_filename(path: str, val: str) -> str:
from os.path import splitext

Expand Down