File size: 5,278 Bytes
622387b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
diff --git a/confit.py b/confit.py
index 1048f30..1f6f21a 100644
--- a/confit.py
+++ b/confit.py
@@ -1105,6 +1105,48 @@ class Filename(Template):
 
         return 'Filename({0})'.format(', '.join(args))
 
+    def template_with_relatives(self, view, template):
+        """ Make a template containing only the Filenames needed to resolve
+        relative paths.
+        """
+        # make shallow copy (we only modify the dict with pop)
+        template = dict(template.subtemplates)
+
+        # save time by skipping MappingTemplate's init loop
+        new_template = MappingTemplate({})
+        new_subtemplates = new_template.subtemplates
+
+        next_relative = self.relative_to
+
+        # gather all the needed templates and nothing else
+        while next_relative is not None:
+            try:
+                # pop to avoid infinite loop because of recursive
+                # relative paths
+                next_template = template.pop(next_relative)
+            except KeyError:
+                if next_relative in new_subtemplates:
+                    # we encountered this config key previously
+                    raise ConfigTemplateError((
+                        '{0} and {1} are recursively relative'
+                    ).format(view.name, self.relative_to))
+                else:
+                    raise ConfigTemplateError((
+                        'missing template for {0}, needed to expand {1}\'s' +
+                        'relative path'
+                    ).format(self.relative_to, view.name))
+
+            if isinstance(next_template, Filename):
+                new_subtemplates[next_relative] = next_template
+                next_relative = next_template.relative_to
+            else:
+                raise ConfigTemplateError((
+                    '{0} can not be relative to non Filename key {1}')
+                    .format(view.name, next_relative)
+                )
+
+        return new_template
+
     def resolve_relative_to(self, view, template):
         if not isinstance(template,
                 (collections.Mapping, MappingTemplate)):
@@ -1118,8 +1160,11 @@ class Filename(Template):
                 '{0} is relative to itself'.format(view.name)
             )
 
-        elif self.relative_to not in view.parent.keys():
-            # self.relative_to is not in the config
+        elif self.relative_to not in view.parent.keys() and (
+            self.relative_to not in template.subtemplates or
+            template.subtemplates[self.relative_to].default == REQUIRED
+        ):
+            # self.relative_to is not in the config and has no default
             self.fail(
                 (
                     'needs sibling value "{0}" to expand relative path'
@@ -1127,39 +1172,26 @@ class Filename(Template):
                 view
             )
 
-        old_template = {}
-        old_template.update(template.subtemplates)
-
-        # save time by skipping MappingTemplate's init loop
-        next_template = MappingTemplate({})
-        next_relative = self.relative_to
-
-        # gather all the needed templates and nothing else
-        while next_relative is not None:
-            try:
-                # pop to avoid infinite loop because of recursive
-                # relative paths
-                rel_to_template = old_template.pop(next_relative)
-            except KeyError:
-                if next_relative in template.subtemplates:
-                    # we encountered this config key previously
-                    raise ConfigTemplateError((
-                        '{0} and {1} are recursively relative'
-                    ).format(view.name, self.relative_to))
-                else:
-                    raise ConfigTemplateError((
-                        'missing template for {0}, needed to expand {1}\'s' +
-                        'relative path'
-                    ).format(self.relative_to, view.name))
-
-            next_template.subtemplates[next_relative] = rel_to_template
-            next_relative = rel_to_template.relative_to
-
-        return view.parent.get(next_template)[self.relative_to]
+        return view.parent.get(
+            self.template_with_relatives(view, template)
+        )[self.relative_to]
 
     def value(self, view, template=None):
-        path, source = view.first()
+        try:
+            path, source = view.first()
+            used_default = False
+        except NotFoundError:
+            if self.default is REQUIRED:
+                raise NotFoundError("{0} not found".format(view.name))
+            else:
+                used_default = True
+                path = self.default
+
         if not isinstance(path, BASESTRING):
+            # return non path default as is
+            if used_default:
+                return path
+
             self.fail(
                 'must be a filename, not {0}'.format(type(path).__name__),
                 view,
@@ -1178,7 +1210,7 @@ class Filename(Template):
                     path,
                 )
 
-            elif source.filename:
+            elif not used_default and source.filename:
                 # From defaults: relative to the app's directory.
                 path = os.path.join(view.root().config_dir(), path)