Skip to main content

Library for wrapping CLI commands with static configuration.

Project description

Latest PyPI version https://travis-ci.org/modulus-sa/abcmd.svg?branch=master

A library for wrapping shell commands with static configuration.

Usage

The library provides the abcmd.Command ABC that can be used to create shell command wrappers.

Subclassing abcmd.Command requires the following methods to be implemented:

  • run

in addition the following methods are optional and will run only if implemented:

  • dont_run
  • handle_error
  • before_run
  • after_run

The decorator abcmd.error_handler is used to decorate subclass methods which are called only for the matching errors, errors that don’t have specific handlers will fall back to calling the handle_error method if it’s implemented.

Examples

Automating backup of dotfiles

First we subclass Command and describe the procedure:

import datetime
import os

import abcmd

class Backup(abcmd.Command):
    make = 'mkdir {directory}'
    copy = 'cp {files} {directory}'
    sync = 'rsync {directory} {user}@{server}:'

    def run(self, *args, **kwargs):
        os.chdir(os.environ['HOME'])
        self.make()
        self.copy()
        self.sync()

    def dont_run(self, *args, **kwargs):
        # don't run between working hours
        return dt.datetime.now().hour in range(8, 16)

    def handle_error(self, cmd, error):
        # if the backup directory exists ignore the error and continue
        return cmd.startswith('mkdir') and error.endswith('File exists')

then we instantiate with a mapping that is used to render the templates, this will return a callable object that when called will run the procedure:

config = {
    'user': 'laerus',
    'directory': 'dotfiles',
    'files': ['.vimrc', '.bashrc', '.inputrc'],
    'server': '192.168.1.10'
}

runner = Backup(config)
runner()

This would be equivalent with running the following commands:

$ cd ~
$ mkdir dotfiles
$ cp .vimrc .bashrc .inputrc dotfiles
$ rsync dotfiles laerus@192.168.1.10:

Installation

$ pip install abcmd

Compatibility

python3.5+

Licence

MIT

Authors

abcmd was written by Konstantinos Tsakiltzidis.

Project details


Release history Release notifications

This version
History Node

0.3.3

History Node

0.3.2

History Node

0.3.1

History Node

0.3.0

History Node

0.2.4

History Node

0.2.3

History Node

0.2.2

History Node

0.2.1

History Node

0.2.0

History Node

0.1.3

History Node

0.1.2

History Node

0.1.1

History Node

0.1.0

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
abcmd-0.3.3.tar.gz (5.2 kB) Copy SHA256 hash SHA256 Source None Jan 12, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page