Skip to content

Commit

Permalink
Merge pull request #40 from Ableton/mkp-fix-typing
Browse files Browse the repository at this point in the history
Fix some type errors indicated by mypy
  • Loading branch information
MattijsKneppers authored Jul 8, 2024
2 parents 7f81fcb + 80e5ae1 commit 69ceaf0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
15 changes: 12 additions & 3 deletions maxdiff/frozen_device_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ def parse_footer(data: bytes) -> list[str]:
size = int.from_bytes(data[4:8], byteorder="big")
fields = get_fields(data[8 : 8 + size])
if "fnam" in fields and "sz32" in fields and "mdat" in fields:
name_field = fields["fnam"]
size_field = fields["sz32"]
date_field = fields["mdat"]
if not (
isinstance(name_field, str)
and isinstance(size_field, int)
and isinstance(date_field, datetime.datetime)
):
raise Exception("Incorrect type for parsed footer fields")
dependencies.append(
f'{fields["fnam"]}: {fields["sz32"]} bytes, modified at {fields["mdat"].strftime("%Y/%m/%d %T")} UTC'
f'{fields["fnam"]}: {fields["sz32"]} bytes, modified at {date_field.strftime("%Y/%m/%d %T")} UTC'
)
data = data[size:]
return dependencies


def get_fields(data: bytes) -> dict[str, str | int | datetime.datetime]:
def get_fields(data: bytes) -> dict[str, str | int | datetime.datetime | None]:
"""Parses the data for a frozen dependency and returns a dict of its fields and their contents."""
fields = {}
while len(data) >= 12:
Expand Down Expand Up @@ -69,7 +78,7 @@ def parse_field_data(
return int.from_bytes(data, byteorder="big")
case "mdat":
return get_hfs_date(data)
return
return None


def remove_trailing_zeros(data: bytes) -> bytes:
Expand Down
12 changes: 6 additions & 6 deletions maxdiff/patch_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def print_patcher_summary_recursive(
"----------- patch cords -----------\n", indent
)

lines = []
for line in patcher_dict["patcher"]["lines"]:
from_name = ""
from_outlet = ""
Expand Down Expand Up @@ -215,7 +214,9 @@ def get_box_text(box: dict) -> str:
return boxtext


def get_object_names_from_ids_recursive(id_hierarchy: list, boxes: dict, indent: int = 0):
def get_object_names_from_ids_recursive(
id_hierarchy: list, boxes_parent: list, indent: int = 0
):
"""Translates an object id hierarchy string in the form of "obj-n::obj-m:: etc" to a string
in the form of "<objectname1>/<objectname2/ etc", replacing the object ids with the textual
representation of these objects.
Expand All @@ -224,8 +225,8 @@ def get_object_names_from_ids_recursive(id_hierarchy: list, boxes: dict, indent:
inside an abstraction, the object id is used instead.
"""

# every entry of "boxes" has a single item "box"
boxes = list(map(lambda val: val["box"], boxes))
# every entry of "boxes_parent" has a single item "box"
boxes = list(map(lambda val: val["box"], boxes_parent))

id_to_check = id_hierarchy[0] if isinstance(id_hierarchy, list) else id_hierarchy
name = ""
Expand Down Expand Up @@ -360,7 +361,6 @@ def get_parameters_string_block(patcher: dict) -> str:
Non-overridden parameter attributes are already shown with the parameter objects.
"""
parameters = patcher["parameters"]
boxes = patcher["boxes"]
parameters_string = ""
for key, value in parameters.items():
if key in ["parameter_overrides", "parameterbanks"]:
Expand All @@ -369,7 +369,7 @@ def get_parameters_string_block(patcher: dict) -> str:
parsed_key = key
if key.startswith("obj"):
id_tokens = key.split("::")
parsed_key = get_object_names_from_ids_recursive(id_tokens, boxes)
parsed_key = get_object_names_from_ids_recursive(id_tokens, patcher["boxes"])

parameters_string += f"\t{parsed_key}: {value}"

Expand Down

0 comments on commit 69ceaf0

Please sign in to comment.