Skip to main content

Handle a list of commands that should be executed easily and with undo support.

Project description

pycommands

CircleCI Codecov

Handle a list of commands that should be executed easily and with undo support.

How to use

Install

pycommands is available on PyPI

pip install pycommands

Basic usage

Definition of commands

from commands.base import BaseCommand
from commands.exceptions import CommandException
from commands.invoker import Invoker


class Command1(BaseCommand):
    def build(self):
        return "touch content.txt"

    def build_undo(self):
        return "rm content.txt"


class Command2(BaseCommand):
    def build(self):
        return "mv content.txt content-replaced.txt"

    def build_undo(self):
        return "mv content-replaced.txt content.txt"


class InvalidCommand(BaseCommand):
    def build(self):
        raise CommandException()

Simple default execution with success commands

from commands.invoker import Invoker


invoker = Invoker()

invoker.execute([
    Command1(),
    Command2(),
], run_undo=False)

# output
running command: touch content.txt
running command: mv content.txt content-replaced.txt


# If a invoker.undo() is called then all commands undo operation will be done in the LIFO order.

invoker.undo()

# output
running undo command: mv content-replaced.txt content.txt
running undo command: rm content.txt


# If a invoker.execute() is called with run_undo as True, then the undo operation will be done always
# that a command raise CommandException

invoker = Invoker()

invoker.execute([
    Command1(),
    Command2(),
    InvalidCommand(),
], run_undo=True)

# output
running command: touch content.txt
running command: mv content.txt content-replaced.txt
running command: touch content.txt
running command: mv content.txt content-replaced.txt
running undo command: mv content-replaced.txt content.txt
running undo command: rm content.txt

How to contribute

We welcome contributions of many forms, for example:

  • Code (by submitting pull requests)
  • Documentation improvements
  • Bug reports and feature requests

Setting up for local development

We use pipenv to manage dependencies, so make sure you have it installed.

Creating environment

/<project-path>/pipenv install --python=3

Activing environment

/<project-path>/pipenv shell

Install pre-commit hooks:

pre-commit install

Run tests by evoking pytest:

pytest

That's it! You're ready from development

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycommands-0.1.0.tar.gz (4.0 kB view hashes)

Uploaded source

Built Distribution

pycommands-0.1.0-py3-none-any.whl (6.8 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page