[BUG] Fix chapter metadata ordering (#131)

This commit is contained in:
Jesse Bannon 2022-07-30 21:59:02 -07:00 committed by GitHub
parent 09bc12c815
commit 23fce80db1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -161,6 +161,7 @@ class Chapters:
return FileMetadata.from_dict( return FileMetadata.from_dict(
value_dict={ts.readable_str: title for ts, title in zip(self.timestamps, self.titles)}, value_dict={ts.readable_str: title for ts, title in zip(self.timestamps, self.titles)},
title=title, title=title,
sort_dict=False, # timestamps + titles are already sorted
) )
@classmethod @classmethod

View file

@ -43,7 +43,9 @@ class FileMetadata:
return self return self
@classmethod @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 Parameters
---------- ----------
@ -51,13 +53,19 @@ class FileMetadata:
Dict of things to print indented Dict of things to print indented
title title
Optional. Title line to put above the dict Optional. Title line to put above the dict
sort_dict
Whether to sort dicts in the value_dict. Defaults to true.
""" """
lines: List[str] = [] lines: List[str] = []
if title is not None: if title is not None:
lines.append(title) lines.append(title)
def _recursive_add_dict_lines(rdict: Dict, indent: int): 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 _indent = " " * indent
if isinstance(value, Dict): if isinstance(value, Dict):
lines.append(f"{_indent}{key}:") lines.append(f"{_indent}{key}:")