Edgewall Software

Ticket #519: abspath_resolution.diff

File abspath_resolution.diff, 4.0 KB (added by jason kirtland <jek@…>, 12 years ago)

Diff against trunk r1185

  • genshi/template/loader.py

     
    173173        """
    174174        if cls is None:
    175175            cls = self.default_class
    176         search_path = self.search_path
    177176
    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)
    185178        cachekey = filename
    186179
    187180        self._lock.acquire()
     
    197190            except (KeyError, OSError):
    198191                pass
    199192
    200             isabs = False
     193            isabs = os.path.isabs(filename)
    201194
    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 
    219195            for loadfunc in search_path:
    220196                if isinstance(loadfunc, basestring):
    221                     loadfunc = directory(loadfunc)
     197                    loadfunc = self.directory(loadfunc)
    222198                try:
    223199                    filepath, filename, fileobj, uptodate = loadfunc(filename)
    224200                except IOError:
     
    248224        finally:
    249225            self._lock.release()
    250226
     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
    251262    def _instantiate(self, cls, fileobj, filepath, filename, encoding=None):
    252263        """Instantiate and return the `Template` object based on the given
    253264        class and parameters.