Ticket #2539: template_02.diff

File template_02.diff, 5.7 KB (added by limodou, 18 years ago)

move rsplit to rindex and reduce duplicate path entry in path

  • template/__init__.py

     
    6060from django.template.context import Context, RequestContext, ContextPopException
    6161from django.utils.functional import curry
    6262from django.utils.text import smart_split
     63from django.utils.datastructures import SortedDict
    6364
    6465__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
    6566
     
    223224        self.tokens = tokens
    224225        self.tags = {}
    225226        self.filters = {}
     227        self.libs = {}
    226228        for lib in builtins:
    227229            self.add_library(lib)
    228230
     
    251253                # execute callback function for this tag and append resulting node
    252254                self.enter_command(command, token)
    253255                try:
    254                     compile_func = self.tags[command]
     256                    compile_func = self.get_tags(command)
    255257                except KeyError:
    256258                    self.invalid_block_tag(token, command)
    257259                try:
     
    317319    def add_library(self, lib):
    318320        self.tags.update(lib.tags)
    319321        self.filters.update(lib.filters)
     322        #save index of lib
     323        if lib.appname:
     324            d = self.libs.setdefault(lib.taglibname, SortedDict({}))
     325            d[lib.appname] = {'tags':lib.tags, 'filters':lib.filters}
    320326
     327    def get_tags(self, tag):
     328       
     329        v = tag.split('.')
     330        if len(v) == 3:
     331            appname, taglibname, command = v
     332            return self.libs[taglibname][appname]['tags'][command]
     333        elif len(v) == 2:
     334            taglibname, command = v
     335            return self.libs[taglibname].values()[0]['tags'][command]
     336        else:
     337            return self.tags[tag]
     338
     339    def get_filters(self, filter):
     340       
     341        v = filter.split('.')
     342        if len(v) == 3:
     343            appname, taglibname, command = v
     344            return self.libs[taglibname][appname]['filters'][command]
     345        elif len(v) == 2:
     346            taglibname, command = v
     347            return self.libs[taglibname].values()[0]['filters'][command]
     348        else:
     349            return self.filters[filter]
     350       
    321351    def compile_filter(self, token):
    322352        "Convenient wrapper for FilterExpression"
    323353        return FilterExpression(token, self)
    324354
    325355    def find_filter(self, filter_name):
    326         if self.filters.has_key(filter_name):
    327             return self.filters[filter_name]
    328         else:
     356        try:
     357            return self.get_filters(filter_name)
     358        except:
     359            import traceback
     360            traceback.print_exc()
    329361            raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name
     362#        if self.filters.has_key(filter_name):
     363#            return self.filters[filter_name]
     364#        else:
     365#            raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name
    330366
    331367class DebugParser(Parser):
    332368    def __init__(self, lexer):
     
    468504^"(?P<constant>%(str)s)"|
    469505^(?P<var>[%(var_chars)s]+)|
    470506 (?:%(filter_sep)s
    471      (?P<filter_name>\w+)
     507     (?P<filter_name>[\w.]+)
    472508         (?:%(arg_sep)s
    473509             (?:
    474510              %(i18n_open)s"(?P<i18n_arg>%(str)s)"%(i18n_close)s|
     
    873909            return func
    874910        return dec
    875911
     912import os.path
    876913def get_library(module_name):
    877914    lib = libraries.get(module_name, None)
    878915    if not lib:
    879916        try:
    880917            mod = __import__(module_name, '', '', [''])
     918           
     919            #get the appname and tag module name from the __file__
     920            v = mod.__file__.replace('\\', '/').split('/')
     921            if v[-2] == 'templatetags':
     922                appname, taglibname = v[-3], os.path.splitext(v[-1])[0]
     923            else:
     924                appname, taglibname = None, None
    881925        except ImportError, e:
     926            import traceback
     927            traceback.print_exc()
    882928            raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e)
    883929        try:
    884930            lib = mod.register
     931           
     932            #assign appname and taglibname to lib
     933            lib.appname = appname
     934            lib.taglibname = taglibname
    885935            libraries[module_name] = lib
    886936        except AttributeError:
    887937            raise InvalidTemplateLibrary, "Template library %s does not have a variable named 'register'" % module_name
  • template/defaulttags.py

     
    686686    for taglib in bits[1:]:
    687687        # add the library to the parser
    688688        try:
    689             lib = get_library("django.templatetags.%s" % taglib.split('.')[-1])
     689            if taglib.find('.') > -1:
     690                appname, module = taglib.split('.')
     691                taglib = '.'.join([appname, 'templatetags', module])
     692            lib = get_library("django.templatetags.%s" % taglib)
    690693            parser.add_library(lib)
    691694        except InvalidTemplateLibrary, e:
     695            import traceback
     696            traceback.print_exc()
    692697            raise TemplateSyntaxError, "'%s' is not a valid tag library: %s" % (taglib, e)
    693698    return LoadNode()
    694699load = register.tag(load)
  • templatetags/__init__.py

     
    22
    33for a in settings.INSTALLED_APPS:
    44    try:
     5        path = __import__(a[:a.rindex('.')], '', '', ['']).__path__
     6        for i in path:
     7            if not i in __path__:
     8                __path__.append(i)
    59        __path__.extend(__import__(a + '.templatetags', '', '', ['']).__path__)
    610    except ImportError:
    711        pass
Back to Top