Skip to content

Commit

Permalink
Get usage count for other dependencies as well
Browse files Browse the repository at this point in the history
Also refreeze FrozenTest to match the updated Test.amxd

Note: not all objects that have external dependencies may be covered yet
  • Loading branch information
mkp committed Oct 15, 2024
1 parent 3b08f93 commit 5cd1384
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 28 deletions.
15 changes: 5 additions & 10 deletions maxdiff/frozen_device_printer.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down
48 changes: 39 additions & 9 deletions maxdiff/get_frozen_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ 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 = [
item for item in entries if str(item["file_name"]).endswith(".maxpat")
]

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()

Expand Down Expand Up @@ -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
14 changes: 7 additions & 7 deletions maxdiff/tests/test_baselines/FrozenTest.amxd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion maxdiff/tests/test_baselines/StatsTest.amxd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion maxdiff/tests/test_baselines/Test.amxd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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]
Expand Down
Binary file modified maxdiff/tests/test_files/FrozenTest.amxd
Binary file not shown.
Binary file modified maxdiff/tests/test_files/Test.amxd
Binary file not shown.

0 comments on commit 5cd1384

Please sign in to comment.