1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from ...utils.uris import as_file
22 class Location(object):
24 Base class for ARIA locations.
26 Locations are used by :class:`~aria.parser.loading.LoaderSource` to delegate to
27 an appropriate :class:`~aria.parser.loading.Loader`.
30 def is_equivalent(self, location):
31 raise NotImplementedError
38 class UriLocation(Location):
40 A URI location can be absolute or relative, and can include a scheme or not.
42 If no scheme is included, it should be treated as a filesystem path.
44 See :class:`~aria.parser.loading.UriTextLoader`.
47 def __init__(self, uri):
50 def is_equivalent(self, location):
51 return isinstance(location, UriLocation) and (location.uri == self.uri)
55 prefix = os.path.dirname(self.uri)
56 if prefix and (as_file(prefix) is None):
57 # Yes, it's weird, but dirname handles URIs,
58 # too: http://stackoverflow.com/a/35616478/849021
59 # We just need to massage it with a trailing slash
67 class LiteralLocation(Location):
69 A location that embeds content.
71 See :class:`~aria.parser.loading.LiteralLoader`.
74 def __init__(self, content, name='literal'):
75 self.content = content
78 def is_equivalent(self, location):
79 return isinstance(location, LiteralLocation) and (location.content == self.content)
82 return '<%s>' % self.name