def test_embedding(self): # Multiple apps (slightly modified for testing) out1 = Stream() out2 = Stream() app1 = clip.App(stdout=out1, name='app1') app2 = clip.App(stdout=out2, name='app2') clip.echo('App 1 via name', app='app1') clip.echo('App 2 via name', app='app2') app1.echo('App 1 directly') app2.echo('App 2 directly') clip.echo('Broadcast!') self.assertEqual( out1._writes, ['App 1 via name\n', 'App 1 directly\n', 'Broadcast!\n']) self.assertEqual( out2._writes, ['App 2 via name\n', 'App 2 directly\n', 'Broadcast!\n'])
def test_argument_mistakes(self): # Specifying more than one name for an argument with self.assertRaises(TypeError): app = clip.App() @app.main() @clip.arg('name', 'whoops') def f(name): pass
def test_parameter_mistakes(self): # If nargs != 1 and default is not a list with self.assertRaises(TypeError): app = clip.App() @app.main() @clip.opt('-a', nargs=-1, default='whoops') def f(a): pass
def test_command_mistakes(self): # Giving a command a callback that is already a command with self.assertRaises(TypeError): app = clip.App() @app.main() def f(): pass @f.subcommand() @f.subcommand() def sub(): pass # Giving main an "inherits" with self.assertRaises(AttributeError): app = clip.App() @app.main(inherits=['whoops']) def f(whoops): pass # Inheriting a parameter that doesn't exist with self.assertRaises(AttributeError): app = clip.App() @app.main() def f(): pass @f.subcommand(inherits=['whoops']) def sub(whoops): pass # Giving tree_view something other than a flag with self.assertRaises(TypeError): app = clip.App() @app.main(tree_view='arg') @clip.arg('arg', help='whoops') def f(arg): pass
def test_app_mistakes(self): # App should not define more than one main command app = self.make_kitchen_sink_app() with self.assertRaises(AttributeError): @app.main() def f(): pass # App should define at least one main command with self.assertRaises(AttributeError): clip.App().run('something')
def on_open(self, info): self._todos = [] self._app = clip.App(stdout=Stream(self.on_out), stderr=Stream(self.on_err)) @self._app.main() def todo(): pass @todo.subcommand() @clip.arg('desc', nargs=-1, required=True) def add(desc): desc = ' '.join(desc) self._todos.append(Todo(desc)) clip.echo('Added "{}" to the list of todos'.format(desc)) @todo.subcommand() @clip.arg('index', type=int, required=True) def remove(index): try: removed = self._todos.pop(index - 1) clip.echo('Removed todo "{}"'.format(removed._item)) except IndexError: clip.exit('Invalid todo index given', True) @todo.subcommand() @clip.arg('index', type=int, required=True) def complete(index): try: completed = self._todos[index - 1] completed.complete() clip.echo('Marked todo "{}" as completed'.format( completed._item)) except IndexError: clip.exit('Invalid todo index given', True) @todo.subcommand(name='list') @clip.flag('-a', '--active') def list_todos(active): l = [e for e in self._todos if not e._completed] if active else self._todos clip.echo('\n'.join([ '{}. {}'.format(i + 1, e.get_str()) for i, e in enumerate(l) ]))
def make_kitchen_sink_app(self): app = clip.App() self.a = [] self.b = [] @app.main() @clip.flag('-a', '--apple') @clip.flag('--banana', '-b') @clip.opt('--file', name='filename') @clip.arg('donut') def a(apple, banana, filename, donut): self.a = [apple, banana, filename, donut] @a.subcommand() @clip.flag('-t') @clip.flag('--long-thing') @clip.arg('args', nargs=-1) def b(t, long_thing, args): self.b = [t, long_thing, args] return app
from getpass import getpass import os import sys import clip import pika from colorama import init, Back, Fore, Style from configobj import ConfigObj app = clip.App() CONFIG_FILE_PATH = os.path.expanduser("~/.amqpclirc") if os.path.exists(CONFIG_FILE_PATH): USER_MAP = ConfigObj(infile=CONFIG_FILE_PATH, encoding='utf8') else: USER_MAP = ConfigObj(encoding='utf8') USER_MAP.filename = CONFIG_FILE_PATH def get_password(user): password = os.getenv('AMQP_PASSWORD', None) if password: return password user_record = USER_MAP.get('users', {}).get(user, None) if user_record: return user_record['password'] return getpass(Fore.GREEN + "Password: " + Fore.RESET) def get_vhost(user): vhost = os.getenv('AMQP_VHOST', None) if vhost:
#! /usr/bin/env python from __future__ import absolute_import import clip, logging, logtool, raven, sys from path import Path from .config import Config from . import __version__ logging.basicConfig(level=logging.WARN) LOG = logging.getLogger(__name__) APP = clip.App(name="xxpaper") Config.set("xxpaper", {}) @logtool.log_call def option_logging(flag): # pylint: disable=unused-argument logging.root.setLevel(logging.DEBUG) @logtool.log_call def option_verbose(value): Config._verbose = value # pylint: disable=protected-access @logtool.log_call def option_version(opt): # pylint: disable=unused-argument clip.echo("Version: %s" % __version__) sys.exit(0) @APP.main(name=Path(sys.argv[0]).basename(),
__version__ = get_versions()['version'] del get_versions logging.basicConfig(level=logging.WARN) LOG = logging.getLogger(__name__) TIME_T, TIME_STR = logtool.now() # Python 2 will try to coerce its output stream to match the terminal it is # printing to. If we pipe the output, it will attempt to do plain ASCII # encoding, which breaks on unicode characters. The following will change # the default encoding for pipes from ascii to utf-8. # See : https://wiki.python.org/moin/PrintFails sys.stdout = StreamWriter(sys.stdout) sys.stderr = StreamWriter(sys.stderr) APP = clip.App(name="s3clumper") CONFIG = Dict({ "check": False, "force": False, "nocolour": False, "delete": False, "quiet": False, "time_str": TIME_STR, "time_t": TIME_T, "verbose": False, }) IO = CmdIO(conf=CONFIG) @logtool.log_call
def embed(self): out, err = Stream(), Stream() app = clip.App(stdout=out, stderr=err) return app, out, err
import clip import sys class Stream(object): def __init__(self, f): self._f = f def write(self, message): self._f(message) def stream_a(message): sys.stdout.write('From stream A: {}'.format(message)) def stream_b(message): sys.stdout.write('From stream B: {}'.format(message)) app1 = clip.App(stdout=Stream(stream_a), name='app1') app2 = clip.App(stdout=Stream(stream_b), name='app2') clip.echo('App 1 via name', app='app1') clip.echo('App 2 via name', app='app2') app1.echo('App 1 directly') app2.echo('App 2 directly') clip.echo('Broadcast!')
del get_versions logging.basicConfig(level=logging.WARN) LOG = logging.getLogger(__name__) TIME_T, TIME_STR = logtool.now() BLOCK_LIMIT = 1024 * 1024 # Python 2 will try to coerce its output stream to match the terminal it is # printing to. If we pipe the output, it will attempt to do plain ASCII # encoding, which breaks on unicode characters. The following will change # the default encoding for pipes from ascii to utf-8. # See : https://wiki.python.org/moin/PrintFails sys.stdout = StreamWriter(sys.stdout) sys.stderr = StreamWriter(sys.stderr) APP = clip.App(name="s3lncoll") CONFIG = Dict({ "block": BLOCK_LIMIT, "force": False, "delete": False, "json": "False", "quiet": False, "time_str": TIME_STR, "time_t": TIME_T, }) @logtool.log_call def option_setopt(option, value): CONFIG[option] = value