forked from jpadilla/django-rest-framework-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers.py
34 lines (26 loc) · 981 Bytes
/
parsers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Provides YAML parsing support.
"""
from __future__ import unicode_literals
from django.conf import settings
from django.utils import six
from rest_framework.exceptions import ParseError
from rest_framework.parsers import BaseParser
from .compat import yaml
class YAMLParser(BaseParser):
"""
Parses YAML-serialized data.
"""
media_type = 'application/yaml'
def parse(self, stream, media_type=None, parser_context=None):
"""
Parses the incoming bytestream as YAML and returns the resulting data.
"""
assert yaml, 'YAMLParser requires pyyaml to be installed'
parser_context = parser_context or {}
encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
try:
data = stream.read().decode(encoding)
return yaml.safe_load(data)
except (ValueError, yaml.parser.ParserError) as exc:
raise ParseError('YAML parse error - %s' % six.text_type(exc))