跳转到主要内容

Python的轻量级YAML解析器。🐓

项目描述

pyoyo

Python的轻量级YAML解析器。🐓

pyoyo 不允许反序列化任意的Python对象。支持的类型包括 strboolintfloatNoneType 以及 dictlist 值。

⚠️ 请注意,pyoyo仅支持YAML格式的一个子集,这是为了解析 cookiecutter用户配置文件 所必需的。pyoyo不支持将数据序列化成YAML格式,并且与JSON不兼容。

安装

pyoyo在 PyPI 上可用,支持Python 2.7及更高版本,可以通过 pip 进行安装

pip install poyo

此包没有其他额外需求。 📦

用法

pyoyo附带一个 parse_string() 函数,用于将UTF-8编码的字符串数据加载到Python字典中。

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)

示例

输入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 :)
    longtext: >
        This is a multiline string.
        It can contain all manners of characters.

        Single line breaks are ignored,
        but blank linkes cause line breaks.
    trueish: Falseeeeeee
    blog   : raphael.codes
    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!

输出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"longtext": (
            u"This is a multiline string. It can contain all "
            u"manners of characters.\nSingle line breaks are "
            u"ignored, but blank linkes cause line breaks.\n"
        ),
        u"trueish": u"Falseeeeeee",
        u"blog": u"raphael.codes",
        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!",
}

日志记录

poyo遵循库中的日志记录建议,这意味着它不会自行配置日志记录。其根日志记录器命名为poyo,其所有子日志记录器的名称都跟踪包/模块层次结构。poyo将日志记录到NullHandler,仅在DEBUG级别。

如果您的应用程序配置了日志记录并允许显示调试消息,则在使用poyo时您将看到日志记录。日志消息指示用于给定字符串的解析器方法,因为解析器在反序列化配置时使用。您可以通过将poyo日志记录器的日志级别设置为高于DEBUG的值来在您的应用程序中从poyo中删除所有日志记录。

禁用日志记录

import logging

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

示例调试日志配置

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
"""

parse_string(CONFIG)

示例调试日志消息

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>

关于本项目

我们创建了这个项目来解决一个依赖现有Python YAML解析器的cookiecutter版本安装问题。有关更多信息,请查阅此GitHub问题

社区

您想为做出贡献吗?您太棒了!😃

请查看good first issue标签,以找到适合您首次为poyo做出贡献的任务。您的贡献将非常受欢迎!每一份贡献都至关重要,并且始终会给予认可。

在poyo项目的代码库、问题跟踪器、聊天室和邮件列表中互动的每个人都应遵守PyPI行为准则

加入poyo社区!🌍🌏🌎

许可证

在MIT许可条款下分发,poyo是免费且开源的软件。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

poyo-0.5.0.tar.gz (15.3 kB 查看散列)

上传时间

构建分布

poyo-0.5.0-py2.py3-none-any.whl (10.2 kB 查看散列)

上传时间 Python 2 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面