def test_summary(self): target = CliApplication(tests.sample_app, name='testing') assert target.application_summary == 'testing version 1.2.3' target = CliApplication(tests.sample_app, name='testing', description='This is a test') assert target.application_summary == 'testing version 1.2.3 - This is a test'
def test_loading_logging(self): import logging target = CliApplication( tests.sample_app, application_settings='tests.sample_app.logging_settings') target.dispatch(args=('--log-level', 'WARN', 'settings')) assert logging.root.level == logging.WARN
def test_loading_logging(self): import logging target = CliApplication( tests.sample_app, application_settings="tests.sample_app.logging_settings") target.dispatch(args=("--log-level", "WARN", "settings")) assert logging.root.level == logging.WARN
def test_env_key_settings(self): target = CliApplication(tests.sample_app) assert target.env_loglevel_key == 'PYAPP_LOGLEVEL' assert target.env_settings_key == 'PYAPP_SETTINGS' target = CliApplication(tests.sample_app, env_loglevel_key='MYAPP_LOGLEVEL', env_settings_key='MYAPP_SETTINGS') assert target.env_loglevel_key == 'MYAPP_LOGLEVEL' assert target.env_settings_key == 'MYAPP_SETTINGS'
def test_dispatch_args(self): closure = {} target = CliApplication(tests.sample_app) @target.register_handler(cli_name='sample') @add_argument('--foo', dest='foo') def sample_handler(opts): closure['opts'] = opts target.dispatch(args=('sample', '--foo', 'bar')) assert closure['opts'].foo == 'bar'
def test_dispatch_args(self): closure = {} target = CliApplication(tests.sample_app) @target.command(name="sample") @argument("--foo", dest="foo") def sample_handler(opts): closure["opts"] = opts target.dispatch(args=("sample", "--foo", "bar")) assert closure["opts"].foo == "bar"
def test_initialisation_alternate_settings(self): target = CliApplication(tests.sample_app, application_settings='tests.runtime_settings') assert target.application_settings == 'tests.runtime_settings'
def test_initialisation(self): target = CliApplication(tests.sample_app) assert target.root_module is tests.sample_app assert target.application_settings == 'tests.sample_app.default_settings' assert len(target._handlers) == 3
from pyapp import checks from pyapp import feature_flags from pyapp.app import CliApplication import tests.sample_app app = CliApplication(tests.sample_app) @app.command def happy(opts): print("=o)") @app.command def sad(opts): print("=o(") return -2 @app.command def cheeky(opts): print("=oD") raise KeyboardInterrupt() @app.command def angry(opts): print(">=o(") raise Exception("Grrrr")
# -*- coding: utf-8 -*- """ CLI ~~~ Retort contains a CLI for common functions """ from pyapp.app import CliApplication, add_argument import retort app = CliApplication(retort, "retort") @app.command(cli_name='runserver') @add_argument('HANDLER', help='Module that contains your request handler.') @add_argument('--host', dest='host', help='Hostname to bind server to.') @add_argument('--port', type=int, dest='port', help='Port to bind server to.') @add_argument('--noreload', action='store_true', dest='no_reload', help='Disable automatic reloading.') @add_argument('--debug', action='store_true', dest='debug', help='Enable debugging.') def run_server(args): import retort.server retort.server.run_server(args.HANDLER, args.host, args.port, args.no_reload, args.debug)
""" CLI Interface ~~~~~~~~~~~~~ """ from pyapp.app import Arg from pyapp.app import CliApplication from pyapp.app.argument_actions import KeyValueAction from pyapp.conf import settings APP = CliApplication(prog="sample", description="Sample pyApp application.") @APP.command def foo_do(*, verbose: bool = False): """ Perform a foo operation. """ # Use a command line argument if verbose: print("Doing foo verbosely!") else: print("Doing foo.") # Print a message from the settings file print(settings.FOO_MESSAGE) class BarGroup: group = APP.create_command_group("bar", aliases="b")
def test_repr(self): target = CliApplication(tests.sample_app, prog="testing") assert repr(target) == "CliApplication(<module tests.sample_app>)"
def test_summary(self, kwargs, expected): target = CliApplication(tests.sample_app, **kwargs) assert str(target) == expected
def test_env_settings_key(self, kwargs, expected): target = CliApplication(tests.sample_app, **kwargs) assert target.env_settings_key == expected
""" Main CLI ~~~~~~~~ Definition of {{ cookiecutter.project_name }} CLI. """ from pyapp.app import CliApplication, argument, CommandOptions APP = CliApplication() main = APP.dispatch @APP.command @argument("-n", "--name", default="Dave") def hello(opts: CommandOptions): """ Say hello """ # Do command imports locally to improve startup time! print(f"Hello {opts.name}") @APP.command(name="hello-async") @argument("-n", "--name", default="Jane") async def hello_async(opts: CommandOptions): """ Say hello in an AsyncIO run-loop """
""" Single file sample """ from pyapp.app import argument from pyapp.app import CliApplication from pyapp.app import CommandOptions from pyapp.app import KeyValueAction APP = CliApplication() @APP.default def helper(*, bar: dict): print(bar) # print(opts.bar) if __name__ == "__main__": APP.dispatch()
from pyapp.app import CliApplication, CommandOptions from pyapp.injection import inject_into, Args import sample app = CliApplication(sample) def main(args=None): app.dispatch(args) if __name__ == "__main__": main()
from pyapp.app import CliApplication, argument import sample app = CliApplication(sample, name="PyApp Sample", description="Sample pyApp application.") @app.command def foo(opts): print("bar") if __name__ == "__main__": app.dispatch()
from pyapp.app import CliApplication app = CliApplication() if __name__ == "__main__": app.dispatch()