← Back to team overview

launchpad-dev team mailing list archive

script_commands library

 

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

While hacking on the local-latency script (which now has a configurable
- --port, by the way), I was unable to resist my compulsion to make it
simpler the next time I do this kind of thing.

The result is utilities/script_commands.py, which makes it easy to turn
a function into a script, or script subcommand.

Here's the definition of the "start" subcommand:

@helps(delay='Length of delay in miliseconds (each way).',
       port='Port to induce delay on.')
def start(delay=500, port=443):
    """Add artificial latency to the lo interface on the specified port."""

As you can see, the only weird thing about the function is the "helps"
decorator that describes each parameter.  And that's optional.  The stop
command, which takes no parameters, is even simpler:

def stop():


You run such commands with script.commands.run_from_args.  Each
parameter that has a default is converted into an option, and the type
of the parameter is determined from the default.  (I haven't needed
positional parameters yet, so I haven't added support for them yet.)

If your script needs subcommands, you stick them in a dict and call
script.commands.run_subcommand instead of run_from_args.

So the next time you're writing a utility script, give script_commands a
try.  It could save you time and drudgery.

Aaron
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4/724ACgkQ0F+nu1YWqI0pXwCfbsTonDqOSf1O8G+kqsfn0+z6
o/gAnRdhApBiIBw26bGLMrjksWFQ6A74
=fVE8
-----END PGP SIGNATURE-----


Follow ups