From 23fce80db1ea3be91be3b78e61bb244b90b0db36 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sat, 30 Jul 2022 21:59:02 -0700 Subject: [PATCH] [BUG] Fix chapter metadata ordering (#131) --- src/ytdl_sub/utils/chapters.py | 1 + src/ytdl_sub/utils/file_handler.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/ytdl_sub/utils/chapters.py b/src/ytdl_sub/utils/chapters.py index 8bb6ecbc..aedc614a 100644 --- a/src/ytdl_sub/utils/chapters.py +++ b/src/ytdl_sub/utils/chapters.py @@ -161,6 +161,7 @@ class Chapters: return FileMetadata.from_dict( value_dict={ts.readable_str: title for ts, title in zip(self.timestamps, self.titles)}, title=title, + sort_dict=False, # timestamps + titles are already sorted ) @classmethod diff --git a/src/ytdl_sub/utils/file_handler.py b/src/ytdl_sub/utils/file_handler.py index 3d9f55f1..5e4a4e1f 100644 --- a/src/ytdl_sub/utils/file_handler.py +++ b/src/ytdl_sub/utils/file_handler.py @@ -43,7 +43,9 @@ class FileMetadata: return self @classmethod - def from_dict(cls, value_dict: Dict[str, Any], title: Optional[str] = None) -> "FileMetadata": + def from_dict( + cls, value_dict: Dict[str, Any], title: Optional[str] = None, sort_dict: bool = True + ) -> "FileMetadata": """ Parameters ---------- @@ -51,13 +53,19 @@ class FileMetadata: Dict of things to print indented title Optional. Title line to put above the dict + sort_dict + Whether to sort dicts in the value_dict. Defaults to true. """ lines: List[str] = [] if title is not None: lines.append(title) def _recursive_add_dict_lines(rdict: Dict, indent: int): - for key, value in sorted(rdict.items()): + rdict_items = rdict.items() + if sort_dict: + rdict_items = sorted(rdict_items) + + for key, value in rdict_items: _indent = " " * indent if isinstance(value, Dict): lines.append(f"{_indent}{key}:")