From 7bb559ebff929a3829c0ea7666cd00f90c68885f Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Mon, 24 Jul 2023 14:59:43 -0400 Subject: [PATCH] Always use utf-8, fix mypy errors --- auto_editor/analyze.py | 8 ++++---- auto_editor/formats/json.py | 2 +- auto_editor/formats/shotcut.py | 13 +++++++++---- auto_editor/render/subtitle.py | 6 +++--- auto_editor/subcommands/palet.py | 2 +- auto_editor/timeline.py | 10 +++------- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/auto_editor/analyze.py b/auto_editor/analyze.py index 6d291aab0..061c2c306 100644 --- a/auto_editor/analyze.py +++ b/auto_editor/analyze.py @@ -208,7 +208,7 @@ def read_cache(self, tag: str, obj: dict[str, Any]) -> None | np.ndarray: ) try: - with open(workfile) as file: + with open(workfile, encoding="utf-8") as file: cache = Parser(Lexer(workfile, file)).expr() except Exception: return None @@ -232,7 +232,7 @@ def cache(self, tag: str, obj: dict[str, Any], arr: np.ndarray) -> np.ndarray: key = obj_tag(tag, self.tb, obj) try: - with open(workfile) as file: + with open(workfile, encoding="utf-8") as file: json_object = Parser(Lexer(workfile, file)).expr() except Exception: json_object = {} @@ -249,7 +249,7 @@ def cache(self, tag: str, obj: dict[str, Any], arr: np.ndarray) -> np.ndarray: else: json_object[src_key] = {key: entry} - with open(os.path.join(workdur, "cache.json"), "w") as file: + with open(os.path.join(workdur, "cache.json"), "w", encoding="utf-8") as file: dump(json_object, file) return arr @@ -330,7 +330,7 @@ def subtitle( ) parser = SubtitleParser(self.tb) - with open(sub_file) as file: + with open(sub_file, encoding="utf-8") as file: parser.parse(file.read(), "webvtt") # stackoverflow.com/questions/9662346/python-code-to-remove-html-tags-from-a-string diff --git a/auto_editor/formats/json.py b/auto_editor/formats/json.py index a53d0c0c8..a42720ab4 100644 --- a/auto_editor/formats/json.py +++ b/auto_editor/formats/json.py @@ -238,7 +238,7 @@ def read_v1(tl: Any, ffmpeg: FFmpeg, log: Log) -> v3: def read_json(path: str, ffmpeg: FFmpeg, log: Log) -> v3: - with open(path) as f: + with open(path, encoding="utf-8", errors="ignore") as f: try: tl = Parser(Lexer(path, f)).expr() except MyError as e: diff --git a/auto_editor/formats/shotcut.py b/auto_editor/formats/shotcut.py index e5f31da18..03448db42 100644 --- a/auto_editor/formats/shotcut.py +++ b/auto_editor/formats/shotcut.py @@ -1,11 +1,17 @@ from __future__ import annotations import xml.etree.ElementTree as ET +from typing import TYPE_CHECKING, Any, cast -from auto_editor.ffwrapper import FFmpeg from auto_editor.timeline import v3 from auto_editor.utils.func import aspect_ratio, to_timecode -from auto_editor.utils.log import Log + +if TYPE_CHECKING: + from collections.abc import Sequence + + from auto_editor.ffwrapper import FFmpeg + from auto_editor.timeline import TlAudio, TlVideo + from auto_editor.utils.log import Log """ Shotcut uses the MLT timeline format @@ -78,13 +84,12 @@ def shotcut_write_mlt(output: str, tl: v3) -> None: producers = 0 if tl.v: - clips = tl.v[0] + clips: Sequence[TlVideo | TlAudio] = cast(Any, tl.v[0]) elif tl.a: clips = tl.a[0] else: clips = [] - for clip in clips: src = tl.sources[clip.src] length = to_timecode((clip.offset / clip.speed + clip.dur) / tb, "standard") diff --git a/auto_editor/render/subtitle.py b/auto_editor/render/subtitle.py index f953795af..aa0df006e 100644 --- a/auto_editor/render/subtitle.py +++ b/auto_editor/render/subtitle.py @@ -111,7 +111,7 @@ def edit(self, chunks: Chunks) -> None: self.contents = new_content def write(self, file_path: str) -> None: - with open(file_path, "w") as file: + with open(file_path, "w", encoding="utf-8") as file: file.write(self.header) for c in self.contents: file.write( @@ -135,12 +135,12 @@ def make_new_subtitles(tl: v3, ffmpeg: FFmpeg, temp: str, log: Log) -> list[str] parser = SubtitleParser(tl.tb) if sub.codec in parser.supported_codecs: - with open(file_path) as file: + with open(file_path, encoding="utf-8") as file: parser.parse(file.read(), sub.codec) else: convert_path = os.path.join(temp, f"{s}s_convert.vtt") ffmpeg.run(["-i", file_path, convert_path]) - with open(convert_path) as file: + with open(convert_path, encoding="utf-8") as file: parser.parse(file.read(), "webvtt") parser.edit(tl.v1.chunks) diff --git a/auto_editor/subcommands/palet.py b/auto_editor/subcommands/palet.py index 1034cf1b3..b451f0eaa 100644 --- a/auto_editor/subcommands/palet.py +++ b/auto_editor/subcommands/palet.py @@ -8,7 +8,7 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None: if sys_args: - with open(sys_args[0]) as file: + with open(sys_args[0], encoding="utf-8", errors="ignore") as file: program_text = file.read() try: diff --git a/auto_editor/timeline.py b/auto_editor/timeline.py index 2e2578a84..b186f4b45 100644 --- a/auto_editor/timeline.py +++ b/auto_editor/timeline.py @@ -30,12 +30,8 @@ def as_dict(self) -> dict: } -class Tl: - pass - - @dataclass -class TlVideo(Tl): +class TlVideo: start: int dur: int src: str @@ -46,7 +42,7 @@ class TlVideo(Tl): @dataclass -class TlAudio(Tl): +class TlAudio: start: int dur: int src: str @@ -58,7 +54,7 @@ class TlAudio(Tl): @dataclass -class _Visual(Tl): +class _Visual: start: int dur: int x: int | float