LogoLogo
Official SiteAPI Reference
stable/3.0
stable/3.0
  • Cement Developer Guide
  • Release Information
    • What's New!
    • Upgrading
    • ChangeLog
    • Deprecations
  • Getting Started
    • Installation
    • Developer Tools
    • Framework Overview
    • Beginner Tutorial
      • Part 1: Creating Your First Project
      • Part 2: Adding Features
      • Part 3: Extending a Project
      • Part 4: Making Everything Legit
  • Core Foundation
    • Interfaces and Handlers
    • Hooks
    • Configuration Settings
    • Arguments
    • Logging
    • Controllers
    • Output Rendering
    • Caching
    • Mail Messaging
    • Framework Extensions
    • Application Plugins
    • Templating
  • Utilities
    • Filesystem
    • Shell
    • Miscellaneous
  • Extensions
    • Alarm
    • Argparse
    • Colorlog
    • ConfigParser
    • Daemon
    • Dummy
    • Generate
    • Jinja2
    • Json
    • Logging
    • Memcached
    • Mustache
    • Plugin
    • Print
    • Redis
    • Scrub
    • SMTP
    • Tabulate
    • Yaml
    • Watchdog
  • Additional Topics
    • Extending The App Object
    • Unit Testing
    • Cleanup
    • Signal Handling
    • Pipenv
    • Autocomplete
    • Profiling with cProfile
    • Debugging with VSCode
  • Environment Variables
  • Terminology
  • Contributing
  • Privacy Policy
Powered by GitBook
On this page
  • Introduction
  • Requirements
  • Configuration
  • Usage
  • Config Handler
  • Output Handler
  1. Extensions

Yaml

Introduction

The Yaml Extension includes the YamlOutputHandler to render output in pure Yaml, as well as the YamlConfigHandler that allows applications to use Yaml configuration files as a drop-in replacement of the default ConfigParserConfigHandler.

Documentation References:

  • Configuration Settings

  • Output Rendering

API References:

  • Cement Yaml Extension

  • Yaml Library

Requirements

  • pyYaml

Cement 3.0.8+:

pip install cement[yaml]

Applications using Cement <3.0.8 should continue to include pyYaml in their dependencies.

Configuration

This extension does not support any application level configuration settings or meta options.

Usage

Config Handler

myapp.py
from cement import App

class MyApp(App):
    class Meta:
        label = 'myapp'
        extensions = ['yaml']
        config_handler = 'yaml'
        config_file_suffix = '.yml'
~/.myapp.yml
---
myapp:
    foo: bar

Output Handler

In general, you likely would not set output_handler to yaml, but rather another type of output handler that displays readable output to the end-user (ex: Mustache, Jinja2, or Tabulate). However, Cement supports overriding handlers via command line options if the Handler.Meta.overridable option is set. For example, -o yaml will trigger the framework to use the yaml output handler, overriding the default set in App.Meta.output_handler.

See the documentation on Overriding Handlers via Command Line.

myapp.py
from cement import App, init_defaults

META = init_defaults('output.yaml')
META['output.yaml']['overridable'] = True

class MyApp(App):
    class Meta:
        label = 'myapp'
        extensions = ['yaml', 'mustache']
        meta_defaults = META
        output_handler = 'mustache'
        template_dir = './templates'

with MyApp() as app:
    app.run()
    data = {'foo': 'bar'}
    app.render(data, 'example.m')
templates/example.m
Foo: {{ foo }}
$ python myapp.py
Foo: bar

$ python myapp.py -o yaml
{foo: bar}
PreviousTabulateNextWatchdog

Last updated 2 years ago