Project description

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:

importdatetimeimportosimportabcmdclassBackup(abcmd.Command):make='mkdir {directory}'copy='cp {files} {directory}'sync='rsync {directory} {user}@{server}:'defrun(self,*args,**kwargs):os.chdir(os.environ['HOME'])self.make()self.copy()self.sync()defdont_run(self,*args,**kwargs):# don't run between working hoursreturndt.datetime.now().hourinrange(8,16)defhandle_error(self,cmd,error):# if the backup directory exists ignore the error and continuereturncmd.startswith('mkdir')anderror.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: