Show More
@@ -775,48 +775,15 b' def _readmapfile(mapfile):' | |||||
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 |
|
778 | class loader(object): | |
779 |
|
779 | """Load template fragments optionally from a map file""" | ||
780 | def __init__(self, filters=None, defaults=None, resources=None, |
|
|||
781 | cache=None, aliases=(), minchunk=1024, maxchunk=65536): |
|
|||
782 | """Create template engine optionally with preloaded template fragments |
|
|||
783 |
|
780 | |||
784 | - ``filters``: a dict of functions to transform a value into another. |
|
781 | def __init__(self, cache, aliases): | |
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 | |
@@ -869,6 +836,66 b' class templater(object):' | |||||
869 | for x in tree[1:]: |
|
836 | for x in tree[1:]: | |
870 | self._findsymbolsused(x, syms) |
|
837 | self._findsymbolsused(x, syms) | |
871 |
|
838 | |||
|
839 | def symbolsused(self, t): | |||
|
840 | """Look up (keywords, filters/functions) referenced from the name | |||
|
841 | template 't' | |||
|
842 | ||||
|
843 | This may load additional templates from the map file. | |||
|
844 | """ | |||
|
845 | syms = (set(), set()) | |||
|
846 | self._findsymbolsused(self.load(t), 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 | ||||
872 | def symbolsuseddefault(self): |
|
899 | def symbolsuseddefault(self): | |
873 | """Look up (keywords, filters/functions) referenced from the default |
|
900 | """Look up (keywords, filters/functions) referenced from the default | |
874 | unnamed template |
|
901 | unnamed template | |
@@ -883,9 +910,7 b' class templater(object):' | |||||
883 |
|
910 | |||
884 | This may load additional templates from the map file. |
|
911 | This may load additional templates from the map file. | |
885 | """ |
|
912 | """ | |
886 | syms = (set(), set()) |
|
913 | return self._loader.symbolsused(t) | |
887 | self._findsymbolsused(self.load(t), syms) |
|
|||
888 | return syms |
|
|||
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""" |
General Comments 0
You need to be logged in to leave comments.
Login now