GitHub - anaconda-graveyard/clyent: Command line client library for posix and windows --moved from Anaconda-Platform/clyent (original) (raw)

Clyent

Binstar Badge Binstar Badge Binstar Badge

Clyent is a python command line utiliy library forbinstar,binstar-buildand chalmers

Clyent API Usage:

clyent.add_default_arguments(parser, version=None)

Add some default arguments to the argument parser.

clyent.add_subparser_modules(parser, package)

This will add sub parsers from a python package e.g. for the directory structure:

package/__init__.py
package/commands/__init__.py
package/commands/command.py

One would do:

from package import commands add_subparser_modules(parser, package)

Each command module must contain the funciton add_parser(subparsers) which takes an argument subparsers wich is the result of the function argparse.ArgumentParser.add_subparsers

The add_parser method should call:

parser = subparsers.add_parser('some-name') parser.set_defaults(main=your_main_function)

clyent.setup_logging(logger, log_level, use_color, show_tb, logfile)

Set up loggers to print color errors

TODO: documnent more

clyent.run_command(parser, exit=True)

This command will run your subcommand and capture the output

Putting this together in a command line client

def main(args=None, exit=True):

parser = ArgumentParser(description=__doc__)

add_default_arguments(parser, version)
add_subparser_modules(parser, chalmers.commands)

args = parser.parse_args(args)
logfile = join(dirs.user_log_dir, 'chalmers.log')
setup_logging(logger, args.log_level, use_color=args.color,
              show_tb=args.show_traceback, logfile=logfile)

run_command(args, exit=exit)

See the chalmers main script for more details.

from clyent.logs.colors.printer import print_colors print_colors('Hey! {=This is an inline \nmessage!c:red,bold,underline} ...')

Hey! This is an inline
message

print_colors('This is a format substitution {ok!c:green,bold} ' 'Because the value contains unescaped characters', ok='{OK!}')

This is a format substitution OK! Because the value contains unescaped characters

with color.blue: print('This is a message within a color context')

This is a message within a color context

print_colors(color.underline('hello'), 'is euqal to', '{=hello!c:underline}')

hello is equal to hello