Skip to main content

A lightweight YAML Parser for Python

Project description

PyPI Package PyPI Python Versions PyPI Package License See Build Status on Travis CI

A lightweight YAML Parser for Python

Please note that Poyo supports only a chosen subset of the YAML format.

It can only read but not write and is not compatible with JSON.

Poyo does not allow deserialization of arbitrary Python objects. Supported types are str, bool, int, float, NoneType as well as dict and list values. Please see the examples below to get an idea of what Poyo understands.

Installation

poyo is available for download from PyPI via pip:

$ pip install poyo

Poyo is 100% Python and does not require any additional libs.

Usage

Poyo comes with a parse_string() function, to load utf-8 encoded string data into a Python dict.

import codecs
import logging

from poyo import parse_string, PoyoException

logging.basicConfig(level=logging.DEBUG)

with codecs.open('tests/foobar.yml', encoding='utf-8') as ymlfile:
    ymlstring = ymlfile.read()

try:
    config = parse_string(ymlstring)
except PoyoException as exc:
    logging.error(exc)
else:
    logging.debug(config)

Example

In (YAML):

---
default_context: # foobar
    greeting: こんにちは
    email: "raphael@hackebrot.de"
    docs: true

    gui: FALSE
    123: 456.789
    # comment
    # allthethings
    'some:int': 1000000
    foo: "hallo #welt" #Inline comment :)
    trueish: Falseeeeeee
    relative-root: /          # web app root path (default: '')
    lektor: 0.0.0.0:5000      # local build
    doc_tools:
        # docs or didn't happen
        -    mkdocs
        - 'sphinx'

        - null
    # 今日は
zZz: True
NullValue: Null

# Block
# Comment

Hello World:
    # See you at EuroPython
    null: This is madness   # yo
    gh: https://github.com/{0}.git
"Yay #python": Cool!

Out (Python):

{
    u'default_context': {
        u'greeting': u'こんにちは',
        u'email': u'raphael@hackebrot.de',
        u'docs': True,
        u'gui': False,
        u'lektor': '0.0.0.0:5000',
        u'relative-root': '/',
        123: 456.789,
        u'some:int': 1000000,
        u'foo': u'hallo #welt',
        u'trueish': u'Falseeeeeee',
        u'doc_tools': [u'mkdocs', u'sphinx', None],
    },
    u'zZz': True,
    u'NullValue': None,
    u'Hello World': {
        None: u'This is madness',
        u'gh': u'https://github.com/{0}.git',
    },
    u'Yay #python': u'Cool!'
}

Logging

Poyo follows the recommendations for logging in a library, which means it does not configure logging itself. Its root logger is named poyo and the names of all its children loggers track the package/module hierarchy. Poyo logs to a NullHandler and solely on DEBUG level.

If your application configures logging and allows debug messages to be shown, you will see logging when using Poyo. The log messages indicate which parser method is used for a given string as the parser deseralizes the config. You can remove all logging from Poyo in your application by setting the log level of the poyo logger to a value higher than DEBUG.

Disable Logging:

import logging

logging.getLogger('poyo').setLevel(logging.WARNING)

Example Debug Logging Config:

import logging
from poyo import parse_string

logging.basicConfig(level=logging.DEBUG)

CONFIG = """
---
default_context: # foobar
    greeting: こんにちは
    gui: FALSE
    doc_tools:
        # docs or didn't happen
        -    mkdocs
        - 'sphinx'
    123: 456.789
"""

logging.debug(parse_string(CONFIG))

Example Debug Logging Messages:

DEBUG:poyo.parser:parse_blankline <- \n
DEBUG:poyo.parser:parse_blankline -> IGNORED
DEBUG:poyo.parser:parse_dashes <- ---\n
DEBUG:poyo.parser:parse_dashes -> IGNORED
DEBUG:poyo.parser:parse_section <- default_context: # foobar\n
DEBUG:poyo.parser:parse_str <- default_context
DEBUG:poyo.parser:parse_str -> default_context
DEBUG:poyo.parser:parse_section -> <Section name: default_context>
DEBUG:poyo.parser:parse_simple <-     greeting: \u3053\u3093\u306b\u3061\u306f\n
DEBUG:poyo.parser:parse_str <- greeting
DEBUG:poyo.parser:parse_str -> greeting
DEBUG:poyo.parser:parse_str <- \u3053\u3093\u306b\u3061\u306f
DEBUG:poyo.parser:parse_str -> \u3053\u3093\u306b\u3061\u306f
DEBUG:poyo.parser:parse_simple -> <Simple name: greeting, value: \u3053\u3093\u306b\u3061\u306f>
DEBUG:poyo.parser:parse_simple <-     gui: FALSE\n
DEBUG:poyo.parser:parse_str <- gui
DEBUG:poyo.parser:parse_str -> gui
DEBUG:poyo.parser:parse_false <- FALSE
DEBUG:poyo.parser:parse_false -> False
DEBUG:poyo.parser:parse_simple -> <Simple name: gui, value: False>
DEBUG:poyo.parser:parse_list <-     doc_tools:\n        # docs or didn't happen\n        -    mkdocs\n        - 'sphinx'\n
DEBUG:poyo.parser:parse_str <- mkdocs
DEBUG:poyo.parser:parse_str -> mkdocs
DEBUG:poyo.parser:parse_str <- 'sphinx'
DEBUG:poyo.parser:parse_str -> sphinx
DEBUG:poyo.parser:parse_str <- doc_tools
DEBUG:poyo.parser:parse_str -> doc_tools
DEBUG:poyo.parser:parse_list -> <Simple name: doc_tools, value: ['mkdocs', 'sphinx']>
DEBUG:poyo.parser:parse_simple <-     123: 456.789\n
DEBUG:poyo.parser:parse_int <- 123
DEBUG:poyo.parser:parse_int -> 123
DEBUG:poyo.parser:parse_float <- 456.789
DEBUG:poyo.parser:parse_float -> 456.789
DEBUG:poyo.parser:parse_simple -> <Simple name: 123, value: 456.789>
DEBUG:poyo.parser:parse_simple <-     docs: true\n
DEBUG:poyo.parser:parse_str <- docs
DEBUG:poyo.parser:parse_str -> docs
DEBUG:poyo.parser:parse_true <- true
DEBUG:poyo.parser:parse_true -> True
DEBUG:poyo.parser:parse_simple -> <Simple name: docs, value: True>
DEBUG:root:{'default_context': {'docs': True, 'doc_tools': ['mkdocs', 'sphinx'], 123: 456.789, 'greeting': 'こんにちは', 'gui': False}}

WHY?!

Because a couple of cookiecutter users, including myself, ran into issues when installing well-known YAML parsers for Python on various platforms and Python versions.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Code of Conduct

Everyone interacting in the Poyo project’s codebases, issue trackers, chat rooms, and mailing lists is expected to follow the PyPA Code of Conduct.

License

Distributed under the terms of the MIT license, poyo is free and open source software.

OSI certified

Project details


Download files

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

Source Distribution

poyo-0.4.2.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

poyo-0.4.2-py2.py3-none-any.whl (8.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file poyo-0.4.2.tar.gz.

File metadata

  • Download URL: poyo-0.4.2.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for poyo-0.4.2.tar.gz
Algorithm Hash digest
SHA256 c34a5413191210ed564640510e9c4a4ba3b698746d6b454d46eb5bfb30edcd1d
MD5 5db7a089967d4f6e1f6c226a8875e8ae
BLAKE2b-256 e2743cca4e9a3d11f4f55eb7ed44d1cb09d403b7491cb03daa037f8338a09ed1

See more details on using hashes here.

File details

Details for the file poyo-0.4.2-py2.py3-none-any.whl.

File metadata

  • Download URL: poyo-0.4.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for poyo-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d1c317054145a6b1ca0608b5e676b943ddc3bfd671f886a2fe09288b98221edb
MD5 12585d428e202ef4943b3ff16ba64593
BLAKE2b-256 e016e00e3001007a5e416ca6a51def6f9e4be6a774bf1c8486d20466f834d113

See more details on using hashes here.

Supported by

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