Usage

Install

Install with pip:

pip install precept

Write async console applications

Precept comes with many classes and functions to build async applications, the main class consist of Precept which you subclass to create your application. Methods of this class decorated with Command are automatically added as sub command to the application.

Basic example defining an echo command:

from precept import Precept, Command, Argument

class App(Precept):
    @Command(
        Argument('name'),
    )
    async def echo(self, name):
        print(f'Hello {name}')

Then call from the terminal like this: app echo bob -> Prints Hello bob

Note

If no command was supplied, main will be called instead.

Starting the application

To create a console application from a precept app you need to add function that will create an instance of your precept subclass and call start then add it to setup.py.

app.py
def cli():
    App().start()
setup.py
from setuptools import setup

setup(
    entry_points: {
        'console_scripts': ['cli = app:cli']
    }
)

You can also create a global instance of the app and assign the entrypoint to it’s start method

Configs

Precept comes with a built in config system, create a subclass of Config with members as config. You can nest classes definition to create sub sections.

Example

from precept import Config, Nestable, ConfigProperty

class MyConfig(Config):
    my_config = ConfigProperty(comment='comment', config_type=str)

    class SubConfig(Nestable):
        nested = ConfigProperty(default='Default')

    sub_config: SubConfig  # A class member will be auto created.

Then you use it in the precept class like so:

from precept import Precept

class MyApp(Precept):
    config = MyConfig()

Config file

To use the config with files, add a config_file argument to precept init:

from precept import Precept

class MyApp(Precept):
    def __init__(self):
        super().__init__(
            config_file='config.yml',
        )

Precept will automatically add a --config-file global argument for the user to override.

It will also add a dump-config command to dump the default config for first use.

Note

The config_file argument can also be a list, in which case the first file found will be used.

Config format

Precept can read and write three config format, default being yaml:

ConfigFormat

  • yaml

  • ini

  • json, doesn’t support comments.

See also

Concepts