1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
13 from ..loading import LiteralLocation, UriLocation
14 from .yaml import YamlReader
15 from .json import JsonReader
16 from .jinja import JinjaReader
17 from .exceptions import ReaderNotFoundError
23 '.jinja': JinjaReader}
26 class ReaderSource(object):
28 Base class for ARIA reader sources.
30 Reader sources provide appropriate :class:`Reader` instances for locations.
34 def get_reader(context, location, loader): # pylint: disable=unused-argument
35 raise ReaderNotFoundError('location: %s' % location)
38 class DefaultReaderSource(ReaderSource):
40 The default ARIA reader source will generate a :class:`YamlReader` for
41 locations that end in ".yaml", a :class:`JsonReader` for locations that
42 end in ".json", and a :class:`JinjaReader` for locations that end in
46 def __init__(self, literal_reader_class=YamlReader):
47 super(DefaultReaderSource, self).__init__()
48 self.literal_reader_class = literal_reader_class
50 def get_reader(self, context, location, loader):
51 if isinstance(location, LiteralLocation):
52 return self.literal_reader_class(context, location, loader)
54 elif isinstance(location, UriLocation):
55 for extension, reader_class in EXTENSIONS.iteritems():
56 if location.uri.endswith(extension):
57 return reader_class(context, location, loader)
59 return super(DefaultReaderSource, self).get_reader(context, location, loader)