Split top-level Python functions into dedicated modules and rewire imports automatically.
Project description
ManaSplice
ManaSplice is a small CLI for pulling top-level Python functions into their own files without leaving the original module broken.
It creates a
modules/package next to the source file, moves the function there, and rewrites the source module to import it back in.
What it does
- Splits one top-level function with
splitfunc. - Splits every top-level function in a file, or every top-level function in each Python file in a directory, with
splitall. - Restructures modules toward a simple OOP shape with
paradigm OOP. - Checks whether a split is safe, and shows the planned changes without writing files, with
check. - Emits JSON plans with
--jsonfor automation. - Copies the imports and top-level definitions the extracted function needs.
- Maintains
modules/__init__.pyso rewritten files can use a single merged import line.
Quick example
Starting point:
import math
def area(radius: float) -> float:
return math.pi * radius * radius
def greet(name: str) -> str:
return f"Hello, {name}!"
Run:
uv run manasplice splitfunc main.area
Result:
import math
from modules import area
def greet(name: str) -> str:
return f"Hello, {name}!"
"""Auto-generated by ManaSplice from main.py."""
import math
def area(radius: float) -> float:
return math.pi * radius * radius
Installation
For using in a project:
uv add manasplice
or
pip install manasplice
For local development in this repo:
uv sync
To run the CLI without installing it globally:
uv run manasplice --help
Commands
manasplice splitfunc <module>.<function>
manasplice splitall <path/to/file.py>
manasplice splitall --dir <directory>
manasplice splitall --dir <directory> --recursive
manasplice check <module>.<function>
manasplice check <module>.<function> --json
manasplice check <module>.<function> --project-check
manasplice check <path/to/file.py>
manasplice check --dir <directory>
manasplice undo [count]
manasplice config init
manasplice config show
manasplice splitfunc <module>.<function> --preview
manasplice splitfunc <module>.<function> --preview --json
manasplice splitall <path/to/file.py> --preview
manasplice splitall <path/to/file.py> --preview --json
manasplice splitfunc <module>.<function> --validate
manasplice splitall <path/to/file.py> --public-only --exclude main,_*
manasplice splitall <path/to/file.py> --auto-group
manasplice splitall <path/to/file.py> --group area,diameter,circumference --module geometry
manasplice splitfunc <module>.<function> --output-package generated
manasplice splitfunc <module>.<function> --output modules/geometry.py
manasplice splitfunc <module>.<function> --name circle_area
manasplice splitfunc <module>.<function> --into modules/geometry.py
manasplice splitfunc <module>.<function> --format
manasplice splitfunc <module>.<function> --require-clean-git
manasplice splitfunc <module>.<function> --git-commit
manasplice splitfunc <module>.<function> --keep-decorators
manasplice splitfunc <module>.<function> --strip-decorators
manasplice splitmethod <module>.<Class>.<method>
manasplice splitfunc <module>.<function> --force
manasplit paradigm OOP
manasplit paradigm functional <path/to/file.py>
manasplit paradigm event-driven <path/to/file.py>
manasplit paradigm procedural <path/to/file.py>
manasplit paradigm OOP --dir <directory> --recursive
manasplit paradigm OOP <path/to/file.py> --preview
Examples:
uv run manasplice splitfunc main.area
uv run manasplice splitfunc package.utils.normalize_name
uv run manasplice splitall main.py
uv run manasplice splitall --dir app
uv run manasplice splitall --dir app --recursive
uv run manasplice check main.area
uv run manasplice check main.area --project-check
uv run manasplice check main.py --public-only --exclude main,_*
uv run manasplice splitall main.py --preview
uv run manasplice splitall main.py --preview --json
uv run manasplice splitall main.py --public-only --exclude main,_*
uv run manasplice splitall main.py --auto-group
uv run manasplice splitall main.py --group area,diameter,circumference --module geometry
uv run manasplice splitfunc main.area --validate
uv run manasplice splitfunc main.area --output-package generated
uv run manasplice splitfunc main.area --output modules/geometry.py
uv run manasplice splitfunc main.area --into modules/geometry.py
uv run manasplice splitfunc main.area --format
uv run manasplice splitmethod services.UserService.normalize_name
uv run manasplit paradigm OOP
uv run manasplit paradigm functional main.py
uv run manasplit paradigm event-driven main.py
uv run manasplit paradigm procedural main.py
uv run manasplit paradigm OOP main.py --preview
uv run manasplice undo
Configuration
ManaSplice reads [tool.manasplice] from the nearest pyproject.toml above --cwd.
Create a starter config:
manasplice config init
Show the active config:
manasplice config show
manasplice config show --json
Example:
[tool.manasplice]
output_package = "modules"
validate = true
public_only = true
exclude = ["main", "_*"]
recursive = true
format = "ruff"
Notes
- ManaSplice primarily moves top-level functions.
splitmethodsupports conservative class-method extraction by generating a forwarding wrapper and refusing methods without explicitself/cls. paradigm OOPconverts safe top-level functions into@staticmethods on a generated module class and leaves compatibility wrappers in place. With no path, it scans the project recursively while skipping virtualenvs, caches, build output, and__init__.py.paradigm functionaladds a generated functional facade withFUNCTIONAL_API,pipe, andcompose_functionswithout changing existing function definitions.paradigm event-drivenaddsEVENT_HANDLERSanddispatch_eventso functions can be invoked through event names.paradigm proceduralflattens ManaSplice-generated OOP staticmethod classes back into top-level functions while preserving a compatibility class.splitallis literal: it will split every top-level function it finds, including helper functions andmain()if present.- Use
checkfor a no-write safety report without diffs. - Use
--jsonwithcheckor preview commands to get a machine-readable plan instead of human text and colored diffs. - Use
--previewto inspect planned edits with a safety report and unified diffs before ManaSplice writes anything. - Use
--validateto make ManaSplice parse the generated Python source before it writes changes. - Use
--include,--exclude,--public-only, and--recursiveto makesplitallselective instead of splitting every top-level function. - Use
--auto-groupto keep top-level functions that reference each other in the same generated module. - Use
--group ... --module ...when architectural grouping should override call-graph grouping. - Use
--output-packageif you want generated modules somewhere other thanmodules/. - Use
--output,--name, and--intoto control the exact destination module, extracted function name, or append to an existing module. - Use
--keep-decoratorsor--strip-decoratorsto control decorator handling for frameworks with registration side effects. - Use
--formatto runruff formatandruff check --fixon changed Python files after writing. - Use
--require-clean-gitto refuse dirty working trees, or--git-committo create a commit after a successful write. - ManaSplice refuses to overwrite an existing generated module unless you pass
--force. - ManaSplice refuses splits that depend on mutable top-level globals, because copying those globals into a new module changes runtime state.
- ManaSplice now refuses to split functions that participate in a local top-level dependency cycle, such as simple mutual recursion.
checkreports async and generator functions explicitly.- Every non-preview split records rollback history in
.manasplice_history.json, andundoreplays the last recorded operation. - The generated
modules/package is part of the rewritten code, not just scratch output.
Development
Run tests with:
uv run python -m pytest tests --basetemp .pytest_tmp
uv run ruff check src tests
uv run mypy
CONTRIBUTION
Feel free to give feedback and/or suggest changes. This is just meant to be a helpful tool for larger projects made for fun.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file manasplice-0.1.1.2a0.tar.gz.
File metadata
- Download URL: manasplice-0.1.1.2a0.tar.gz
- Upload date:
- Size: 128.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63929db4f6ec9645f46b9f0e72c1f581ea27125ffccbfa85ca03ce465358c31b
|
|
| MD5 |
29b372eaab22b83bf984de516bf0216a
|
|
| BLAKE2b-256 |
2f440e28e6c19f9adf8d8bd19973da5e712bc3726d7d1863add64b7838bcfe96
|
File details
Details for the file manasplice-0.1.1.2a0-py3-none-any.whl.
File metadata
- Download URL: manasplice-0.1.1.2a0-py3-none-any.whl
- Upload date:
- Size: 38.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b179b80d45cbc3afd798e1a8065cb2b188d6eaf6fcaf3b2a74335af2aba357e5
|
|
| MD5 |
b0a97e77553862e88cac9e0e8cb52741
|
|
| BLAKE2b-256 |
6f99aceefcfea1cceea73e4c454338a4e9da87f5ffaf79f8d8fa8227da2690a2
|