Update use of safe_load and safe_dump to load and dump using the CSafeLoader with SafeLoader as a fallback. Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
37 lines
892 B
Python
37 lines
892 B
Python
from __future__ import unicode_literals
|
|
|
|
import yaml
|
|
|
|
try:
|
|
from yaml import CSafeLoader as SafeLoader
|
|
except ImportError:
|
|
from yaml import SafeLoader
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.deconstruct import deconstructible
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
@deconstructible
|
|
class YAMLValidator(object):
|
|
"""
|
|
Validates that the input is YAML compliant.
|
|
"""
|
|
def __call__(self, value):
|
|
value = value.strip()
|
|
try:
|
|
yaml.load(stream=value, Loader=SafeLoader)
|
|
except yaml.error.YAMLError:
|
|
raise ValidationError(
|
|
_('Enter a valid YAML value.'),
|
|
code='invalid'
|
|
)
|
|
|
|
def __eq__(self, other):
|
|
return (
|
|
isinstance(other, YAMLValidator)
|
|
)
|
|
|
|
def __ne__(self, other):
|
|
return not (self == other)
|