Skip to main content

Extra utils for click library

Project description

ClickUtils

Extra utils for click library

Examples

Loading click groups and commands from filepath or directory

Directory structure
_ cwd
 |_ __init__.py (contains code below)
 |_ plugins/
   |_ group_or_command_folder
     |_ *groups (this folder must exists to find click groups or commands)
       |_ __init__.py (any click groups or commands will be pulled from this location)
   ...
import os
import click

from clickutils import click_loader


@click.group()
@click.option('--verbose', '-v', type=bool, is_flag=True, default=False,
    help='Print more verbose output')
def cli(verbose: bool):
    '''click_plugins'''
    click_loader.load(plugins_group, os.path.dirname(os.path.abspath(__file__)), verbose=verbose)

@click.group()
def plugins_group():
    '''click_plugins test plugins'''
    pass

cli.add_command(plugins_group, name='plugins')


if __name__ == '__main__':
    cli()

Same example using decorator

import os
import click

from clickutils import click_loader


@click.group()
def cli(verbose: bool):
    '''click_plugins'''

@click_loader.group(os.path.dirname(os.path.abspath(__file__)), name='plugins')
def plugins_group():
    '''click_plugins test plugins'''
    pass

cli.add_command(plugins_group)


if __name__ == '__main__':
    cli()

Early example of ClickViewset

import click
from clickutils.viewsets import AbstractClickViewset, clickmixins
from clickutils.viewsets import AbstractClickViewset, clickmixins

class UserDictViewset(AbstractClickViewset):
    Name = 'DictionaryViewset'
    Version = '1.0.0'
    Viewset = {'users': ['user1', 'user2', 'user3']}
    commands = ('list', 'version', 'another_command', 'echo')
    hidden_commands = ('echo', )

    @clickmixins.command(name='another_command')
    def another_command(self):
        print('defined another user command that can interact with object (self) which contains Viewset attributes')

    """
    overloading convert function in BaseClickViewset; 
    this is a custom way to add the Viewset into the command class attributes
    """
    def convert(self):
        if isinstance(self.Viewset, dict):
            for key, value in self.Viewset.items():
                setattr(self, key, value)

@UserDictViewset(name='test_command2')
def test_command_group2():
    """Test command2 plugin"""
    pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clickutils-0.1.6.tar.gz (8.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page