Skip to content

Commit

Permalink
correct imports, add back validate func
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Dec 6, 2023
1 parent 9134d15 commit 70172dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion yacman/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from ._version import __version__
from .alias import *
from .alias import AliasedYAMLConfigManager
from .const import *
from .exceptions import *
from .yacman import YAMLConfigManager, select_config, load_yaml
33 changes: 32 additions & 1 deletion yacman/yacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def my_construct_mapping(self, node, deep=False):
DEFAULT_WAIT_TIME = 60
LOCK_PREFIX = "lock."
SCHEMA_KEY = "schema"

FILEPATH_KEY = "file_path"

from collections.abc import MutableMapping

Expand Down Expand Up @@ -304,6 +304,37 @@ def reset(self, filepath=None):
self.data = self.load(entries={}, skip_read_lock=True)
return self

def validate(self, schema=None, exclude_case=False):
"""
Validate the object against a schema
:param dict schema: a schema object to use to validate, it overrides the one
that has been provided at object construction stage
:param bool exclude_case: whether to exclude validated objects
from the error. Useful when used with large configs
"""
try:
_validate(
self.to_dict(expand=True), schema or getattr(self, SCHEMA_KEY)
)
except ValidationError as e:
_LOGGER.error(
f"{self.__class__.__name__} object did not pass schema validation"
)
# if getattr(self, FILEPATH_KEY, None) is not None:
# need to unlock locked files in case of validation error so that no
# locks are left in place
# self.make_readonly()
# commented out because I think this is taken care of my context managers now
if not exclude_case:
raise
raise ValidationError(
f"{self.__class__.__name__} object did not pass schema validation: "
f"{e.message}"
)
_LOGGER.debug("Validated successfully")


@ensure_locked
def write(self, schema=None, exclude_case=False):
"""
Expand Down

0 comments on commit 70172dd

Please sign in to comment.