1
0
mirror of https://github.com/esphome/esphome.git synced 2025-02-12 07:58:17 +00:00

Use YAML C loader when available

This is borrowed from HA core
527a3dba9c/homeassistant/util/yaml/loader.py (L14)
527a3dba9c/homeassistant/util/yaml/dumper.py (L12)
This commit is contained in:
J. Nick Koston 2023-11-09 16:57:48 -06:00
parent bc7519f645
commit 037f593363
No known key found for this signature in database

View File

@ -23,6 +23,18 @@ from esphome.core import (
from esphome.helpers import add_class_to_obj
from esphome.util import OrderedDict, filter_yaml_files
try:
from yaml import CSafeLoader as FastestAvailableSafeLoader
from yaml import CSafeDumper as FastestAvailableSafeDumper
except ImportError:
from yaml import ( # type: ignore[assignment]
SafeLoader as FastestAvailableSafeLoader,
)
from yaml import ( # type: ignore[assignment]
SafeDumper as FastestAvailableSafeDumper,
)
_LOGGER = logging.getLogger(__name__)
# Mostly copied from Home Assistant because that code works fine and
@ -89,7 +101,7 @@ def _add_data_ref(fn):
return wrapped
class ESPHomeLoader(yaml.SafeLoader):
class ESPHomeLoader(FastestAvailableSafeLoader):
"""Loader class that keeps track of line numbers."""
@_add_data_ref
@ -439,7 +451,7 @@ def is_secret(value):
return None
class ESPHomeDumper(yaml.SafeDumper):
class ESPHomeDumper(FastestAvailableSafeDumper):
def represent_mapping(self, tag, mapping, flow_style=None):
value = []
node = yaml.MappingNode(tag, value, flow_style=flow_style)