Skip to main content

ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order

Project description

ruamel.yaml

ruamel.yaml is a YAML package for Python. It is a derivative of Kirill Simonov’s PyYAML 3.11 which supports YAML1.1

Major differences with PyYAML 3.11:

  • intergrated Python 2 and 3 sources, running on Python 2.6, 2.7 (CPython, PyPy), 3.3 and 3.4.

  • round trip mode that includes comments (block mode, key ordering kept)

  • support for simple lists as mapping keys by transformation to tuples

  • !!omap generates ordereddict (C) on Python 2, collections.OrderedDict on Python 3, and !!omap is generated for these types.

  • some YAML 1.2 enhancements (0o octal prefix, \/ escape)

  • pep8 compliance

  • tox and py.test based testing

  • Tests whether the C yaml library is installed as well as the header files. That library doesn’t generate CommentTokens, so it cannot be used to do round trip editing on comments. It can be used for speeded up normal processing (so you don’t need to install ruamel.yaml and PyYaml). See the section Optional requirements.

  • Basic support for multiline strings with preserved newlines and chomping ( ‘|’, ‘|+’, ‘|-’ ). As this subclasses the string type the information is lost on reassignment. (This might be changed in the future so that the preservation/folding/chomping is part of the parent container, like comments).

  • RoundTrip preservation of flow style sequences ( ‘a: b, c, d’) (based on request and test by Anthony Sottile)

  • adding/replacing of comments on block style sequences and mappings with smart column positioning

  • collection objects (when read in via RoundTripParser) have an lc property that contains line and column info lc.line and lc.col

Round trip including comments

The major motivation for this fork is the round-trip capability for comments. The integration of the sources was just an initial step to make this easier.

adding/replacing comments

Starting with version 0.8, you can add/replace comments on block style collections (mappings/sequences resuting in Python dict/list). The basic for for this is:

from __future__ import print_function

import ruamel.yaml

inp = """\
abc:
  - a     # comment 1
xyz:
  a: 1    # comment 2
  b: 2
  c: 3
  d: 4
  e: 5
  f: 6 # comment 3
"""

data = ruamel.yaml.load(inp, ruamel.yaml.RoundTripLoader)
data['abc'].append('b')
data['abc'].yaml_add_eol_comment('comment 4', 1)  # takes column of comment 1
data['xyz'].yaml_add_eol_comment('comment 5', 'c')  # takes column of comment 2
data['xyz'].yaml_add_eol_comment('comment 6', 'e')  # takes column of comment 3
data['xyz'].yaml_add_eol_comment('comment 7', 'd', column=20)

print(ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper), end='')

Resulting in:

abc:
- a       # comment 1
- b       # comment 4
xyz:
  a: 1    # comment 2
  b: 2
  c: 3    # comment 5
  d: 4              # comment 7
  e: 5 # comment 6
  f: 6 # comment 3

If the comment doesn’t start with ‘#’, this will be added. The key is is the element index for list, the actual key for dictionaries. As can be seen from the example, the column to choose for a comment is derived from the previous, next or preceding comment column (picking the first one found).

Config file formats

There are only a few configuration file formats that are easily readable, and editable: JSON, INI/ConfigParser, YAML (XML is to cluttered to be called easily readable).

Unfortunately JSON doesn’t support comments, and although there are some solutions with pre-processed filtering of comments, there are no libraries that support round trip updating of such commented files.

INI files support comments, and the excellent ConfigObj library by Foord and Larosa even supports round trip editing with comment preservation, nesting of sections and limited lists (within a value). Retrieval of particular value format is explicit (and extensible).

YAML has basic mapping and sequence structures as well support for ordered mappings and sets. It supports scalars are of various types including dates and datetimes (missing in JSON) as a list of YAML has comments, but these are normally thrown away.

Block structured YAML is a clean and very human readable format. By extending the Python YAML parser to support round trip preservation of comments, it makes YAML a very good choice for configuration files that are human readable and editable while at the same time interpretable and modifiable by a program.

Extending

There are normally 6 files involved when extending the roundtrip capabilities: the reader, parser, composer and constructor to go from YAML to Python and the resolver, representer, serializer and emitter to go the other way.

Extending involves keeping extra data around for the next process step, eventuallly resulting in a different Python object (subclass or alternative), that should behave like the original, but on the way from Python to YAML generates the original (or at least something much closer).

Examples

Basic round trip of parsing YAML to Python objects, modifying and generating YAML:

from __future__ import print_function

import ruamel.yaml

inp = """\
# example
name:
  # details
  family: Smith   # very common
  given: Alice    # one of the siblings
"""

code = ruamel.yaml.load(inp, ruamel.yaml.RoundTripLoader)
code['name']['given'] = 'Bob'

print(ruamel.yaml.dump(code, Dumper=ruamel.yaml.RoundTripDumper), end='')

Resulting in

# example
name:
  # details
  family: Smith   # very common
  given: Bob      # one of the siblings

Optional requirements

If you have the C yaml library and headers installed, as well as the header files for your Python executables then you can use the non-roundtrip but faster C loader en emitter.

On Debian systems you should use:

sudo apt-get install libyaml-dev python-dev python3-dev

you can leave out python3-dev if you don’t use python3

For CentOS (7) based systems you should do:

sudo yum install libyaml-devel python-devel

Testing

Testing is done using the tox, which uses virtualenv and pytest.

yaml utlity

A utility name yaml is included and allows for basic operations on files:

  • yaml round-trip <file_name> for basic roundtrip testing of YAML files

  • yaml json <file_name> for conversion of JSON file(s) to a single YAML block style document

  • yaml ini <file_name> for conversion of an INI/config file (ConfigObj comment and nested sections supported) to a YAML block style document. This requires configobj to be installed (pip install configobj)

  • yaml html <file_name> for conversion of the basic structure in a YAML file to a a table in an HTML file. The YAML file:

    title:
    - fruit
    - legume
    local:
    - apple
    - sprouts
    import:
    - orange
    - broccoli

    is converted into the table:

    title

    fruit

    legume

    local

    apple

    sprouts

    import

    orange

    broccoli

See yaml --help for more information on the availble commands

Project details


Release history Release notifications | RSS feed

This version

0.9.5

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ruamel.yaml-0.9.5.tar.gz (170.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

ruamel.yaml-0.9.5-cp34-none-win_amd64.whl (71.5 kB view details)

Uploaded CPython 3.4Windows x86-64

ruamel.yaml-0.9.5-cp34-none-win32.whl (71.4 kB view details)

Uploaded CPython 3.4Windows x86

ruamel.yaml-0.9.5-cp33-none-win_amd64.whl (71.5 kB view details)

Uploaded CPython 3.3Windows x86-64

ruamel.yaml-0.9.5-cp33-none-win32.whl (71.5 kB view details)

Uploaded CPython 3.3Windows x86

ruamel.yaml-0.9.5-cp27-none-win_amd64.whl (71.5 kB view details)

Uploaded CPython 2.7Windows x86-64

ruamel.yaml-0.9.5-cp27-none-win32.whl (71.5 kB view details)

Uploaded CPython 2.7Windows x86

ruamel.yaml-0.9.5-cp26-none-win_amd64.whl (71.5 kB view details)

Uploaded CPython 2.6Windows x86-64

ruamel.yaml-0.9.5-cp26-none-win32.whl (71.5 kB view details)

Uploaded CPython 2.6Windows x86

File details

Details for the file ruamel.yaml-0.9.5.tar.gz.

File metadata

  • Download URL: ruamel.yaml-0.9.5.tar.gz
  • Upload date:
  • Size: 170.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ruamel.yaml-0.9.5.tar.gz
Algorithm Hash digest
SHA256 46e1c172b94c5685b935cd72ec4cb617411c9af8dd6b8b34ad8b5e43a1c3fd8f
MD5 7fa7490c9b0e03b716f324857cdf14fb
BLAKE2b-256 5d13546a0f77741f4c7d3fa551435d7c230007c0445e091ce93d6c7c17fdfa5a

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp34-none-win_amd64.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp34-none-win_amd64.whl
Algorithm Hash digest
SHA256 47e298fa32451893e791434a0f9813c80f40a3430fb80a1147c77e9d65c64e41
MD5 4592f82d1ea5c0358e0a703b05b41d43
BLAKE2b-256 6a1587ab374385fff7676408edcddbfbb1c0147c15d789265a85f5bbba31ca11

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp34-none-win32.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp34-none-win32.whl
Algorithm Hash digest
SHA256 5a7e1c02461522353508c9196bdfd80e858b01cd7be3ce3e0b7acfd132949546
MD5 d06455b4f9eb487454e7bb0c472733f7
BLAKE2b-256 5e56cbb4827208d1edeb6bfd18a00a3e6be6a4822d176eeeaf69f46701f6e31a

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp33-none-win_amd64.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp33-none-win_amd64.whl
Algorithm Hash digest
SHA256 5accfa74c9d054135ca6caa5643e326ae9b0555dfcf4bd282609a239061f8fe1
MD5 6c92a4be2ac1af5d34575e76641936b9
BLAKE2b-256 99c4d51eedb2012dbc8a409cc2cf3b96e3f526cb48cd0c07ad0a7cad5523cae2

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp33-none-win32.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp33-none-win32.whl
Algorithm Hash digest
SHA256 902c90c7c2f754a4f45859e8ed22a243b6300601f718848bea733c42344eef36
MD5 2c6c1fce446ef5ec14d6c0c7d834282b
BLAKE2b-256 7c366240fad74df4a44c87ed0fd3fc3333219070a41fbea24fc3bceae0769ed8

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp27-none-win_amd64.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp27-none-win_amd64.whl
Algorithm Hash digest
SHA256 3a9fed2ff98f7182013057c82a4765cca7e161137533c3a25a1250a6429ac33f
MD5 fb7056f8d78eb31fb21d54de70992905
BLAKE2b-256 4df40c209197e93eaaa2c626fba0e8f513401de9babba51e9fd798d2e16923f6

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp27-none-win32.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp27-none-win32.whl
Algorithm Hash digest
SHA256 df76d9b0d0e97d125e082dd62b08a304907c282e2782e5f348b181e34838b0eb
MD5 9d62bb7481fc07dd4bcc23b94d505bdc
BLAKE2b-256 a9d863afa7d42dbd09544d039bb9e8d84e90bed0fd2225c93c9ba0b09b8c72d0

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp26-none-win_amd64.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp26-none-win_amd64.whl
Algorithm Hash digest
SHA256 236ef83fb89ed746731ea019be12052c7446507684df876397968a0cf59a076b
MD5 c15783626c33406f144e0f9b96108bed
BLAKE2b-256 ffdf0473a36a02f337b7e29bbf0d3a785162c0db6d6038d7d8479df164400d20

See more details on using hashes here.

File details

Details for the file ruamel.yaml-0.9.5-cp26-none-win32.whl.

File metadata

File hashes

Hashes for ruamel.yaml-0.9.5-cp26-none-win32.whl
Algorithm Hash digest
SHA256 cdfa7b037883880ee96055538b88c1cd2902202bb840dae23b6a92da1947db6e
MD5 b3c0f5102fe3fbbad162099a32f03055
BLAKE2b-256 6095a863abc3a05a6d4589eacc865cd43b80e569891b9474c47f1e30476982f4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page