summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/parser/loading/location.py
blob: 902e856c1ad3fd892826e5153e7ecfd37f5ac1ad (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import os

from ...utils.uris import as_file


class Location(object):
    """
    Base class for ARIA locations.

    Locations are used by :class:`~aria.parser.loading.LoaderSource` to delegate to
    an appropriate :class:`~aria.parser.loading.Loader`.
    """

    def is_equivalent(self, location):
        raise NotImplementedError

    @property
    def prefix(self):
        return None


class UriLocation(Location):
    """
    A URI location can be absolute or relative, and can include a scheme or not.

    If no scheme is included, it should be treated as a filesystem path.

    See :class:`~aria.parser.loading.UriTextLoader`.
    """

    def __init__(self, uri):
        self.uri = uri

    def is_equivalent(self, location):
        return isinstance(location, UriLocation) and (location.uri == self.uri)

    @property
    def prefix(self):
        prefix = os.path.dirname(self.uri)
        if prefix and (as_file(prefix) is None):
            # Yes, it's weird, but dirname handles URIs,
            # too: http://stackoverflow.com/a/35616478/849021
            # We just need to massage it with a trailing slash
            prefix += '/'
        return prefix

    def __str__(self):
        return self.uri


class LiteralLocation(Location):
    """
    A location that embeds content.

    See :class:`~aria.parser.loading.LiteralLoader`.
    """

    def __init__(self, content, name='literal'):
        self.content = content
        self.name = name

    def is_equivalent(self, location):
        return isinstance(location, LiteralLocation) and (location.content == self.content)

    def __str__(self):
        return '<%s>' % self.name