Skip to content

Commit

Permalink
Inline open_with_system_default()
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Oct 4, 2024
1 parent 2f133ad commit 8bde122
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
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

0 comments on commit 8bde122

Please sign in to comment.