<commit_before>.. image:: ./res/logo.png

*A lightweight application environment checker.*

*Python 3 only. The future is now.*

.. image:: https://travis-ci.org/humangeo/preflyt.svg
   :target: https://travis-ci.org/humangeo/preflyt

.. image:: https://coveralls.io/repos/github/humangeo/preflyt/badge.svg?branch=master
   :target: https://coveralls.io/github/humangeo/preflyt?branch=master

Getting started
--------------------

To use Preflyt, install it :code:`pip install preflyt`, and then invoke it:

.. code-block:: python

    import preflyt

    ok, results = preflyt.check([
        # Assert the presence and value of the $APP_ENV environment variable
        {"checker": "env", "name": "APP_ENV", "value": "production"},

        # Assert that a file at the given path exists
        {"checker": "file", "path": DATA_FILE},
    ])

    if ok:
        print("Preflyt checks passed.")
        run()
    else:
        print("Preflyt checks failed!")
        for result in results:
            if not result["success"]:
                print("{checker[checker]}: {message}".format(**result))


Philosophy
-------------------------

You know what sucks? Kicking off a long running data ingestion/processing task only to discover, near the end, that an external dependency (e.g. webservice, binary) is missing or otherwise inaccessible. "I know what I'll do!" you, the frustrated programmer, exclaims. Choose your own adventure:

* **"I'm going to manually verify things are as they should be before I kick off the task."**

  Congratulations, you just played yourself. Not only do you run the risk of forgetting a checklist item, but now you have to enforce this practice within your team.

* **"I'm going to programatically check things on script start."**

  Getting warm! Hopefully your solution is configuration driven. Even then, what are the odds you wind up with this boilerplate across your scripts?

  .. code-block:: python

      # settings.py<commit_msg>Add checker docs to the README.<commit_after>.. image:: ./res/logo.png

*A lightweight application environment checker.*

*Python 3 only. The future is now.*

.. image:: https://travis-ci.org/humangeo/preflyt.svg
   :target: https://travis-ci.org/humangeo/preflyt

.. image:: https://coveralls.io/repos/github/humangeo/preflyt/badge.svg?branch=master
   :target: https://coveralls.io/github/humangeo/preflyt?branch=master

Getting started
--------------------

To use Preflyt, install it :code:`pip install preflyt`, and then invoke it:

.. code-block:: python

    import preflyt

    ok, results = preflyt.check([
        # Assert the presence and value of the $APP_ENV environment variable
        {"checker": "env", "name": "APP_ENV", "value": "production"},

        # Assert that a file at the given path exists
        {"checker": "file", "path": DATA_FILE},
    ])

    if ok:
        print("Preflyt checks passed.")
        run()
    else:
        print("Preflyt checks failed!")
        for result in results:
            if not result["success"]:
                print("{checker[checker]}: {message}".format(**result))


Checkers
---------

Out of the box, the following checkers are available.

| Name | Description | Args |
|------|-------------|------|
| env | Check environment state | <ul><li><strong>name</strong>: Variable name</li><li><strong>value</strong>: (optional) Variable value</li></ul> |
| dir | Check directory state | <ul><li><strong>path</strong>: Directory path</li><li><strong>present</strong>: (optional, default=True) False if the directory should be absent.</li></ul> |
| file | Check file state | <ul><li><strong>path</strong>: File path</li><li><strong>present</strong>: (optional, default=True) False if the file should be absent.</li></ul> |
| psql | Check PostgreSQL state | <ul><li><strong>host</strong>: (optional, default=localhost) Postgres hostname</li><li><strong>port</strong>: (optional, default=5432) Postgres server port</li><li><strong>dbname</strong>: (optional, default=None) The database name for which to test</li></ul> |
| web | Check web service state | <ul><li><strong>url</strong>: The webservice URL</li><li><strong>statuses</strong>: (optional, default=None) A collection of acceptable status codes (aside from 200).</li></ul> |

Future versions of Preflyt will add additional default checkers while allowing third parties to ship their own.

Philosophy
-------------------------

You know what sucks? Kicking off a long running data ingestion/processing task only to discover, near the end, that an external dependency (e.g. webservice, binary) is missing or otherwise inaccessible. "I know what I'll do!" you, the frustrated programmer, exclaims. Choose your own adventure:

* **"I'm going to manually verify things are as they should be before I kick off the task."**

  Congratulations, you just played yourself. Not only do you run the risk of forgetting a checklist item, but now you have to enforce this practice within your team.

* **"I'm going to programatically check things on script start."**

  Getting warm! Hopefully your solution is configuration driven. Even then, what are the odds you wind up with this boilerplate across your scripts?

  .. code-block:: python

      # settings.py
{'fraction_non_alphanumeric': 0.14263891540958565, 'fraction_numerical': 0.00171854114951308, 'mean_word_length': 3.409090909090909, 'pattern_counts': {'":': 10, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1810694', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1432, 'n_words': 553}