Skip to content

Repository files navigation

MoleculerPy REPL

Interactive CLI shell for MoleculerPy microservices framework.

CI PyPI version Python versions License


Features

  • πŸ–₯️ Interactive Shell β€” Command-line interface for MoleculerPy brokers
  • πŸ“ž Call Actions β€” Invoke service actions with params
  • πŸ“¨ Emit Events β€” Publish events to the cluster
  • πŸ” Inspect Cluster β€” List actions, services, nodes, events
  • ⚑ Tab Completion β€” Autocomplete for actions, events, nodes
  • 🎨 Rich Output β€” Beautiful tables and colors (optional)
  • πŸ”§ Extensible β€” Add custom commands easily

Quick Start

Installation

cd sources/moleculerpy-repl
pip install -e .

# Optional: rich for better output
pip install rich

Usage with Mock Broker

python3 examples/demo_repl.py

Usage with MoleculerPy

import asyncio
from moleculerpy import Broker
from moleculerpy_repl import REPL

async def main():
    broker = Broker("node-1", transporter="nats://localhost:4222")
    await broker.start()

    # Start REPL
    repl = REPL(broker)
    await repl.run()

asyncio.run(main())

Commands

Command Alias Description Example
actions a List available actions actions
services s List registered services services
nodes n List cluster nodes nodes -d
events ev List event subscriptions events
info i System information info
call c Call a service action call math.add a=5 b=3
emit e Emit an event emit user.created name=Bob
broadcast b Broadcast event to all broadcast system.shutdown
help β€” Show help help call
clear cls Clear screen clear
quit exit Exit REPL quit

Parameter Syntax

# Basic parameters β†’ payload
mol $ call math.add a=5 b=3

# Meta parameters (prefix #) β†’ context metadata
mol $ emit user.created name=Bob #userId=123 #source=api

# Options (prefix $) β†’ call options
mol $ call slow.action $timeout=30000 $retries=3

# JSON parameters
mol $ call user.create --json '{"name": "Bob", "age": 30}'

# Load from file
mol $ call user.create --load params.json

# Force string with @ prefix
mol $ call search query=@123  # "123" as string, not int

Example Session

$ python3 examples/demo_repl.py

πŸš€ MoleculerPy REPL v0.1.0
   Node ID: demo-node
   Type 'help' for available commands.

demo $ services
Services:
Service                        Version    State      Nodes
------------------------------------------------------------
greeter                        1          OK         1
math                           1          OK         1
user                           1          OK         1

demo $ actions
Actions:
Action                                   Nodes      State
------------------------------------------------------------
greeter.hello                            1          OK
math.add                                 1          OK
math.multiply                            1          OK
user.get                                 1          OK
user.list                                1          OK

demo $ call math.add a=10 b=25
>> Response (0.00ms):
Result: 35

demo $ call user.list
>> Response (0.00ms):
[
  {"id": "u1", "name": "John"},
  {"id": "u2", "name": "Jane"}
]

demo $ emit user.created name=Charlie #userId=u4
[EVENT] user.created: {'name': 'Charlie'}
Event emitted: user.created

demo $ info
Broker Information:
----------------------------------------
  Node ID:         demo-node
  Namespace:       (none)
  Version:         0.1.0
  Protocol:        4

Transport:
----------------------------------------
  Transporter:     None
  Serializer:      JSON

Statistics:
----------------------------------------
  Services:        3
  Actions:         5
  Events:          1

demo $ quit
Goodbye! πŸ‘‹

Custom Commands

from moleculerpy_repl import REPL, BaseCommand, CommandResult

class HelloCommand(BaseCommand):
    name = "hello"
    description = "Say hello"
    usage = "hello [name]"
    aliases = ["hi"]

    async def execute(self, broker, args):
        name = args.positional[0] if args.positional else "World"
        return CommandResult(
            success=True,
            output=f"Hello, {name}!"
        )

repl = REPL(broker, custom_commands=[HelloCommand])

Architecture

moleculerpy-repl/
β”œβ”€β”€ src/moleculerpy_repl/
β”‚   β”œβ”€β”€ __init__.py          # Public API: REPL, BaseCommand, etc.
β”‚   β”œβ”€β”€ repl.py              # Main REPL class (extends cmd.Cmd)
β”‚   β”œβ”€β”€ parser.py            # Argument parser with prefix system
β”‚   β”œβ”€β”€ output.py            # Formatter (uses rich if available)
β”‚   └── commands/
β”‚       β”œβ”€β”€ base.py          # BaseCommand, CommandResult, Registry
β”‚       β”œβ”€β”€ actions.py       # `actions` command
β”‚       β”œβ”€β”€ services.py      # `services` command
β”‚       β”œβ”€β”€ nodes.py         # `nodes` command
β”‚       β”œβ”€β”€ events.py        # `events` command
β”‚       β”œβ”€β”€ info.py          # `info` command
β”‚       β”œβ”€β”€ call.py          # `call` command
β”‚       └── emit.py          # `emit`, `broadcast` commands
β”œβ”€β”€ examples/
β”‚   └── demo_repl.py         # Demo with mock broker
└── docs/
    β”œβ”€β”€ PLAN.md              # Implementation status
    β”œβ”€β”€ ARCHITECTURE.md
    └── COMMANDS.md

Documentation


Development Status

Phase Description Status
Phase 1 Core REPL module βœ… Complete
Phase 2 Discovery commands βœ… Complete
Phase 3 Action commands βœ… Complete
Phase 4 Advanced commands ⏳ Pending
Phase 5 MoleculerPy integration ⏳ Pending
Phase 6 DX polish βœ… Partial

License

MIT License β€” see LICENSE for details.


Acknowledgments

Inspired by moleculer-repl for Moleculer.js.

About

No description, website, or topics provided.

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages