GithubHelp home page GithubHelp logo

commis's Introduction

Commis

Create command line applications like Django management commands.

Build Status Coverage Status PyPI version Documentation Status

Pâte de fruit (gominolas) de laranxa sanguina

Read the full documentation at http://commis.readthedocs.org/.

Getting Started

To install the Commis library, the simplest thing to do is use pip as follows:

$ pip install commis

Alternatively, you can download the latest (development) version or clone directly from Github and install it using the setup.py script:

$ git clone https://github.com/bbengfort/commis.git
$ cd commis
$ python setup.py install

Writing Commands

There is a bit more detail in the full documentation, but basically the way you create commands is to subclass the Command class, define its name, help, and args attributes, then implement a handle method. This more or less looks as follows:

class GreetingCommand(Command):

    name    = "greeting"
    help    = "an example command, delivers a greeting"

    args    = {
        # Language selection option
        ('-l', '--lang'): {
            'type': str,
            'default': 'english',
            'metavar': 'LANG',
            'choices': ['english', 'french', 'spanish'],
            'help': 'the language of the greeting',
        },

        # List of one or more name arguments
        'name': {
            'nargs': "+",
            'type': str,
            'help': 'the name to greet you by'
        }
    }

    def handle(self, args):
        """
        Greet each person by name.
        """
        salutation = {
            'french': 'Bonjour',
            'spanish': 'Hola',
            'english': 'Hello',
        }[args.lang.lower()]

        output = []
        for name in args.name:
            output.append("{} {}!".format(salutation, name))
        return "\n".join(output)

The args is a Python dictionary of keyword arguments to be passed to argparse.add_argument and uses the same syntax. See the add_argument method for more details.

The handle method should accept args as its only argument, and should return a string to be printed to the command line.

Writing a Console Program

Once you have your commands, you need to create and define a console utility to execute them. Simply subclass ConsoleProgram with your definition as follows:

VERSION     = "1.0"
DESCRIPTION = "Inspects the mimetype distribution of a directory."
EPILOG      = "Created for scientific purposes and not diagnostic ones."
COMMANDS    = [
    GreetingCommand,
]

class ExampleUtility(ConsoleProgram):

    description = DESCRIPTION
    epilog      = EPILOG
    version     = VERSION

    @classmethod
    def load(klass, commands=COMMANDS):
        utility = klass()
        for command in commands:
            utility.register(command)
        return utility

The ConsoleProgram has a register method which allows you to register various commands. You can do this at run time, creating dynamic utilities with different commands, or you can explore a directory and load all commands from it. Here I just simply add a simple load static method to load the commands and instantiate the utility. Using the utility is then as simple as creating a script as follows:

from example import ExampleUtility

if __name__ == '__main__':
    utility = ExampleUtility.load()
    utility.execute()

For more on writing console utilities and programs, check out the documentation at http://commis.readthedocs.org/.

About

I'm not sure why this doesn't exist yet, but I needed a library for creating command line utilities that wrapped the argparse library. Most of the ones I found used a decorator based syntax; but I really do like the Django management commands style of creating applications. Therefore, this library serves to give you the ability to do so!

Contributing

Commis is open source, and I would be happy to have you contribute! You can contribute in the following ways:

  1. Create a pull request in Github: https://github.com/bbengfort/commis
  2. Add issues or bugs on the bug tracker: https://github.com/bbengfort/commis/issues
  3. Checkout the current dev board on waffle.io: https://waffle.io/bbengfort/commis

Note that labels in the Github issues are defined in the blog post: How we use labels on GitHub Issues at Mediocre Laboratories.

If you've contributed a fair amount, I'll give you direct access to the repository, which is set up in a typical production/release/development cycle as described in A Successful Git Branching Model. A typical workflow is as follows:

  1. Select a card from the dev board - preferably one that is "ready" then move it to "in-progress".

  2. Create a branch off of develop called "feature-[feature name]", work and commit into that branch.

     ~$ git checkout -b feature-myfeature develop
    
  3. Once you are done working (and everything is tested) merge your feature into develop.

     ~$ git checkout develop
     ~$ git merge --no-ff feature-myfeature
     ~$ git branch -d feature-myfeature
     ~$ git push origin develop
    
  4. Repeat. Releases will be routinely pushed into master via release branches, then deployed to the server.

Note that pull requests will be reviewed when the Travis-CI tests pass, so including tests with your pull request is ideal!

You can contact me on Twitter if needed: @bbengfort

Name Origin

com · mis
/ˈkämē,kô-/
noun a junior chef.

Origin
[Latin] committere: 1930s: from French, 'deputy, clerk', past participle of commettre 'entrust', from Latin.<br >

A commis is a basic chef in larger kitchens who works under a chef de partie to learn the station's or range's responsibilities and operation.[3] This may be a chef who has recently completed formal culinary training or is still undergoing training. — Wikipedia

This package is closely related to the Confire configuration tool, hence the name in the same vein — French cooking words. In this case, a commis is someone to whom orders are given (commands) and seemed an appropriate term for a package whose function is accept and execute commands.

Attribution

The photo used in this README, “Pâte de fruit (gominolas) de laranxa sanguina” by Receitasparatodososdias is used under a CC BY-NC-ND 2.0 creative commons license.

Releases

The release versions that are sent to the Python package index are also tagged in Github. You can see the tags through the Github web application and download the tarball of the version you'd like. Additionally PyPI will host the various releases of commis.

The versioning uses a three part version system, "a.b.c" - "a" represents a major release that may not be backwards compatible. "b" is incremented on minor releases that may contain extra features, but are backwards compatible. "c" releases are bug fixes or other micro changes that developers should feel free to immediately update to.

commis's People

Contributors

bbengfort avatar konstantinosx avatar looselycoupled avatar waffle-iron avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

commis's Issues

Tutorial

Create a tutorial to more effectively document how Commis is used!

Documentation

Get the basic example documented and ready to go.

Label Command

A management command which takes one or more arbitrary arguments (labels) on the command line, and does something with each of them.

Rather than implementing handle(), subclasses must implement handle_label(), which will be called once for each label.

Name commis not defined

Traceback (most recent call last):
  File "example-app.py", line 1, in <module>
    from utility import ExampleUtility
  File "/Users/kostasx/Documents/current_workspace/commis/testing/utility.py", line 10, in <module>
    class ExampleUtility(ConsoleProgram):
  File "/Users/kostasx/Documents/current_workspace/commis/testing/utility.py", line 14, in ExampleUtility
    version     = commis.__version__
NameError: name 'commis' is not defined

Tried to run example on the README and wouldn't work unless I hardcoded the version in.

Push to PyPI

Test the setup.py and get the project added to PyPI. (Also record what you're doing!)

Testing

Create some tests for the library!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.