Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/launchpad/analyzers/apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import lief

from ..artifacts import AppleArtifact, ZippedXCArchive
from ..insights.common import DuplicateFilesInsight, InsightsInput
from ..insights.common import DuplicateFilesInsight
from ..insights.insight import InsightsInput
from ..models import AppleAnalysisResults, AppleAppInfo, FileAnalysis, FileInfo, MachOBinaryAnalysis
from ..models.apple import AppleInsightResults
from ..models.treemap import FILE_TYPE_TO_TREEMAP_TYPE, TreemapType
Expand Down Expand Up @@ -128,7 +129,7 @@ def analyze(self, artifact: AppleArtifact) -> AppleAnalysisResults:
treemap=treemap,
)
insights = AppleInsightResults(
duplicate_files=DuplicateFilesInsight().__call__(insights_input),
duplicate_files=DuplicateFilesInsight().get_results(insights_input),
)

results = AppleAnalysisResults(
Expand Down
49 changes: 4 additions & 45 deletions src/launchpad/insights/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,16 @@
from __future__ import annotations

from collections import defaultdict
from dataclasses import dataclass
from typing import Dict, List, Protocol, TypeVar
from typing import Dict, List

from launchpad.models.common import FileAnalysis, FileInfo
from launchpad.insights.insight import Insight, InsightsInput
from launchpad.models.common import FileInfo
from launchpad.models.insights import DuplicateFilesInsightResult
from launchpad.models.treemap import TreemapResults

from ..models.apple import AppleAppInfo, MachOBinaryAnalysis

T_co = TypeVar("T_co", covariant=True)


@dataclass
class InsightsInput:
app_info: AppleAppInfo
file_analysis: FileAnalysis
treemap: TreemapResults | None
binary_analysis: List[MachOBinaryAnalysis]


class Insight(Protocol[T_co]):
"""Protocol for insight functions.

Insights are functions that take analysis results and return typed insight results.
All data needed for the insight must be collected during the main analysis phase.
"""

def __call__(self, input: InsightsInput) -> T_co:
"""Generate insights from analysis results.

Args:
results: The analysis results to generate insights from

Returns:
Typed insight results
"""
...


class DuplicateFilesInsight(Insight[DuplicateFilesInsightResult]):
"""Insight for duplicate files analysis."""

def __call__(self, input: InsightsInput) -> DuplicateFilesInsightResult:
"""Generate insights about duplicate files.

Args:
results: The analysis results to generate insights from

Returns:
Duplicate files insight results
"""
def get_results(self, input: InsightsInput) -> DuplicateFilesInsightResult:
# Group files by hash
files_by_hash: Dict[str, List[FileInfo]] = defaultdict(list)
for file in input.file_analysis.files:
Expand Down
35 changes: 35 additions & 0 deletions src/launchpad/insights/insight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from dataclasses import dataclass
from typing import Protocol, TypeVar

from ..models.apple import AppleAppInfo, MachOBinaryAnalysis
from ..models.common import FileAnalysis
from ..models.treemap import TreemapResults

T_co = TypeVar("T_co", covariant=True)


@dataclass
class InsightsInput:
app_info: AppleAppInfo
file_analysis: FileAnalysis
treemap: TreemapResults | None
binary_analysis: list[MachOBinaryAnalysis]


class Insight(Protocol[T_co]):
"""Protocol for insight functions.

Insights are functions that take analysis results and return typed insight results.
All data needed for the insight must be collected during the main analysis phase.
"""

def get_results(self, input: InsightsInput) -> T_co:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could this be something like perform() to indicate that work is being done, get kind of hides that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. when I implement the image compression insight that becomes more apparent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, will update to something along those lines

"""Generate insights from analysis results.

Args:
results: The analysis results to generate insights from

Returns:
Typed insight results
"""
raise NotImplementedError("Not implemented")