773 elif key != '__base__': |
773 elif key != '__base__': |
774 tmap[key] = os.path.join(base, val) |
774 tmap[key] = os.path.join(base, val) |
775 aliases.extend(conf['templatealias'].items()) |
775 aliases.extend(conf['templatealias'].items()) |
776 return cache, tmap, aliases |
776 return cache, tmap, aliases |
777 |
777 |
778 class templater(object): |
778 class loader(object): |
779 |
779 """Load template fragments optionally from a map file""" |
780 def __init__(self, filters=None, defaults=None, resources=None, |
780 |
781 cache=None, aliases=(), minchunk=1024, maxchunk=65536): |
781 def __init__(self, cache, aliases): |
782 """Create template engine optionally with preloaded template fragments |
|
783 |
|
784 - ``filters``: a dict of functions to transform a value into another. |
|
785 - ``defaults``: a dict of symbol values/functions; may be overridden |
|
786 by a ``mapping`` dict. |
|
787 - ``resources``: a resourcemapper object to look up internal data |
|
788 (e.g. cache), inaccessible from user template. |
|
789 - ``cache``: a dict of preloaded template fragments. |
|
790 - ``aliases``: a list of alias (name, replacement) pairs. |
|
791 |
|
792 self.cache may be updated later to register additional template |
|
793 fragments. |
|
794 """ |
|
795 if filters is None: |
|
796 filters = {} |
|
797 if defaults is None: |
|
798 defaults = {} |
|
799 if cache is None: |
782 if cache is None: |
800 cache = {} |
783 cache = {} |
801 self.cache = cache.copy() |
784 self.cache = cache.copy() |
802 self._map = {} |
785 self._map = {} |
803 self._filters = templatefilters.filters.copy() |
|
804 self._filters.update(filters) |
|
805 self.defaults = defaults |
|
806 self._resources = resources |
|
807 self._aliasmap = _aliasrules.buildmap(aliases) |
786 self._aliasmap = _aliasrules.buildmap(aliases) |
808 self._minchunk, self._maxchunk = minchunk, maxchunk |
|
809 |
|
810 @classmethod |
|
811 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None, |
|
812 cache=None, minchunk=1024, maxchunk=65536): |
|
813 """Create templater from the specified map file""" |
|
814 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk) |
|
815 cache, tmap, aliases = _readmapfile(mapfile) |
|
816 t.cache.update(cache) |
|
817 t._map = tmap |
|
818 t._aliasmap = _aliasrules.buildmap(aliases) |
|
819 return t |
|
820 |
787 |
821 def __contains__(self, key): |
788 def __contains__(self, key): |
822 return key in self.cache or key in self._map |
789 return key in self.cache or key in self._map |
823 |
790 |
824 def load(self, t): |
791 def load(self, t): |
867 self._findsymbolsused(tree[2], syms) |
834 self._findsymbolsused(tree[2], syms) |
868 return |
835 return |
869 for x in tree[1:]: |
836 for x in tree[1:]: |
870 self._findsymbolsused(x, syms) |
837 self._findsymbolsused(x, syms) |
871 |
838 |
872 def symbolsuseddefault(self): |
|
873 """Look up (keywords, filters/functions) referenced from the default |
|
874 unnamed template |
|
875 |
|
876 This may load additional templates from the map file. |
|
877 """ |
|
878 return self.symbolsused('') |
|
879 |
|
880 def symbolsused(self, t): |
839 def symbolsused(self, t): |
881 """Look up (keywords, filters/functions) referenced from the name |
840 """Look up (keywords, filters/functions) referenced from the name |
882 template 't' |
841 template 't' |
883 |
842 |
884 This may load additional templates from the map file. |
843 This may load additional templates from the map file. |
885 """ |
844 """ |
886 syms = (set(), set()) |
845 syms = (set(), set()) |
887 self._findsymbolsused(self.load(t), syms) |
846 self._findsymbolsused(self.load(t), syms) |
888 return syms |
847 return syms |
|
848 |
|
849 class templater(object): |
|
850 |
|
851 def __init__(self, filters=None, defaults=None, resources=None, |
|
852 cache=None, aliases=(), minchunk=1024, maxchunk=65536): |
|
853 """Create template engine optionally with preloaded template fragments |
|
854 |
|
855 - ``filters``: a dict of functions to transform a value into another. |
|
856 - ``defaults``: a dict of symbol values/functions; may be overridden |
|
857 by a ``mapping`` dict. |
|
858 - ``resources``: a resourcemapper object to look up internal data |
|
859 (e.g. cache), inaccessible from user template. |
|
860 - ``cache``: a dict of preloaded template fragments. |
|
861 - ``aliases``: a list of alias (name, replacement) pairs. |
|
862 |
|
863 self.cache may be updated later to register additional template |
|
864 fragments. |
|
865 """ |
|
866 if filters is None: |
|
867 filters = {} |
|
868 if defaults is None: |
|
869 defaults = {} |
|
870 self._filters = templatefilters.filters.copy() |
|
871 self._filters.update(filters) |
|
872 self.defaults = defaults |
|
873 self._resources = resources |
|
874 self._loader = loader(cache, aliases) |
|
875 self._minchunk, self._maxchunk = minchunk, maxchunk |
|
876 |
|
877 @classmethod |
|
878 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None, |
|
879 cache=None, minchunk=1024, maxchunk=65536): |
|
880 """Create templater from the specified map file""" |
|
881 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk) |
|
882 cache, tmap, aliases = _readmapfile(mapfile) |
|
883 t._loader.cache.update(cache) |
|
884 t._loader._map = tmap |
|
885 t._loader._aliasmap = _aliasrules.buildmap(aliases) |
|
886 return t |
|
887 |
|
888 def __contains__(self, key): |
|
889 return key in self._loader |
|
890 |
|
891 @property |
|
892 def cache(self): |
|
893 return self._loader.cache |
|
894 |
|
895 def load(self, t): |
|
896 """Get parsed tree for the given template name. Use a local cache.""" |
|
897 return self._loader.load(t) |
|
898 |
|
899 def symbolsuseddefault(self): |
|
900 """Look up (keywords, filters/functions) referenced from the default |
|
901 unnamed template |
|
902 |
|
903 This may load additional templates from the map file. |
|
904 """ |
|
905 return self.symbolsused('') |
|
906 |
|
907 def symbolsused(self, t): |
|
908 """Look up (keywords, filters/functions) referenced from the name |
|
909 template 't' |
|
910 |
|
911 This may load additional templates from the map file. |
|
912 """ |
|
913 return self._loader.symbolsused(t) |
889 |
914 |
890 def renderdefault(self, mapping): |
915 def renderdefault(self, mapping): |
891 """Render the default unnamed template and return result as string""" |
916 """Render the default unnamed template and return result as string""" |
892 return self.render('', mapping) |
917 return self.render('', mapping) |
893 |
918 |