Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ class SomeOtherClass:
│ │ └── io_wrapper.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── logger.py
│ │ └── result.py
│ │ └── logger.py
│ ├── domain
│ │ ├── __init__.py
│ │ ├── entities.py
Expand All @@ -170,8 +169,7 @@ class SomeOtherClass:
│ │ ├── __init__.py
│ │ └── test_adapter_apis.py
│ ├── core
│ │ ├── __init__.py
│ │ └── test_result.py
│ │ └── __init__.py
│ ├── domain
│ │ ├── __init__.py
│ │ ├── test_entities.py
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ authors = [
]
requires-python = ">=3.12"
dependencies = [
"attrs>=25.1.0",
"attrs>=25.4.0",
"black>=25.1.0",
"danom>=0.1.0",
"isort>=6.0.1",
"libcst>=1.6.0",
"numpy>=2.3.2",
Expand All @@ -36,7 +37,7 @@ dev = [
"ruff>=0.9.9",
]
research = [
"attrs==25.1.0",
"attrs==25.4.0",
"black==25.1.0",
"faker==37.1.0",
"fastapi==0.115.12",
Expand Down
29 changes: 6 additions & 23 deletions src/spaghettree/__main__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import argparse
import json
from pathlib import Path

from danom import Result

from spaghettree.adapters.io_wrapper import IOBase, IOWrapper
from spaghettree.core.logger import logger
from spaghettree.core.result import Result
from spaghettree.domain.optimisation import (
AdjMat,
get_dwm,
get_top_suggested_merges,
yellow,
)
from spaghettree.domain.parsing import (
create_call_tree,
extract_entities_and_locations,
filter_non_native_calls,
)
from spaghettree.domain.processing import (
analyse_existing_structure,
optimise_entity_positions,
)

Expand Down Expand Up @@ -65,20 +59,9 @@ def run_process(
new_root=new_root,
).unwrap()
else:
# remove any new_root so that it doesn't try to use ruff on the json
new_root = ""
adj_mat = AdjMat.from_call_tree(call_tree, optimise=optimise_src_code).unwrap()
print( # noqa: T201
yellow(
f"Current Directed Weighted Modularity (DWM): {get_dwm(adj_mat.mat, adj_mat.communities): .5f}"
)
)
top_merges = get_top_suggested_merges(adj_mat).unwrap()

for merge in top_merges:
merge.display()

res = {Path(call_tree_save_path).absolute(): json.dumps(call_tree, indent=4)}
res, new_root = analyse_existing_structure(
call_tree, call_tree_save_path, optimise_src_code=optimise_src_code
).unwrap()

return io.write_files(res, ruff_root=new_root, format_bulk=optimise_src_code)

Expand Down
4 changes: 2 additions & 2 deletions src/spaghettree/adapters/io_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import attrs
import black
import isort
from danom import Err, Ok, Result, safe
from ruff.__main__ import find_ruff_bin

from spaghettree.core.logger import logger
from spaghettree.core.result import Err, Ok, Result, safe
from spaghettree.domain.optimisation import yellow


Expand Down Expand Up @@ -50,7 +50,7 @@ def write(self, modified_code: str, filepath: str, *, format_code: bool = True)
def _run_ruff(self, path: str) -> None:
pass

def read_files(self, root: str | Path) -> Result:
def read_files(self, root: str | Path) -> Result[dict[str, str], dict[str, Err]]:
paths_res = self.list_files(root)
if not paths_res.is_ok():
return paths_res
Expand Down
97 changes: 0 additions & 97 deletions src/spaghettree/core/result.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/spaghettree/domain/optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import attrs
import numpy as np
from danom import safe

from spaghettree.core.logger import logger
from spaghettree.core.result import safe


@attrs.define
Expand Down
2 changes: 1 addition & 1 deletion src/spaghettree/domain/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import libcst as cst
import numpy as np
from danom import safe
from tqdm import tqdm

from spaghettree.core.logger import logger
from spaghettree.core.result import safe
from spaghettree.domain.entities import EntityCST
from spaghettree.domain.optimisation import AdjMat
from spaghettree.domain.visitors import EntityLocation, OnePassVisitor
Expand Down
30 changes: 29 additions & 1 deletion src/spaghettree/domain/processing.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import json
import os
from collections import Counter, defaultdict
from copy import deepcopy
from pathlib import Path
from typing import Literal

from danom import Result, safe

from spaghettree.core.logger import logger
from spaghettree.core.result import Result, safe
from spaghettree.domain.entities import EntityCST, ImportCST, ImportType
from spaghettree.domain.optimisation import (
AdjMat,
get_dwm,
get_top_suggested_merges,
merge_single_entity_communities_if_no_gain_penalty,
optimise_communities,
yellow,
)
from spaghettree.domain.parsing import (
cst_to_str,
Expand Down Expand Up @@ -42,6 +49,27 @@ def optimise_entity_positions(
)


@safe
def analyse_existing_structure(
call_tree: dict[str, list[str]], call_tree_save_path: str, *, optimise_src_code: bool
) -> tuple[dict[Path, str], Literal[""]]:
adj_mat = AdjMat.from_call_tree(call_tree, optimise=optimise_src_code).unwrap()
print( # noqa: T201
yellow(
f"Current Directed Weighted Modularity (DWM): {get_dwm(adj_mat.mat, adj_mat.communities): .5f}"
)
)
top_merges = get_top_suggested_merges(adj_mat).unwrap()

for merge in top_merges:
merge.display()

# remove any new_root so that it doesn't try to use ruff on the json
new_root = ""
res: dict[Path, str] = {Path(call_tree_save_path).absolute(): json.dumps(call_tree, indent=4)}
return res, new_root


@safe
def create_new_module_map(
adj_mat: AdjMat,
Expand Down
18 changes: 0 additions & 18 deletions tests/core/test_result.py

This file was deleted.

30 changes: 25 additions & 5 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.