pykern.pkcli package

Submodules

pykern.pkcli.github module

pykern.pkcli.pkexample module

Demonstrate how to write reusable command line tools.

A pykern.pkcli module is designed to be used from any context any other piece of code. Since we use argh, it makes it easy to dispatch with a bit of glue so that all you have to do is write a function.

Documentation should be provided as a normal docstring for the function. See echo.

You should set up your project directory with pykern.pkcli.projex, which will give you a shell command that gets automatically installed. Each project has one such command. For example, pykern.pykern_console is the module that invokes all pykern pkcli functions.

To invoke echo, use the shell command:

$ pykern pkexample echo something
howdy: something

The output of the function is printed on stdout so if you don’t have to print messages, and the function can be used in other contexts where stdout is not useful (e.g. returned to a brower).

If you invoke this module without arguments, you get a list of functions you can call:

$ pykern pkexample usage: pykern pkexample [-h] {echo,primes} … pykern pkexample: error: too few arguments

This list only includes “public” functions (not beginning with an underscore).

In your project, you’ll have your own function, for example, you can start Sirepo with:

$ sirepo service http
 * Running on http://0.0.0.0:8000/
 * Restarting with reloader

The module sirepo.sirepo_console is automatically installed as a shell command by setup.py, and pykern.pkcli searches for modules to invoke in the package sirepo.pkcli. In fact, you can list out all modules by invoking sirepo without any arguments:

$ sirepo
usage: sirepo module command [args...]
Modules:
celery
elegant
service
srw
warp
copyright:Copyright (c) 2016 RadiaSoft LLC. All Rights Reserved.
license:http://www.apache.org/licenses/LICENSE-2.0.html
pykern.pkcli.pkexample.echo(suffix, prefix='howdy: ')[source]

Concatenate prefix and suffix

Parameters:
  • to_echo (str) – what to print and must be at least five chars
  • prefix (str) – what to put in front of to_echo [“howdy: “]
Returns:

prefix + suffix

Return type:

str

pykern.pkcli.pkexample.primes(max_prime, timeit=0)[source]

Compute primes less than max_prime

Parameters:
  • max_prime (int) – maximum number of primes
  • timeit (int) – run timeit function with this many loops
Returns:

list of primes (list) or timeit result (str)

Return type:

object

pykern.pkcli.projex module

Manage Python development projects

copyright:Copyright (c) 2015 Bivio Software, Inc. All Rights Reserved.
license:http://www.apache.org/licenses/LICENSE-2.0.html
pykern.pkcli.projex.DEFAULTS = {'copyright_license_rst': ':copyright: Copyright (c) {year} {author}. All Rights Reserved.\n:license: {license}', 'license': 'apache2', 'year': 2018}

Default values

pykern.pkcli.projex.LICENSES = {'agpl3': ('http://www.gnu.org/licenses/agpl-3.0.txt', 'License :: OSI Approved :: GNU Affero General Public License v3'), 'apache2': ('http://www.apache.org/licenses/LICENSE-2.0.html', 'License :: OSI Approved :: Apache Software License'), 'gpl2': ('http://www.gnu.org/licenses/gpl-2.0.txt', 'License :: OSI Approved :: GNU General Public License v2 (GPLv2)'), 'gpl3': ('http://www.gnu.org/licenses/gpl-3.0.txt', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)'), 'lgpl2': ('http://www.gnu.org/licenses/lgpl-3.0.txt', 'License :: OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)'), 'lgpl3': ('http://www.gnu.org/licenses/lgpl-3.0.txt', 'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)'), 'mit': ('http://opensource.org/licenses/MIT', 'License :: OSI Approved :: MIT License'), 'proprietary': ('PROPRIETARY AND CONFIDENTIAL. See LICENSE file for details.', 'License :: Other/Proprietary License')}

Licenses

pykern.pkcli.projex.init_rs_tree(description)[source]

Initialize defaults for RadiaSoft project tree and call init_tree

name is the base name of the current directory.

url is https://github.com/radiasoft/<name>

Parameters:description (str) – one-line summary of project
pykern.pkcli.projex.init_tree(name, author, author_email, description, license, url)[source]

Setup a project tree with: docs, tests, etc., and checkin to git.

Creates: setup.py, index.rst, project dir, <name>_console.py, etc. Overwrites files if they exist without checking.

Parameters:
  • name (str) – short name of the project, e.g. pykern.
  • author (str) – copyright holder, e.g. RadiaSoft LLC
  • author_email (str) – how to reach author, e.g. pip@pykern.org
  • description (str) – one-line summary of project
  • license (str) – url of license
  • url (str) – website for project, e.g. http://pykern.org

pykern.pkcli.pytest module

DEPRECATED

copyright:Copyright (c) 2015 RadiaSoft LLC. All Rights Reserved.
license:http://www.apache.org/licenses/LICENSE-2.0.html
pykern.pkcli.pytest.default_command(*args, **kwargs)[source]

DEPRECATED: just run py.test

pykern.pkcli.rsmanifest module

Create and read global and user manifests.

copyright:Copyright (c) 2017 RadiaSoft LLC. All Rights Reserved.
license:http://www.apache.org/licenses/LICENSE-2.0.html
pykern.pkcli.rsmanifest.BASENAME = 'rsmanifest.json'

Appears in each directory

pykern.pkcli.rsmanifest.CONTAINER_FILE = '/rsmanifest.json'

Written once at build time

pykern.pkcli.rsmanifest.FILE_VERSION = '20170217.180000'

Format version

pykern.pkcli.rsmanifest.USER_FILE = '~/rsmanifest.json'

Read and written multiple times as the run user

pykern.pkcli.rsmanifest.add_code(name, version, uri, source_d, virtual_env=None, pyenv=None)[source]

Add a new code to ~?rsmanifest.json

Parameters:
  • name (str) – name of the package
  • version (str) – commit or version
  • uri (str) – repo, source link
  • source_d (str) – directory containing
  • virtual_env (str) – DEPRECATED
  • pyenv (str) – pyenv version
pykern.pkcli.rsmanifest.pkunit_setup()[source]

Create rsmanifest files

pykern.pkcli.rsmanifest.read_all()[source]

Merge all manifests

Returns:merged data
Return type:dict

pykern.pkcli.sim module

wrapper for running simulations

copyright:Copyright (c) 2017 RadiaSoft LLC. All Rights Reserved.
license:http://www.apache.org/licenses/LICENSE-2.0.html
pykern.pkcli.sim.cfg = None

configuration

pykern.pkcli.sim.default_command(cmd, *args, **kwargs)[source]

Wrapper until figure out args with argh

Module contents

Invoke commands from command line interpreter modules.

Any module in <root_pkg>.pkcli will be found by this module. The public functions of the module will be executed when called from the command line. This module is invoked by pykern.pykern_console. Every project must have its own invocation module.

The basic form is: <project> <simple-module> <function>. <simple-module> is the module without <root_pkg>.pkcli. <function> is any function that begins with a letter and contains word characters (w).

If the module only has one public function named default_command, the form is: <project> <simple-module>.

The purpose of this module is to simplify command-line modules. There is no boilerplate. You just create a module with public functions in a particular package location (e.g. pykern.pkcli). This module does the rest.

pykern.pkcli.pkexample is a working example.

copyright:Copyright (c) 2015-2016 RadiaSoft LLC. All Rights Reserved.
license:http://www.apache.org/licenses/LICENSE-2.0.html
pykern.pkcli.CLI_PKG = ['pkcli', 'pykern_cli']

Sub-package to find command line interpreter (cli) modules will be found

pykern.pkcli.DEFAULT_COMMAND = 'default_command'

If a module only has one command named this, then execute directly.

pykern.pkcli.command_error(fmt, *args, **kwargs)[source]

Raise CommandError with msg

Parameters:fmt (str) – how to represent arguments
Raises:CommandError – always
pykern.pkcli.main(root_pkg, argv=None)[source]

Invokes module functions in pykern.pykern_cli

Looks in <root_pkg>.pykern_cli for the argv[1] module. It then invokes the argv[2] method of that module.

Parameters:
  • root_pkg (str) – top level package name
  • argv (list of str) – Defaults to sys.argv. Only used for testing.
Returns:

0 if ok. 1 if error (missing command, etc.)

Return type:

int