Core Modules
Core modules live in portion/core/ and are all singletons — call the class to get the shared instance from anywhere in the codebase.
Terminal — portion/core/terminal.py
Wraps Rich and questionary for all terminal output and user interaction. Respects the global verbose flag from cli_state.
from portion.core.terminal import Terminal
terminal = Terminal()
terminal.info("Template downloaded.")
Method |
Purpose |
|---|---|
info(msg, **kwargs) |
Print an informational message |
warn(msg, **kwargs) |
Print a warning |
error(msg, **kwargs) |
Print an error message |
prompt(msg, **kwargs) |
Ask the user for a yes/no confirmation and return the answer |
pulse(msg, **kwargs) |
Print a verbose log message (only shown when verbose is enabled) |
choose(msg, choices, **kwargs) |
Show an interactive selection menu and return the chosen item |
print(message) |
Print a Rich renderable directly to the console |
TemplateManager — portion/core/template_manager.py
Handles all template file operations. Templates are stored in the OS user data directory via platformdirs.
from portion.core.template_manager import TemplateManager
tm = TemplateManager()
tm.download_template("https://github.com/org/my-template")
Method |
Purpose |
|---|---|
download_template(link) |
Git-clone a template from a URL |
copy_template(name, project_name) |
Copy base/ into a new project directory |
copy_portion(template, portion_path, dest) |
Copy a file from .portions/ into the project |
read_configuration(name) |
Load and parse a template's .pyportion.yml |
delete_template(name) |
Delete a template from disk |
ProjectManager — portion/core/project_manager.py
Handles all project file operations. Uses RedBaron for AST-based Python file modification, which means it parses the actual Python syntax tree rather than doing raw text replacement.
from portion.core.project_manager import ProjectManager
pm = ProjectManager()
pm.add_import(["cli", "__init__.py"], "from .my_cmd import my_cmd")
Method |
Purpose |
|---|---|
initialize_project(path, name) |
Create a .pyportion.yml in the project |
read_configuration(path) |
Load and parse the project's .pyportion.yml |
replace_in_file(path, replacements) |
Do text substitution inside a file |
add_import(path, import_statement) |
Append an import to a Python file |
add_to_list(path, list_name, value) |
Append a value to a Python list variable |