kids.sh


Namekids.sh JSON
Version 0.0.8 PyPI version JSON
download
home_pagehttp://github.com/0k/kids.sh
SummaryKids shell command call wrapper
upload_time2018-04-09 13:28:47
maintainer
docs_urlNone
authorValentin Lab
requires_python
licenseBSD 3-Clause License
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            =========================
kids.sh
=========================


.. image:: http://img.shields.io/pypi/v/kids.sh.svg?style=flat
   :target: https://pypi.python.org/pypi/kids.sh/
   :alt: Latest PyPI version

.. image:: http://img.shields.io/travis/0k/kids.sh/master.svg?style=flat
   :target: https://travis-ci.org/0k/kids.sh/
   :alt: Travis CI build status

.. image:: http://img.shields.io/coveralls/0k/kids.sh/master.svg?style=flat
   :target: https://coveralls.io/r/0k/kids.sh
   :alt: Test coverage


``kids.sh`` is a Python library providing helpers when calling shell
commands thanks to python. It's part of 'Kids' (for Keep It Dead Simple)
library.


Maturity
========

This is Alpha release. More a place to store common librairies. Will
perhaps evolve into something more consistent.

It is, for now, a very humble package.


Features
========

using ``kids.sh``:

- Call ``wrap()`` when you want to call a system command and you don't
  have to bother about subprocess and other stuff. You get the standard
  output of the command as the return string.

These assumptions are in the code:

- You don't want to deal with precise subprocess things, don't really need to
  care about security (because system command you launch are hard-written).
- You don't need asynchronous code.


Installation
============

You don't need to download the GIT version of the code as ``kids.sh`` is
available on the PyPI. So you should be able to run::

    pip install kids.sh

If you have downloaded the GIT sources, then you could add install
the current version via traditional::

    python setup.py install

And if you don't have the GIT sources but would like to get the latest
master or branch from github, you could also::

    pip install git+https://github.com/0k/kids.sh

Or even select a specific revision (branch/tag/commit)::

    pip install git+https://github.com/0k/kids.sh@master


Usage
=====


More documentation is available in the code.


wrap
----

If command doesn't fail, standard output is returned::

    >>> from __future__ import print_function

    >>> from kids.sh import wrap
    >>> print(wrap('test "$HELLO" && echo "foo" || echo "bar"'))
    bar


But if command fails, then a special ``ShellError`` exception is cast::

    >>> wrap('test "$HELLO" && echo "foo" || { echo "bar" ; false ; }')
    Traceback (most recent call last):
    ...
    ShellError: Wrapped command returned with unexpected errorlevel.
      command: 'test "$HELLO" && echo "foo" || { echo "bar" ; false ; }'
      errlvl: 1
      stdout:
      | bar

If you provide a list instead of a string, no shell will be used to
interpret your command: the process and arguments will be sent
directly to the system::

    >>> wrap(["/bin/cat", "/tmp/should-not-exist-file-xxxx"])
    Traceback (most recent call last):
    ...
    ShellError: Wrapped command returned with unexpected errorlevel.
      command: ['/bin/cat', '/should-not-exist-file-xxxx']
      errlvl: 1
      stderr:
      | /bin/cat: /tmp/should-not-exist-file-xxxx: No such file or directory

Notice:

- that a ``wrap(..)`` command will strip output (remove whitespaces
  and newlines from the beginning and the ending of the output). If
  you don't want this to happen, you can provide ``strip=False``.

    >>> from kids.sh import wrap
    >>> print("[%s]" % wrap('echo "  foo   "'))
    [foo]
    >>> print("[%s]" % wrap('echo "  foo   "', strip=False))
    [  foo   ]

- ``wrap(..)`` support nice exception message even when handling
  multi-line content (typically used for shell scripting)::

    >>> from kids.sh import wrap
    >>> print(wrap('''
    ...    if [ "bar" ]; then
    ...       echo "foo"
    ...       exit 3
    ...    else
    ...       exit 4
    ...    fi
    ... '''))
    Traceback (most recent call last):
    ...
    ShellError: Wrapped command returned with unexpected errorlevel.
      command:
      |
      |    if [ "bar" ]; then
      |       echo "foo"
      |       exit 3
      |    else
      |       exit 4
      |    fi
      |
      errlvl: 3
      stdout:
      | foo


cmd
---

If you would rather want to get all information from the command, you can
use ``cmd``::

    >>> from kids.sh import cmd

    >>> cmd('test "$HELLO" && echo "foo" || { echo "bar" ; false ; }')
    ShellOutput(out=...'bar\n', err=...'', errlvl=1)

So, notice it doesn't cast any exception, but outputs a named tuple.

``cmd(..)`` also support handling a list of arguments instead of a
command string if you want to bypass shell interpretation::

    >>> cmd(['/bin/cat', '/file-does-not-exist-xxxx'])
    ShellOutput(out=...'', err=...'...', errlvl=1)


Contributing
============

Any suggestion or issue is welcome. Push request are very welcome,
please check out the guidelines.


Push Request Guidelines
-----------------------

You can send any code. I'll look at it and will integrate it myself in
the code base and leave you as the author. This process can take time and
it'll take less time if you follow the following guidelines:

- check your code with PEP8 or pylint. Try to stick to 80 columns wide.
- separate your commits per smallest concern.
- each commit should pass the tests (to allow easy bisect)
- each functionality/bugfix commit should contain the code, tests,
  and doc.
- prior minor commit with typographic or code cosmetic changes are
  very welcome. These should be tagged in their commit summary with
  ``!minor``.
- the commit message should follow gitchangelog rules (check the git
  log to get examples)
- if the commit fixes an issue or finished the implementation of a
  feature, please mention it in the summary.

If you have some questions about guidelines which is not answered here,
please check the current ``git log``, you might find previous commit that
would show you how to deal with your issue.


License
=======

Copyright (c) 2018 Valentin Lab.

Licensed under the `BSD License`_.

.. _BSD License: http://raw.github.com/0k/kids.sh/master/LICENSE

Changelog
=========


0.0.8 (2018-04-09)
------------------

Changes
~~~~~~~
- Removing ``swrap(..)`` in favor of ``strip=True`` keyword argument of
  ``wrap(..)``. [Valentin Lab]


0.0.7 (2018-04-09)
------------------

New
~~~
- Providing list of arguments to ``wrap``, ``swrap``, ``cmd`` allows to
  bypass shell. [Valentin Lab]
- More docs about ``wrap(..)` and ``swrap(..)`` [Valentin Lab]

Fix
~~~
- Fixed typo. [Valentin Lab]


0.0.6 (2015-03-12)
------------------

New
~~~
- ``ShellError`` handles full output of failing shell command call.
  [Valentin Lab]

Fix
~~~
- [sh] ``set_env`` wouldn't properly set the environment variables.
  [Valentin Lab]


0.0.5 (2015-02-06)
------------------

Fix
~~~
- Restructured to avoid package ``.tests``. [Valentin Lab]


0.0.2 (2015-02-06)
------------------

New
~~~
- Added doc on ``cmd`` command. [Valentin Lab]
- Provide testing facilities. [Valentin Lab]

  - introduction of ``set_env``
  - unittest base class ``BaseShTest``
  - using a namedtuple for truple from ``cmd``


0.0.1 (2014-05-13)
------------------
- First import. [Valentin Lab]




            

Raw data

            {
    "maintainer": "", 
    "docs_url": null, 
    "requires_python": "", 
    "maintainer_email": "", 
    "cheesecake_code_kwalitee_id": null, 
    "keywords": "", 
    "upload_time": "2018-04-09 13:28:47", 
    "author": "Valentin Lab", 
    "home_page": "http://github.com/0k/kids.sh", 
    "github_user": "0k", 
    "download_url": "https://pypi.python.org/packages/f4/ab/f2aba5c11dbfe0030ccfd896754373c8180cba609d46ae60bcf7bfc1d9ce/kids.sh-0.0.8.tar.gz", 
    "platform": "", 
    "version": "0.0.8", 
    "cheesecake_documentation_id": null, 
    "description": "=========================\nkids.sh\n=========================\n\n\n.. image:: http://img.shields.io/pypi/v/kids.sh.svg?style=flat\n   :target: https://pypi.python.org/pypi/kids.sh/\n   :alt: Latest PyPI version\n\n.. image:: http://img.shields.io/travis/0k/kids.sh/master.svg?style=flat\n   :target: https://travis-ci.org/0k/kids.sh/\n   :alt: Travis CI build status\n\n.. image:: http://img.shields.io/coveralls/0k/kids.sh/master.svg?style=flat\n   :target: https://coveralls.io/r/0k/kids.sh\n   :alt: Test coverage\n\n\n``kids.sh`` is a Python library providing helpers when calling shell\ncommands thanks to python. It's part of 'Kids' (for Keep It Dead Simple)\nlibrary.\n\n\nMaturity\n========\n\nThis is Alpha release. More a place to store common librairies. Will\nperhaps evolve into something more consistent.\n\nIt is, for now, a very humble package.\n\n\nFeatures\n========\n\nusing ``kids.sh``:\n\n- Call ``wrap()`` when you want to call a system command and you don't\n  have to bother about subprocess and other stuff. You get the standard\n  output of the command as the return string.\n\nThese assumptions are in the code:\n\n- You don't want to deal with precise subprocess things, don't really need to\n  care about security (because system command you launch are hard-written).\n- You don't need asynchronous code.\n\n\nInstallation\n============\n\nYou don't need to download the GIT version of the code as ``kids.sh`` is\navailable on the PyPI. So you should be able to run::\n\n    pip install kids.sh\n\nIf you have downloaded the GIT sources, then you could add install\nthe current version via traditional::\n\n    python setup.py install\n\nAnd if you don't have the GIT sources but would like to get the latest\nmaster or branch from github, you could also::\n\n    pip install git+https://github.com/0k/kids.sh\n\nOr even select a specific revision (branch/tag/commit)::\n\n    pip install git+https://github.com/0k/kids.sh@master\n\n\nUsage\n=====\n\n\nMore documentation is available in the code.\n\n\nwrap\n----\n\nIf command doesn't fail, standard output is returned::\n\n    >>> from __future__ import print_function\n\n    >>> from kids.sh import wrap\n    >>> print(wrap('test \"$HELLO\" && echo \"foo\" || echo \"bar\"'))\n    bar\n\n\nBut if command fails, then a special ``ShellError`` exception is cast::\n\n    >>> wrap('test \"$HELLO\" && echo \"foo\" || { echo \"bar\" ; false ; }')\n    Traceback (most recent call last):\n    ...\n    ShellError: Wrapped command returned with unexpected errorlevel.\n      command: 'test \"$HELLO\" && echo \"foo\" || { echo \"bar\" ; false ; }'\n      errlvl: 1\n      stdout:\n      | bar\n\nIf you provide a list instead of a string, no shell will be used to\ninterpret your command: the process and arguments will be sent\ndirectly to the system::\n\n    >>> wrap([\"/bin/cat\", \"/tmp/should-not-exist-file-xxxx\"])\n    Traceback (most recent call last):\n    ...\n    ShellError: Wrapped command returned with unexpected errorlevel.\n      command: ['/bin/cat', '/should-not-exist-file-xxxx']\n      errlvl: 1\n      stderr:\n      | /bin/cat: /tmp/should-not-exist-file-xxxx: No such file or directory\n\nNotice:\n\n- that a ``wrap(..)`` command will strip output (remove whitespaces\n  and newlines from the beginning and the ending of the output). If\n  you don't want this to happen, you can provide ``strip=False``.\n\n    >>> from kids.sh import wrap\n    >>> print(\"[%s]\" % wrap('echo \"  foo   \"'))\n    [foo]\n    >>> print(\"[%s]\" % wrap('echo \"  foo   \"', strip=False))\n    [  foo   ]\n\n- ``wrap(..)`` support nice exception message even when handling\n  multi-line content (typically used for shell scripting)::\n\n    >>> from kids.sh import wrap\n    >>> print(wrap('''\n    ...    if [ \"bar\" ]; then\n    ...       echo \"foo\"\n    ...       exit 3\n    ...    else\n    ...       exit 4\n    ...    fi\n    ... '''))\n    Traceback (most recent call last):\n    ...\n    ShellError: Wrapped command returned with unexpected errorlevel.\n      command:\n      |\n      |    if [ \"bar\" ]; then\n      |       echo \"foo\"\n      |       exit 3\n      |    else\n      |       exit 4\n      |    fi\n      |\n      errlvl: 3\n      stdout:\n      | foo\n\n\ncmd\n---\n\nIf you would rather want to get all information from the command, you can\nuse ``cmd``::\n\n    >>> from kids.sh import cmd\n\n    >>> cmd('test \"$HELLO\" && echo \"foo\" || { echo \"bar\" ; false ; }')\n    ShellOutput(out=...'bar\\n', err=...'', errlvl=1)\n\nSo, notice it doesn't cast any exception, but outputs a named tuple.\n\n``cmd(..)`` also support handling a list of arguments instead of a\ncommand string if you want to bypass shell interpretation::\n\n    >>> cmd(['/bin/cat', '/file-does-not-exist-xxxx'])\n    ShellOutput(out=...'', err=...'...', errlvl=1)\n\n\nContributing\n============\n\nAny suggestion or issue is welcome. Push request are very welcome,\nplease check out the guidelines.\n\n\nPush Request Guidelines\n-----------------------\n\nYou can send any code. I'll look at it and will integrate it myself in\nthe code base and leave you as the author. This process can take time and\nit'll take less time if you follow the following guidelines:\n\n- check your code with PEP8 or pylint. Try to stick to 80 columns wide.\n- separate your commits per smallest concern.\n- each commit should pass the tests (to allow easy bisect)\n- each functionality/bugfix commit should contain the code, tests,\n  and doc.\n- prior minor commit with typographic or code cosmetic changes are\n  very welcome. These should be tagged in their commit summary with\n  ``!minor``.\n- the commit message should follow gitchangelog rules (check the git\n  log to get examples)\n- if the commit fixes an issue or finished the implementation of a\n  feature, please mention it in the summary.\n\nIf you have some questions about guidelines which is not answered here,\nplease check the current ``git log``, you might find previous commit that\nwould show you how to deal with your issue.\n\n\nLicense\n=======\n\nCopyright (c) 2018 Valentin Lab.\n\nLicensed under the `BSD License`_.\n\n.. _BSD License: http://raw.github.com/0k/kids.sh/master/LICENSE\n\nChangelog\n=========\n\n\n0.0.8 (2018-04-09)\n------------------\n\nChanges\n~~~~~~~\n- Removing ``swrap(..)`` in favor of ``strip=True`` keyword argument of\n  ``wrap(..)``. [Valentin Lab]\n\n\n0.0.7 (2018-04-09)\n------------------\n\nNew\n~~~\n- Providing list of arguments to ``wrap``, ``swrap``, ``cmd`` allows to\n  bypass shell. [Valentin Lab]\n- More docs about ``wrap(..)` and ``swrap(..)`` [Valentin Lab]\n\nFix\n~~~\n- Fixed typo. [Valentin Lab]\n\n\n0.0.6 (2015-03-12)\n------------------\n\nNew\n~~~\n- ``ShellError`` handles full output of failing shell command call.\n  [Valentin Lab]\n\nFix\n~~~\n- [sh] ``set_env`` wouldn't properly set the environment variables.\n  [Valentin Lab]\n\n\n0.0.5 (2015-02-06)\n------------------\n\nFix\n~~~\n- Restructured to avoid package ``.tests``. [Valentin Lab]\n\n\n0.0.2 (2015-02-06)\n------------------\n\nNew\n~~~\n- Added doc on ``cmd`` command. [Valentin Lab]\n- Provide testing facilities. [Valentin Lab]\n\n  - introduction of ``set_env``\n  - unittest base class ``BaseShTest``\n  - using a namedtuple for truple from ``cmd``\n\n\n0.0.1 (2014-05-13)\n------------------\n- First import. [Valentin Lab]\n\n\n\n", 
    "lcname": "kids.sh", 
    "bugtrack_url": null, 
    "github": true, 
    "coveralls": false, 
    "name": "kids.sh", 
    "license": "BSD 3-Clause License", 
    "travis_ci": true, 
    "github_project": "kids.sh", 
    "summary": "Kids shell command call wrapper", 
    "split_keywords": [], 
    "author_email": "valentin.lab@kalysto.org", 
    "urls": [
        {
            "has_sig": false, 
            "upload_time": "2018-04-09T13:28:44", 
            "comment_text": "", 
            "python_version": "py2.py3", 
            "url": "https://pypi.python.org/packages/40/ba/7c1524320d5e063f51e46b2ff97f50fc56dfa24673b153e4f23664dc2292/kids.sh-0.0.8-py2.py3-none-any.whl", 
            "md5_digest": "edef4c51cb2da19f0c7e961e99480bd9", 
            "downloads": 0, 
            "filename": "kids.sh-0.0.8-py2.py3-none-any.whl", 
            "packagetype": "bdist_wheel", 
            "path": "40/ba/7c1524320d5e063f51e46b2ff97f50fc56dfa24673b153e4f23664dc2292/kids.sh-0.0.8-py2.py3-none-any.whl", 
            "digests": {
                "sha256": "860d9a5178cec6925bad5b7c0281be6dbe5e58917cc1714deccbc56274e09a14", 
                "md5": "edef4c51cb2da19f0c7e961e99480bd9"
            }, 
            "sha256_digest": "860d9a5178cec6925bad5b7c0281be6dbe5e58917cc1714deccbc56274e09a14", 
            "size": 12013
        }, 
        {
            "has_sig": false, 
            "upload_time": "2018-04-09T13:28:47", 
            "comment_text": "", 
            "python_version": "source", 
            "url": "https://pypi.python.org/packages/f4/ab/f2aba5c11dbfe0030ccfd896754373c8180cba609d46ae60bcf7bfc1d9ce/kids.sh-0.0.8.tar.gz", 
            "md5_digest": "64ad358663c41e83b15c547024ce3c8d", 
            "downloads": 0, 
            "filename": "kids.sh-0.0.8.tar.gz", 
            "packagetype": "sdist", 
            "path": "f4/ab/f2aba5c11dbfe0030ccfd896754373c8180cba609d46ae60bcf7bfc1d9ce/kids.sh-0.0.8.tar.gz", 
            "digests": {
                "sha256": "a0b3572b5ba65cb2e8f61148fb3eba73e6887ed0770b4a6184c42f7a8282719f", 
                "md5": "64ad358663c41e83b15c547024ce3c8d"
            }, 
            "sha256_digest": "a0b3572b5ba65cb2e8f61148fb3eba73e6887ed0770b4a6184c42f7a8282719f", 
            "size": 8622
        }
    ], 
    "_id": null, 
    "cheesecake_installability_id": null
}