Usage

Command line

To enter shell mode

$ python script_path

To dispatch commands

$ python script groupX/taskX [arg1=value arg2=value ...] # execute a task

$ python script -p groupX/taskX arg1=value # execute a task, with partial args.

To launch a scriptlet/dir

$ ec scriptlet/dir [flag] groupX/taskX [args ...]

Examples

A simple example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from ec.ec import task, arg, group

@task # define a task
@arg(type=int, desc='Value for arg1') # add an argument with a type and a description
@arg(type=int)
def task1(arg1, arg2=1):
  print arg1, arg2

@group(desc='A group with some tasks') # define a group
class group1:
  @task
  def task1(arg1): # define a task inside the group
    print arg1 + arg1

From the command line enter

$ python simple.py task1 arg1=1 arg2=2
        1 2

$ python simple.py group1/task1 arg1=1
        11

$ python simple.py group1/task1 1 # positional arg
        11

$ python simple.py group1/task1 +\=1 # escape '=' with a '\' when it's a part of a positional argument
        +=1+=1

An advanced example (wrapping ec to extend it)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
"""
An example for wrapping ec based modules.
"""

from ec import interface

import simple # the ec-ed script

interface.setBase(simple)

interface.call('task1 arg2=1', True)

A nut shell example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
r"""
Everything about using ec. this module is designed to be a nut shell guide to ec and as a way to enable test driven development.

Notes
-----
* The decorators task, arg and group are available as __builtins__, when the script is launched using ec.
* DocStrings of the tasks could be used by external modules (like sphinx). This is one of the key factors of developing ec, apart from it's predecessor Commander.
"""
from ec.ec import task, arg, group, module, member, call, settings, exit_hook
from ec.utils import get
from ec.types import regex

@task
@arg(type=int, desc='Some int') # configure the first arg
@arg('arg2', type=int) # configure the arg named 'arg2'.
def task1(arg1, arg3=3, arg2=2):
  r"""A simple task, at the root of the script.

    * Note that the order of input collection will follow the order of configuration (in this case arg1 and then arg2).
    * Any unconfigured args will be collected after the collection of the configured args (hence arg3 will be collected as the last arg).
    * Since a name isn't specified in @task, the name of the task, *simple* will be used as the name of this task.
  """
  print arg1, arg2, arg3

@group(alias='i', desc='A group.')
class intro:
  r"""
    Groups can contain tasks and other groups within them.
    This group has an alias; thus could be identified as **intro** or **i**. Tasks to could have aliases.
  """
  @task(name='simple')
  @arg('arg1', desc='Some string')
  def renamed_task(arg1):
    r"""A task within a group.

      * This task can be accessed as intro/simple.
      * Notice the missing **self** arg.
      * @task.name, **simple** will be the name of this task, not the function name **renamed_task**.
    """
    intro.log('%s, from %s.' % (arg1, intro.name))

  @task
  def wrapper():
    r"""Calls nut_shell.simple with some arguments, the args that aren't provided will be collected.
    """
    call(simple, arg1=1, arg2=2)

  @task
  def get():
    r"""Get user input through utils.get.
    """
    print get(desc='Email id', type=regex.email)

  def log(message):
    r"""A helper method.
    """
    print message

  name = 'intro' # Groups could even have variables.

# importing other modules
import simple
member(simple) # member() is used to expose imported members as the children of the current module

@exit_hook
def clean_up(): # exit hooks are called when ec exits. There may be more than one hook.
  print ':)'

module(desc='A module to test decorator based configuration.') # module is an optional call, used to configure the group that wraps current module.

settings(dev_mode=True) # settings for ec

Documentation of nut_shell.py

nut_shell

A module to test decorator based configuration.

Everything about using ec. this module is designed to be a nut shell guide to ec and as a way to enable test driven development.

Notes
  • The decorators task, arg and group are available as __builtins__, when the script is launched using ec.
  • DocStrings of the tasks could be used by external modules (like sphinx). This is one of the key factors of developing ec, apart from it’s predecessor Commander.
task1(arg1[arg2=2][arg3=3])

A simple task, at the root of the script.

  • Note that the order of input collection will follow the order of configuration (in this case arg1 and then arg2).
  • Any unconfigured args will be collected after the collection of the configured args (hence arg3 will be collected as the last arg).
  • Since a name isn’t specified in @task, the name of the task, simple will be used as the name of this task.
Args
arg1: Some int.
arg2: int (2).
arg3: str (3).
intro, i

A group.

Groups can contain tasks and other groups within them. This group has an alias; thus could be identified as intro or i. Tasks to could have aliases.
simple(arg1)

A task within a group.

  • This task can be accessed as intro/simple.
  • Notice the missing self arg.
  • @task.name, simple will be the name of this task, not the function name renamed_task.
Args
arg1: Some string.
wrapper()

Calls nut_shell.simple with some arguments, the args that aren’t provided will be collected.

get()

Get user input through utils.get.

simple
task1(arg1[arg2=1])
Args
arg1: Value for arg1.
arg2: int (1).
group1

A group with some tasks

task1(arg1)
Args
arg1: .