kalekundert / byoc

MIT License
0 stars 0 forks source link

byoc — Build Your Own Config


.. image:: https://img.shields.io/pypi/v/byoc.svg :target: https://pypi.python.org/pypi/byoc

.. image:: https://img.shields.io/pypi/pyversions/byoc.svg :target: https://pypi.python.org/pypi/byoc

.. image:: https://img.shields.io/readthedocs/byoc.svg :target: https://byoc.readthedocs.io/en/latest/?badge=latest

.. image:: https://img.shields.io/github/actions/workflow/status/kalekundert/byoc/test_and_release.yml?branch=master :target: https://github.com/kalekundert/byoc/actions

.. image:: https://img.shields.io/coveralls/kalekundert/byoc.svg :target: https://coveralls.io/github/kalekundert/byoc?branch=master

BYOC is a python library for integrating configuration values from any number/kind of sources, e.g. files, command-line arguments, environment variables, remote JSON APIs, etc. The primary goal of BYOC is to give you complete control over your configuration. This means:

To use BYOC, you would create a class with special attributes (called parameters) that know where to look for configuration values. When these parameters are accessed, the desired values are looked up, possibly merged, possibly cached, and returned. Here's a brief example:

.. code-block:: python

import byoc
from byoc import Key, DocoptConfig, AppDirsConfig

class Greet(byoc.App):
    """
    Say a greeting.

    Usage:
        greet <name> [-g <greeting>]
    """

    # Define which config sources are available to this class.
    __config__ = [
            DocoptConfig,
            AppDirsConfig.setup(name='conf.yml'),
    ]

    # Define how to search for each config value.
    name = byoc.param(
            Key(DocoptConfig, '<name>'),
    )
    greeting = byoc.param(
            Key(DocoptConfig, '-g'),
            Key(AppDirsConfig, 'greeting'),
            default='Hello',
    )

    def main(self):
        self.load(DocoptConfig)
        print(f"{self.greeting}, {self.name}!")

if __name__ == '__main__':
    Greet.entry_point()

We can configure this script from the command line:

.. code-block:: bash

$ ./greet 'Sir Bedevere' Hello, Sir Bedevere! $ ./greet 'Sir Lancelot' -g Goodbye Goodbye, Sir Lancelot!

...or from its config files:

.. code-block:: bash

$ mkdir -p ~/.config/greet $ echo "greeting: Run away" > ~/.config/greet/conf.yml $ greet 'Sir Robin' Run away, Sir Robin!

This example only scratches the surface, but hopefully you can already get a sense for how powerful and flexible these parameters are. For more information, refer to the following examples (in lieu of complete documentation).

Examples

For some examples of byoc being used in real scripts, check out the Stepwise — Molecular Biology__ repository. Almost every script in this repository uses byoc. Below are some particular scripts that might be useful:

Simple scripts:

Long but straight-forward scripts:

Complex scripts:

__ https://github.com/kalekundert/stepwise_mol_bio