mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-09 23:10:24 +01:00
utils.serialize: lazy imports (#5224)
This commit is contained in:
@@ -6,7 +6,6 @@ from typing import TYPE_CHECKING, Any, Callable, ContextManager, Dict, Union
|
||||
from typing_extensions import Protocol
|
||||
|
||||
from dvc.exceptions import DvcException
|
||||
from dvc.utils import relpath
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from dvc.tree.base import BaseTree
|
||||
@@ -40,6 +39,8 @@ class ParseError(DvcException):
|
||||
"""Errors while parsing files"""
|
||||
|
||||
def __init__(self, path: "AnyPath", message: str):
|
||||
from dvc.utils import relpath
|
||||
|
||||
path = relpath(path)
|
||||
super().__init__(f"unable to read: '{path}', {message}")
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from contextlib import contextmanager
|
||||
|
||||
import toml
|
||||
from funcy import reraise
|
||||
|
||||
from ._common import ParseError, _dump_data, _load_data, _modify_data
|
||||
@@ -16,8 +15,10 @@ def load_toml(path, tree=None):
|
||||
|
||||
|
||||
def parse_toml(text, path, decoder=None):
|
||||
with reraise(toml.TomlDecodeError, TOMLFileCorruptedError(path)):
|
||||
return toml.loads(text, decoder=decoder)
|
||||
from toml import TomlDecodeError, loads
|
||||
|
||||
with reraise(TomlDecodeError, TOMLFileCorruptedError(path)):
|
||||
return loads(text, decoder=decoder)
|
||||
|
||||
|
||||
def parse_toml_for_update(text, path):
|
||||
@@ -27,11 +28,15 @@ def parse_toml_for_update(text, path):
|
||||
keys may be re-ordered between load/dump, but this function will at
|
||||
least preserve comments.
|
||||
"""
|
||||
decoder = toml.TomlPreserveCommentDecoder()
|
||||
from toml import TomlPreserveCommentDecoder
|
||||
|
||||
decoder = TomlPreserveCommentDecoder()
|
||||
return parse_toml(text, path, decoder=decoder)
|
||||
|
||||
|
||||
def _dump(data, stream):
|
||||
import toml
|
||||
|
||||
return toml.dump(data, stream, encoder=toml.TomlPreserveCommentEncoder())
|
||||
|
||||
|
||||
|
||||
@@ -3,9 +3,6 @@ from collections import OrderedDict
|
||||
from contextlib import contextmanager
|
||||
|
||||
from funcy import reraise
|
||||
from ruamel.yaml import YAML
|
||||
from ruamel.yaml.constructor import DuplicateKeyError
|
||||
from ruamel.yaml.error import YAMLError as _YAMLError
|
||||
|
||||
from ._common import ParseError, _dump_data, _load_data, _modify_data
|
||||
|
||||
@@ -24,6 +21,10 @@ def load_yaml(path, tree=None):
|
||||
|
||||
|
||||
def parse_yaml(text, path, typ="safe"):
|
||||
from ruamel.yaml import YAML
|
||||
from ruamel.yaml import YAMLError as _YAMLError
|
||||
from ruamel.yaml.constructor import DuplicateKeyError
|
||||
|
||||
yaml = YAML(typ=typ)
|
||||
try:
|
||||
with reraise(_YAMLError, YAMLFileCorruptedError(path)):
|
||||
@@ -48,6 +49,8 @@ def parse_yaml_for_update(text, path):
|
||||
|
||||
|
||||
def _get_yaml():
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
yaml = YAML()
|
||||
yaml.default_flow_style = False
|
||||
|
||||
@@ -67,6 +70,8 @@ def dump_yaml(path, data, tree=None):
|
||||
|
||||
|
||||
def loads_yaml(s, typ="safe"):
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
return YAML(typ=typ).load(s)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user