项目作者: crdoconnor

项目描述 :
Type-safe YAML parser and validator.
高级语言: Python
项目地址: git://github.com/crdoconnor/strictyaml.git
创建时间: 2016-06-17T10:56:18Z
项目社区:https://github.com/crdoconnor/strictyaml

开源协议:MIT License

下载


StrictYAML

StrictYAML is a type-safe YAML parser
that parses and validates a restricted subset of the YAML
specification.

Priorities:

  • Beautiful API
  • Refusing to parse the ugly, hard to read and insecure features of YAML like the Norway problem.
  • Strict validation of markup and straightforward type casting.
  • Clear, readable exceptions with code snippets and line numbers.
  • Acting as a near-drop in replacement for pyyaml, ruamel.yaml or poyo.
  • Ability to read in YAML, make changes and write it out again with comments preserved.
  • Not speed, currently.

Simple example:

  1. # All about the character
  2. name: Ford Prefect
  3. age: 42
  4. possessions:
  5. - Towel
  1. from strictyaml import load, Map, Str, Int, Seq, YAMLError

Default parse result:

  1. >>> load(yaml_snippet)
  2. YAML({'name': 'Ford Prefect', 'age': '42', 'possessions': ['Towel']})

All data is string, list or OrderedDict:

  1. >>> load(yaml_snippet).data
  2. {'name': 'Ford Prefect', 'age': '42', 'possessions': ['Towel']}

Quickstart with schema:

  1. from strictyaml import load, Map, Str, Int, Seq, YAMLError
  2. schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())})

42 is now parsed as an integer:

  1. >>> person = load(yaml_snippet, schema)
  2. >>> person.data
  3. {'name': 'Ford Prefect', 'age': 42, 'possessions': ['Towel']}

A YAMLError will be raised if there are syntactic problems, violations of your schema or use of disallowed YAML features:

  1. # All about the character
  2. name: Ford Prefect
  3. age: 42

For example, a schema violation:

  1. try:
  2. person = load(yaml_snippet, schema)
  3. except YAMLError as error:
  4. print(error)
  1. while parsing a mapping
  2. in "<unicode string>", line 1, column 1:
  3. # All about the character
  4. ^ (line: 1)
  5. required key(s) 'possessions' not found
  6. in "<unicode string>", line 3, column 1:
  7. age: '42'
  8. ^ (line: 3)

If parsed correctly:

  1. from strictyaml import load, Map, Str, Int, Seq, YAMLError, as_document
  2. schema = Map({"name": Str(), "age": Int(), "possessions": Seq(Str())})

You can modify values and write out the YAML with comments preserved:

  1. person = load(yaml_snippet, schema)
  2. person['age'] = 43
  3. print(person.as_yaml())
  1. # All about the character
  2. name: Ford Prefect
  3. age: 43
  4. possessions:
  5. - Towel

As well as look up line numbers:

  1. >>> person = load(yaml_snippet, schema)
  2. >>> person['possessions'][0].start_line
  3. 5

And construct YAML documents from dicts or lists:

  1. print(as_document({"x": 1}).as_yaml())
  1. x: 1

Install

  1. $ pip install strictyaml

Why StrictYAML?

There are a number of formats and approaches that can achieve more or
less the same purpose as StrictYAML. I’ve tried to make it the best one.
Below is a series of documented justifications:

Using StrictYAML

How to:

Compound validators:

Scalar validators:

Restrictions:

Design justifications

There are some design decisions in StrictYAML which are controversial
and/or not obvious. Those are documented here:

Star Contributors

Other Contributors

StrictYAML also includes code from ruamel.yaml, Copyright Anthon van der Neut.

Contributing

  • Before writing any code, please read the tutorial on contributing to hitchdev libraries.
  • Before writing any code, if you’re proposing a new feature, please raise it on github. If it’s an existing feature / bug, please comment and briefly describe how you’re going to implement it.
  • All code needs to come accompanied with a story that exercises it or a modification to an existing story. This is used both to test the code and build the documentation.