806 if defaults is None: |
806 if defaults is None: |
807 defaults = {} |
807 defaults = {} |
808 if cache is None: |
808 if cache is None: |
809 cache = {} |
809 cache = {} |
810 self.cache = cache.copy() |
810 self.cache = cache.copy() |
811 self.map = {} |
811 self._map = {} |
812 self.filters = templatefilters.filters.copy() |
812 self._filters = templatefilters.filters.copy() |
813 self.filters.update(filters) |
813 self._filters.update(filters) |
814 self.defaults = defaults |
814 self.defaults = defaults |
815 self._resources = resources |
815 self._resources = resources |
816 self._aliases = aliases |
816 self._aliases = aliases |
817 self.minchunk, self.maxchunk = minchunk, maxchunk |
817 self._minchunk, self._maxchunk = minchunk, maxchunk |
818 self.ecache = {} |
818 self._ecache = {} |
819 |
819 |
820 @classmethod |
820 @classmethod |
821 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None, |
821 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None, |
822 cache=None, minchunk=1024, maxchunk=65536): |
822 cache=None, minchunk=1024, maxchunk=65536): |
823 """Create templater from the specified map file""" |
823 """Create templater from the specified map file""" |
824 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk) |
824 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk) |
825 cache, tmap, aliases = _readmapfile(mapfile) |
825 cache, tmap, aliases = _readmapfile(mapfile) |
826 t.cache.update(cache) |
826 t.cache.update(cache) |
827 t.map = tmap |
827 t._map = tmap |
828 t._aliases = aliases |
828 t._aliases = aliases |
829 return t |
829 return t |
830 |
830 |
831 def __contains__(self, key): |
831 def __contains__(self, key): |
832 return key in self.cache or key in self.map |
832 return key in self.cache or key in self._map |
833 |
833 |
834 def load(self, t): |
834 def load(self, t): |
835 '''Get the template for the given template name. Use a local cache.''' |
835 '''Get the template for the given template name. Use a local cache.''' |
836 if t not in self.cache: |
836 if t not in self.cache: |
837 try: |
837 try: |
838 self.cache[t] = util.readfile(self.map[t][1]) |
838 self.cache[t] = util.readfile(self._map[t][1]) |
839 except KeyError as inst: |
839 except KeyError as inst: |
840 raise templateutil.TemplateNotFound( |
840 raise templateutil.TemplateNotFound( |
841 _('"%s" not in template map') % inst.args[0]) |
841 _('"%s" not in template map') % inst.args[0]) |
842 except IOError as inst: |
842 except IOError as inst: |
843 reason = (_('template file %s: %s') |
843 reason = (_('template file %s: %s') |
844 % (self.map[t][1], |
844 % (self._map[t][1], |
845 stringutil.forcebytestr(inst.args[1]))) |
845 stringutil.forcebytestr(inst.args[1]))) |
846 raise IOError(inst.args[0], encoding.strfromlocal(reason)) |
846 raise IOError(inst.args[0], encoding.strfromlocal(reason)) |
847 return self.cache[t] |
847 return self.cache[t] |
848 |
848 |
849 def renderdefault(self, mapping): |
849 def renderdefault(self, mapping): |
855 return b''.join(self.generate(t, mapping)) |
855 return b''.join(self.generate(t, mapping)) |
856 |
856 |
857 def generate(self, t, mapping): |
857 def generate(self, t, mapping): |
858 """Return a generator that renders the specified named template and |
858 """Return a generator that renders the specified named template and |
859 yields chunks""" |
859 yields chunks""" |
860 ttype = t in self.map and self.map[t][0] or 'default' |
860 ttype = t in self._map and self._map[t][0] or 'default' |
861 if ttype not in self.ecache: |
861 if ttype not in self._ecache: |
862 try: |
862 try: |
863 ecls = engines[ttype] |
863 ecls = engines[ttype] |
864 except KeyError: |
864 except KeyError: |
865 raise error.Abort(_('invalid template engine: %s') % ttype) |
865 raise error.Abort(_('invalid template engine: %s') % ttype) |
866 self.ecache[ttype] = ecls(self.load, self.filters, self.defaults, |
866 self._ecache[ttype] = ecls(self.load, self._filters, self.defaults, |
867 self._resources, self._aliases) |
867 self._resources, self._aliases) |
868 proc = self.ecache[ttype] |
868 proc = self._ecache[ttype] |
869 |
869 |
870 stream = proc.process(t, mapping) |
870 stream = proc.process(t, mapping) |
871 if self.minchunk: |
871 if self._minchunk: |
872 stream = util.increasingchunks(stream, min=self.minchunk, |
872 stream = util.increasingchunks(stream, min=self._minchunk, |
873 max=self.maxchunk) |
873 max=self._maxchunk) |
874 return stream |
874 return stream |
875 |
875 |
876 def templatepaths(): |
876 def templatepaths(): |
877 '''return locations used for template files.''' |
877 '''return locations used for template files.''' |
878 pathsrel = ['templates'] |
878 pathsrel = ['templates'] |