The YAML Output Plugin provides similar functionality to the builtin ‘–json’ output_handler, but outputs yaml. The following features are included:
* --yaml option causes output to be rendered strictly as yaml
The yaml plugin is Open Source and distributed under The MIT License. Please see the LICENSE file included with The Rosendale Project.
Stable versions of the rosendale.yaml plugin are available via PyPi and can be installed by easy_install, pip, or similar:
$ easy_install rosendale.yaml
You will want to add ‘rosendale.yaml’ to your applications ‘setup.py’ under the section ‘install_requires’.
The yaml_output plugin can be enabled in any application built on Cement, however due to its tight integration with Cement internals it can not be loaded dynamically. It MUST be included during your applications root bootstrap process by adding the following to <youapp>/bootstrap/root.py:
from rosendale.bootstrap import yaml_output
This plugin does not honor any other config settings and does not require a plugin config file.
To demonstrate usage we have created a fresh ‘helloworld’ application via the paster cement-app helper. We’ve then enabled the rosendale yaml plugin by adding a permanent import to helloworld/bootstrap/root.py as listed above. We now have:
$ helloworld --help
Usage: helloworld <COMMAND> [ARGS] --(OPTIONS)
Commands:
get-started, cmd1
Help? try '[COMMAND]-help' OR '[NAMESPACE] --help'
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-R, --root-option Example root option
--json render output as json (CLI-API)
--debug toggle debug output
--quiet disable console logging
--yaml render output as yaml
You can see the additional option ‘–yaml’.
The ‘–yaml’ flag simply overrides your applications ‘output_handler_override’ config option on execution. Therefore, all commands function normally but the output is rendered as YAML. The following is the example command we are going to run:
class RootController(CementController):
@expose('helloworld.templates.root.cmd1')
def cmd1(self):
foo = 'In helloworld.controllers.root.cmd1()'
if self.cli_opts.enable_yaml:
print 'The --yaml option was passed'
items = ['one', 'two', 'three']
return dict(foo=foo, items=items)
And this is the output:
$ helloworld cmd1
In helloworld.controllers.root.cmd1()
* one
* two
* three
$ helloworld cmd1 --yaml
foo: In helloworld.controllers.root.cmd1()
items: [one, two, three]
stderr: ''
stdout: 'The --yaml option was passed
Note that, just as the ‘–json’ option, all STDOUT output is captured and passed via YAML as well. Meaning, the output from the command is pure YAML and not corrupted by spurious output.