Skip to content

Commit

Permalink
Combine Timer and Log
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Jun 14, 2024
1 parent 9f7a44d commit 81a0cf6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 63 deletions.
44 changes: 21 additions & 23 deletions auto_editor/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import sys

import auto_editor
from auto_editor.edit import edit_media
from auto_editor.ffwrapper import FFmpeg
from auto_editor.utils.func import setup_tempdir
from auto_editor.utils.log import Log, Timer
from auto_editor.utils.log import Log
from auto_editor.utils.types import (
Args,
bitrate,
Expand Down Expand Up @@ -277,7 +279,7 @@ def main() -> None:
f"auto_editor.subcommands.{sys.argv[1]}", fromlist=["subcommands"]
)
obj.main(sys.argv[2:])
sys.exit()
return

args = main_options(ArgumentParser("Auto-Editor")).parse_args(
Args,
Expand All @@ -296,41 +298,37 @@ def main() -> None:

if args.version:
print(f"{auto_editor.version} ({auto_editor.__version__})")
sys.exit()
return

from auto_editor.edit import edit_media
from auto_editor.ffwrapper import FFmpeg

log = Log(args.debug, args.quiet)
ffmpeg = FFmpeg(
args.ffmpeg_location,
args.my_ffmpeg,
args.show_ffmpeg_commands,
args.show_ffmpeg_output,
)

if args.debug and args.input == []:
if args.debug and not args.input:
import platform as plat

import av

print(f"Python Version: {plat.python_version()}")
print(f"Platform: {plat.system()} {plat.release()} {plat.machine().lower()}")
print(f"FFmpeg Version: {ffmpeg.version}\nFFmpeg Path: {ffmpeg.path}")
print(f"PyAV Version: {av.__version__}")
print(f"Auto-Editor Version: {auto_editor.version}")
sys.exit()
return

if args.input == []:
log = Log(args.debug, args.quiet)
if not args.input:
log.error("You need to give auto-editor an input file.")

temp = setup_tempdir(args.temp_dir, Log())
log = Log(args.debug, args.quiet, temp)
log.machine = args.progress == "machine"
temp = setup_tempdir(args.temp_dir, log)
log = Log(args.debug, args.quiet, temp, args.progress == "machine")
log.debug(f"Temp Directory: {temp}")

ffmpeg = FFmpeg(
args.ffmpeg_location,
args.my_ffmpeg,
args.show_ffmpeg_commands,
args.show_ffmpeg_output,
)
paths = valid_input(args.input, ffmpeg, args, log)
timer = Timer(args.quiet or log.machine)

try:
edit_media(paths, ffmpeg, args, temp, timer, log)
edit_media(paths, ffmpeg, args, temp, log)
except KeyboardInterrupt:
log.error("Keyboard Interrupt")
log.cleanup()
Expand Down
7 changes: 3 additions & 4 deletions auto_editor/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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.log import Log, Timer
from auto_editor.utils.log import Log
from auto_editor.utils.types import Args


Expand Down Expand Up @@ -150,7 +150,7 @@ def parse_export(export: str, log: Log) -> dict[str, Any]:


def edit_media(
paths: list[str], ffmpeg: FFmpeg, args: Args, temp: str, timer: Timer, log: Log
paths: list[str], ffmpeg: FFmpeg, args: Args, temp: str, log: Log
) -> None:
bar = Bar(args.progress)
tl = None
Expand Down Expand Up @@ -190,7 +190,6 @@ def edit_media(

if export["export"] == "timeline":
log.quiet = True
timer.quiet = True

if not args.preview:
log.conwrite("Starting")
Expand Down Expand Up @@ -357,7 +356,7 @@ def pad_chunk(chunk: Chunk, total: int) -> Chunks:
else:
make_media(tl, output)

timer.stop()
log.stop_timer()

if not args.no_open and export["export"] in ("default", "audio", "clip-sequence"):
if args.player is None:
Expand Down
2 changes: 1 addition & 1 deletion auto_editor/formats/fcp7.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def xml_bool(val: str) -> bool:
try:
tree = ET.parse(path)
except FileNotFoundError:
log.nofile(path)
log.error(f"Could not find '{path}'")

root = tree.getroot()

Expand Down
2 changes: 1 addition & 1 deletion auto_editor/subcommands/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:

for file in args.input:
if not os.path.isfile(file):
log.nofile(file)
log.error(f"Could not find '{file}'")

ext = os.path.splitext(file)[1]
if ext == ".json":
Expand Down
59 changes: 26 additions & 33 deletions auto_editor/utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,26 @@

import sys
from datetime import timedelta
from pathlib import Path
from shutil import get_terminal_size, rmtree
from time import perf_counter, sleep
from typing import NoReturn


class Timer:
__slots__ = ("start_time", "quiet")

def __init__(self, quiet: bool = False):
self.start_time = perf_counter()
self.quiet = quiet

def stop(self) -> None:
if not self.quiet:
second_len = round(perf_counter() - self.start_time, 2)
minute_len = timedelta(seconds=round(second_len))

sys.stdout.write(f"Finished. took {second_len} seconds ({minute_len})\n")


class Log:
__slots__ = ("is_debug", "quiet", "temp", "machine")
__slots__ = ("is_debug", "quiet", "temp", "machine", "start_time")

def __init__(
self, show_debug: bool = False, quiet: bool = False, temp: str | None = None
self,
is_debug: bool = False,
quiet: bool = False,
temp: str | None = None,
machine: bool = False,
):
self.is_debug = show_debug
self.is_debug = is_debug
self.quiet = quiet
self.temp = temp
self.machine = False
self.machine = machine
self.start_time = 0 if self.quiet or self.machine else perf_counter()

def debug(self, message: object) -> None:
if self.is_debug:
Expand Down Expand Up @@ -62,6 +51,23 @@ def conwrite(self, message: str) -> None:
buffer = " " * (get_terminal_size().columns - len(message) - 3)
sys.stdout.write(f" {message}{buffer}\r")

def print(self, message: str) -> None:
if not self.quiet:
self.conwrite("")
sys.stdout.write(f"{message}\n")

def warning(self, message: str) -> None:
if not self.quiet:
self.conwrite("")
sys.stderr.write(f"Warning! {message}\n")

def stop_timer(self) -> None:
if not self.quiet and not self.machine:
second_len = round(perf_counter() - self.start_time, 2)
minute_len = timedelta(seconds=round(second_len))

sys.stdout.write(f"Finished. took {second_len} seconds ({minute_len})\n")

def error(self, message: str | Exception) -> NoReturn:
if self.is_debug and isinstance(message, Exception):
self.cleanup()
Expand All @@ -81,16 +87,3 @@ def error(self, message: str | Exception) -> NoReturn:
import os

os._exit(1)

def nofile(self, path: str | Path) -> NoReturn:
self.error(f"Could not find '{path}'")

def warning(self, message: str) -> None:
if not self.quiet:
self.conwrite("")
sys.stderr.write(f"Warning! {message}\n")

def print(self, message: str) -> None:
if not self.quiet:
self.conwrite("")
sys.stdout.write(f"{message}\n")
2 changes: 1 addition & 1 deletion auto_editor/validate_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ def valid_input(inputs: list[str], ffmpeg: FFmpeg, args: Args, log: Log) -> list
else:
if os.path.isdir(my_input):
log.error("Input must be a file or a URL, not a directory.")
log.nofile(my_input)
log.error(f"Could not find '{my_input}'")

return new_inputs

0 comments on commit 81a0cf6

Please sign in to comment.