| 178 | | # Make the filename relative to the template file its being loaded |
| 179 | | # from, but only if that file is specified as a relative path, or no |
| 180 | | # search path has been set up |
| 181 | | if relative_to and (not search_path or not os.path.isabs(relative_to)): |
| 182 | | filename = os.path.join(os.path.dirname(relative_to), filename) |
| 183 | | |
| 184 | | filename = os.path.normpath(filename) |
| | 177 | filename, search_path = self._resolve_paths(filename, relative_to) |
| 202 | | if os.path.isabs(filename): |
| 203 | | # Bypass the search path if the requested filename is absolute |
| 204 | | search_path = [os.path.dirname(filename)] |
| 205 | | isabs = True |
| 206 | | |
| 207 | | elif relative_to and os.path.isabs(relative_to): |
| 208 | | # Make sure that the directory containing the including |
| 209 | | # template is on the search path |
| 210 | | dirname = os.path.dirname(relative_to) |
| 211 | | if dirname not in search_path: |
| 212 | | search_path = list(search_path) + [dirname] |
| 213 | | isabs = True |
| 214 | | |
| 215 | | elif not search_path: |
| 216 | | # Uh oh, don't know where to look for the template |
| 217 | | raise TemplateError('Search path for templates not configured') |
| 218 | | |
| | 227 | def _resolve_paths(self, filename, relative_to): |
| | 228 | """Return a normalized filename and list of search paths. |
| | 229 | |
| | 230 | :param filename: path of the template file to load |
| | 231 | :param relative_to: the filename of the template from which the new |
| | 232 | template is being loaded, or ``None`` if the |
| | 233 | template is being loaded directly |
| | 234 | :return: a normalized *filename* and sequence of appropriate search |
| | 235 | paths |
| | 236 | """ |
| | 237 | search_path = self.search_path |
| | 238 | |
| | 239 | # Make the filename relative to the template file its being loaded |
| | 240 | # from, but only if that file is specified as a relative path, or no |
| | 241 | # search path has been set up |
| | 242 | if relative_to and (not search_path or not os.path.isabs(relative_to)): |
| | 243 | filename = os.path.join(os.path.dirname(relative_to), filename) |
| | 244 | |
| | 245 | filename = os.path.normpath(filename) |
| | 246 | |
| | 247 | if os.path.isabs(filename): |
| | 248 | # Bypass the search path if the requested filename is absolute |
| | 249 | search_path = [os.path.dirname(filename)] |
| | 250 | elif relative_to and os.path.isabs(relative_to): |
| | 251 | # Make sure that the directory containing the including |
| | 252 | # template is on the search path |
| | 253 | dirname = os.path.dirname(relative_to) |
| | 254 | if dirname not in search_path: |
| | 255 | search_path = list(search_path) + [dirname] |
| | 256 | elif not search_path: |
| | 257 | # Uh oh, don't know where to look for the template |
| | 258 | raise TemplateError('Search path for templates not configured') |
| | 259 | |
| | 260 | return filename, search_path |
| | 261 | |