diff --git a/maxdiff/frozen_device_printer.py b/maxdiff/frozen_device_printer.py index 08eba01..9e6ce3b 100644 --- a/maxdiff/frozen_device_printer.py +++ b/maxdiff/frozen_device_printer.py @@ -1,5 +1,5 @@ from freezing_utils import * -from get_frozen_stats import get_stats, get_used_abstractions +from get_frozen_stats import get_stats, get_used_files def print_frozen_device(data: bytes) -> str: @@ -19,7 +19,7 @@ def print_frozen_device(data: bytes) -> str: footer_entries = parse_footer(footer_data[8:]) device_entries = get_device_entries(data, footer_entries) - used_abstractions = get_used_abstractions(device_entries) + used_files = get_used_files(device_entries) i = 0 for entry in device_entries: @@ -29,15 +29,10 @@ def print_frozen_device(data: bytes) -> str: if i == 0: frozen_string += f"{description} <= Device \n" else: - if file_name.endswith(".maxpat"): - if file_name in used_abstractions: - frozen_string += ( - f"{description}, {used_abstractions[file_name]} instances\n" - ) - else: - frozen_string += f"{description}, UNUSED\n" + if file_name in used_files: + frozen_string += f"{description}, {used_files[file_name]} instances\n" else: - frozen_string += f"{description}\n" + frozen_string += f"{description}, NOT FOUND IN PATCH\n" i += 1 frozen_string += get_stats(device_entries) return frozen_string diff --git a/maxdiff/get_frozen_stats.py b/maxdiff/get_frozen_stats.py index 6e4a18e..11c8354 100644 --- a/maxdiff/get_frozen_stats.py +++ b/maxdiff/get_frozen_stats.py @@ -57,7 +57,7 @@ def get_stats(entries: list[device_entry_with_data]): return summary -def get_used_abstractions(entries: list[device_entry_with_data]) -> dict[str, int]: +def get_used_files(entries: list[device_entry_with_data]) -> dict[str, int]: device = entries[0] # the first entry is always the device file abstraction_entries = [ @@ -65,7 +65,7 @@ def get_used_abstractions(entries: list[device_entry_with_data]) -> dict[str, in ] device_patch = get_patcher_dict(device) - abstractions_processor = AbstractionFileNamesProcessor() + abstractions_processor = FileNamesProcessor() process_patch_recursive(device_patch, abstraction_entries, abstractions_processor) return abstractions_processor.get_results() @@ -221,19 +221,49 @@ def get_results(self): return self.object_count, self.line_count -# TODO: expend with dependencies that might be included in the frozen device, like pictures -class AbstractionFileNamesProcessor(Processor): +# TODO: extend with dependencies that might be included in the frozen device, like pictures +class FileNamesProcessor(Processor): def __init__(self): - self.found_abstractions = {} + self.found_filenames = {} def process_elements(self, patcher, voice_count: int, abstraction_name=""): """If this patcher is an abstraction, i.e. when an abstraction_name is passed in, increment the entry in the dict""" + + filenames = get_filenames(patcher) if abstraction_name != "": - if abstraction_name in self.found_abstractions: - self.found_abstractions[abstraction_name] += voice_count + filenames.append(abstraction_name) + + for filename in filenames: + if filename in self.found_filenames: + self.found_filenames[filename] += voice_count else: - self.found_abstractions[abstraction_name] = voice_count + self.found_filenames[filename] = voice_count def get_results(self): """Returns a dict of used abstractions mapped to how oftern they are used.""" - return self.found_abstractions + return self.found_filenames + + +def get_filenames(patcher): + filenames = [] + for box_entry in patcher["boxes"]: + box = box_entry["box"] + + if box.get("pic"): + filenames.append(box.get("pic")) + + if box.get("pictures"): + for name in box.get("pictures"): + filenames.append(name) + + if box["maxclass"] == "pictctrl": + if box.get("name"): + filenames.append(box.get("name")) + + if box.get("bkgndpict"): + filenames.append(box.get("bkgndpict")) + + if box.get("knobpict"): + filenames.append(box.get("knobpict")) + + return filenames diff --git a/maxdiff/tests/test_baselines/FrozenTest.amxd.txt b/maxdiff/tests/test_baselines/FrozenTest.amxd.txt index 25a2398..d603761 100644 --- a/maxdiff/tests/test_baselines/FrozenTest.amxd.txt +++ b/maxdiff/tests/test_baselines/FrozenTest.amxd.txt @@ -2,18 +2,18 @@ MIDI Effect Device ------------------- Device is frozen ----- Contents ----- -Test.amxd: 23958 bytes, modified at 2024/10/15 14:31:39 UTC <= Device +Test.amxd: 24570 bytes, modified at 2024/10/15 14:34:06 UTC <= Device ParamAbstraction.maxpat: 1570 bytes, modified at 2024/05/24 13:59:36 UTC, 2 instances MyAbstraction.maxpat: 2015 bytes, modified at 2024/10/15 14:31:31 UTC, 2 instances AbstractionWithParameter.maxpat: 1569 bytes, modified at 2024/05/24 13:59:36 UTC, 2 instances -hz-icon.svg: 484 bytes, modified at 2024/05/24 13:59:36 UTC -beat-icon.svg: 533 bytes, modified at 2024/05/24 13:59:36 UTC -fpic.png: 7094 bytes, modified at 2024/05/24 13:59:36 UTC -collContent.txt: 8 bytes, modified at 2024/05/24 13:59:36 UTC +hz-icon.svg: 484 bytes, modified at 2024/05/24 13:59:36 UTC, 1 instances +beat-icon.svg: 533 bytes, modified at 2024/05/24 13:59:36 UTC, 1 instances +fpic.png: 7094 bytes, modified at 2024/05/24 13:59:36 UTC, 4 instances +collContent.txt: 8 bytes, modified at 2024/05/24 13:59:36 UTC, NOT FOUND IN PATCH Total - Counting every abstraction instance - Indicates loading time - Object instances: 49 + Object instances: 51 Connections: 16 Unique - Counting abstractions once - Indicates maintainability - Object instances: 44 + Object instances: 46 Connections: 14 diff --git a/maxdiff/tests/test_baselines/StatsTest.amxd.txt b/maxdiff/tests/test_baselines/StatsTest.amxd.txt index ea52654..14acecb 100644 --- a/maxdiff/tests/test_baselines/StatsTest.amxd.txt +++ b/maxdiff/tests/test_baselines/StatsTest.amxd.txt @@ -4,7 +4,7 @@ Device is frozen ----- Contents ----- StatsTest.amxd: 9344 bytes, modified at 2024/10/15 13:55:42 UTC <= Device MyAbstraction.maxpat: 2015 bytes, modified at 2024/10/15 13:54:08 UTC, 7 instances -UnusedAbstraction.maxpat: 2015 bytes, modified at 2024/10/15 13:54:59 UTC, UNUSED +UnusedAbstraction.maxpat: 2015 bytes, modified at 2024/10/15 13:54:59 UTC, NOT FOUND IN PATCH Total - Counting every abstraction instance - Indicates loading time Object instances: 33 diff --git a/maxdiff/tests/test_baselines/Test.amxd.txt b/maxdiff/tests/test_baselines/Test.amxd.txt index 4fa0d95..aeacacb 100644 --- a/maxdiff/tests/test_baselines/Test.amxd.txt +++ b/maxdiff/tests/test_baselines/Test.amxd.txt @@ -18,7 +18,6 @@ dependency_cache: {'name': 'MyAbstraction.maxpat', 'bootpath': '~/maxdevtools/maxdiff/tests/test_files', 'type': 'JSON', 'implicit': 1} {'name': 'ParamAbstraction.maxpat', 'bootpath': '~/maxdevtools/maxdiff/tests/test_files', 'type': 'JSON', 'implicit': 1} {'name': 'beat-icon.svg', 'bootpath': '~/maxdevtools/maxdiff/tests/test_files', 'type': 'svg', 'implicit': 1} - {'name': 'collContent.txt', 'bootpath': '~/maxdevtools/maxdiff/tests/test_files', 'type': 'TEXT', 'implicit': 1} {'name': 'fpic.png', 'bootpath': '~/maxdevtools/maxdiff/tests/test_files', 'type': 'PNG', 'implicit': 1} {'name': 'hz-icon.svg', 'bootpath': '~/maxdevtools/maxdiff/tests/test_files', 'type': 'svg', 'implicit': 1} @@ -31,6 +30,8 @@ project: ----------- patcher ----------- appversion: 8.6.2-x64-1 | rect: [65, 399, 927, 289] | openrect: [0, 0, 0, 169] | default_fontsize: 10.0 | default_fontname: Arial Bold | gridsize: [8, 8] | boxanimatetime: 500 | latency: 0 | is_mpe: 0 | platform_compatibility: 0 | autosave: 0 ----------- objects ----------- +[pictslider] bkgndpict: fpic.png | clickedimage: 0 | knobpict: fpic.png +[pictctrl] name: fpic.png [bpatcher ThisWasAnAbstractionBeforeEmbeddingIt.maxpat] embed: 1 ----------- patcher ----------- appversion: 8.6.2-x64-1 | rect: [927, 431, 640, 480] diff --git a/maxdiff/tests/test_files/FrozenTest.amxd b/maxdiff/tests/test_files/FrozenTest.amxd index e72138f..aeaf822 100644 Binary files a/maxdiff/tests/test_files/FrozenTest.amxd and b/maxdiff/tests/test_files/FrozenTest.amxd differ diff --git a/maxdiff/tests/test_files/Test.amxd b/maxdiff/tests/test_files/Test.amxd index cf5f281..d3d406b 100644 Binary files a/maxdiff/tests/test_files/Test.amxd and b/maxdiff/tests/test_files/Test.amxd differ