Django

Code

Changeset 1372

Show
Ignore:
Timestamp:
11/23/05 13:05:55 (3 years ago)
Author:
adrian
Message:

new-admin: Negligible syntax changes in django.core.template in preparation for merge to trunk

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/new-admin/django/core/template/defaulttags.py

    r1125 r1372  
    193193            grouper = resolve_variable_with_filters('var.%s' % self.expression, \ 
    194194                Context({'var': obj})) 
    195             #TODO: Is this a sensible way to determine equality?  
     195            # TODO: Is this a sensible way to determine equality? 
    196196            if output and repr(output[-1]['grouper']) == repr(grouper): 
    197197                output[-1]['list'].append(obj) 
  • django/branches/new-admin/django/core/template/__init__.py

    r1299 r1372  
    7575ALLOWED_VARIABLE_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.' 
    7676 
    77 #What to report as the origin of templates that come from non loader sources (ie strings) 
     77# what to report as the origin for templates that come from non-loader sources 
     78# (e.g. strings) 
    7879UNKNOWN_SOURCE="<unknown source>" 
    7980 
     
    108109    def __init__(self, name): 
    109110        self.name = name 
    110          
     111 
    111112    def reload(self): 
    112113        raise NotImplementedException 
     
    119120        super(StringOrigin, self).__init__(UNKNOWN_SOURCE) 
    120121        self.source = source 
    121      
     122 
    122123    def reload(self): 
    123124        return self.source 
     
    128129        if TEMPLATE_DEBUG and origin == None: 
    129130            origin = StringOrigin(template_string) 
    130             #Could do some crazy stack frame stuff to record where this string came from... 
     131            # Could do some crazy stack-frame stuff to record where this string 
     132            # came from... 
    131133        self.nodelist = compile_string(template_string, origin) 
    132         
     134 
    133135    def __iter__(self): 
    134136        for node in self.nodelist: 
     
    141143 
    142144def compile_string(template_string, origin): 
    143     "Compiles template_string into NodeList ready for rendering"      
     145    "Compiles template_string into NodeList ready for rendering" 
    144146    lexer = lexer_factory(template_string, origin) 
    145147    parser = parser_factory(lexer.tokenize()) 
     
    187189                return True 
    188190        return False 
    189      
     191 
    190192    def get(self, key, otherwise): 
    191193        for dict in self.dicts: 
     
    205207    def __str__(self): 
    206208        return '<%s token: "%s...">' % ( 
    207             {TOKEN_TEXT:'Text', TOKEN_VAR:'Var', TOKEN_BLOCK:'Block'}[self.token_type], 
     209            {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type], 
    208210            self.contents[:20].replace('\n', '') 
    209211            ) 
    210              
     212 
    211213    def __repr__(self): 
    212214        return '<%s token: "%s">' % ( 
    213             {TOKEN_TEXT:'Text', TOKEN_VAR:'Var', TOKEN_BLOCK:'Block'}[self.token_type], 
     215            {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type], 
    214216            self.contents[:].replace('\n', '') 
    215217            ) 
     
    219221        self.template_string = template_string 
    220222        self.origin = origin 
    221      
     223 
    222224    def tokenize(self): 
    223225        "Return a list of tokens from a given template_string" 
     
    225227        bits = filter(None, tag_re.split(self.template_string)) 
    226228        return map(self.create_token, bits) 
    227          
     229 
    228230    def create_token(self,token_string): 
    229231        "Convert the given token string into a new Token object and return it" 
     
    234236        else: 
    235237            token = Token(TOKEN_TEXT, token_string) 
    236         return token  
     238        return token 
    237239 
    238240class DebugLexer(Lexer): 
    239241    def __init__(self, template_string, origin): 
    240         super(DebugLexer,self).__init__(template_string, origin) 
     242        super(DebugLexer, self).__init__(template_string, origin) 
    241243 
    242244    def tokenize(self): 
     
    245247        for match in tag_re.finditer(self.template_string): 
    246248            start, end = match.span() 
    247             if start > upto:        
    248                 token_tups.append( (self.template_string[upto:start], (upto, start) ) ) 
     249            if start > upto: 
     250                token_tups.append( (self.template_string[upto:start], (upto, start)) ) 
    249251                upto = start 
    250             token_tups.append( (self.template_string[start:end], (start,end) ) ) 
     252            token_tups.append( (self.template_string[start:end], (start,end)) ) 
    251253            upto = end 
    252254        last_bit = self.template_string[upto:] 
    253255        if last_bit: 
    254            token_tups.append( (last_bit, (upto, upto + len(last_bit) ) ) ) 
    255         return [ self.create_token(tok, (self.origin, loc)) for tok, loc in token_tups] 
     256           token_tups.append( (last_bit, (upto, upto + len(last_bit))) ) 
     257        return [self.create_token(tok, (self.origin, loc)) for tok, loc in token_tups] 
    256258 
    257259    def create_token(self, token_string, source): 
     
    289291                    compile_func = registered_tags[command] 
    290292                except KeyError: 
    291                     self.invalid_block_tag(token, command)  
     293                    self.invalid_block_tag(token, command) 
    292294                try: 
    293295                    compiled_result = compile_func(self, token) 
     
    306308    def create_nodelist(self): 
    307309        return NodeList() 
    308      
     310 
    309311    def extend_nodelist(self, nodelist, node, token): 
    310312        nodelist.append(node) 
     
    312314    def enter_command(self, command, token): 
    313315        pass 
    314          
     316 
    315317    def exit_command(self): 
    316318        pass 
     
    321323    def empty_variable(self, token): 
    322324        raise self.error( token, "Empty variable tag") 
    323      
     325 
    324326    def empty_block_tag(self, token): 
    325327        raise self.error( token, "Empty block tag") 
    326      
     328 
    327329    def invalid_block_tag(self, token, command): 
    328330        raise self.error( token, "Invalid block tag: '%s'" % command) 
    329      
     331 
    330332    def unclosed_block_tag(self, parse_until): 
    331333        raise self.error(None, "Unclosed tags: %s " %  ', '.join(parse_until)) 
     
    333335    def compile_function_error(self, token, e): 
    334336        pass 
    335          
     337 
    336338    def next_token(self): 
    337339        return self.tokens.pop(0) 
     
    342344    def delete_first_token(self): 
    343345        del self.tokens[0] 
    344      
     346 
    345347class DebugParser(Parser): 
    346348    def __init__(self, lexer): 
     
    350352    def enter_command(self, command, token): 
    351353        self.command_stack.append( (command, token.source) ) 
    352          
     354 
    353355    def exit_command(self): 
    354356        self.command_stack.pop() 
     
    371373        node.source = token.source 
    372374        super(DebugParser, self).extend_nodelist(nodelist, node, token) 
    373      
     375 
    374376    def unclosed_block_tag(self, parse_until): 
    375377        (command, source) = self.command_stack.pop() 
    376         msg = "Unclosed tag '%s'. Looking for one of: %s " % \ 
    377               (command, ', '.join(parse_until) )  
     378        msg = "Unclosed tag '%s'. Looking for one of: %s " % (command, ', '.join(parse_until)) 
    378379        raise self.source_error( source, msg) 
    379380 
     
    662663    'arg_sep': re.escape(FILTER_ARGUMENT_SEPARATOR), 
    663664    'i18n_open' : re.escape("_("), 
    664     'i18n_close' : re.escape(")"),                          
     665    'i18n_close' : re.escape(")"), 
    665666  } 
    666    
     667 
    667668filter_raw_string = filter_raw_string.replace("\n", "").replace(" ", "") 
    668669filter_re = re.compile(filter_raw_string) 
    669670 
    670671class RegexFilterParser(object): 
    671     """ Not used yet because of i18n""" 
    672      
     672    "Not used yet because of i18n" 
    673673    def __init__(self, token): 
    674674        matches = filter_re.finditer(token) 
     
    828828        except Exception: 
    829829            from sys import exc_info 
    830             wrapped = TemplateSyntaxError( 'Caught exception whilst rendering'
     830            wrapped = TemplateSyntaxError('Caught an exception while rendering.'
    831831            wrapped.source = node.source 
    832832            wrapped.exc_info = exc_info() 
     
    859859        else: 
    860860            return output 
    861          
     861 
    862862    def render(self, context): 
    863863        output = resolve_variable_with_filters(self.var_string, context) 
  • django/branches/new-admin/django/core/template/loader.py

    r1363 r1372  
    88# name is the template name. 
    99# dirs is an optional list of directories to search instead of TEMPLATE_DIRS. 
     10# 
    1011# The loader should return a tuple of (template_source, path). The path returned 
    11 # will be shown to the user for debugging purposes, so it should identify where the template  
    12 # was loaded from.   
     12# might be shown to the user for debugging purposes, so it should identify where 
     13# the template was loaded from. 
    1314# 
    1415# Each loader should have an "is_usable" attribute set. This is a boolean that 
     
    4647        super(LoaderOrigin, self).__init__(display_name) 
    4748        self.loader, self.loadname, self.dirs = loader, name, dirs 
    48      
     49 
    4950    def reload(self): 
    5051        return self.loader(self.loadname, self.dirs)[0] 
     
    9091    context. The template_name may be a string to load a single template using 
    9192    get_template, or it may be a tuple to use select_template to find one of 
    92     the templates in the list. Returns a string.  
     93    the templates in the list. Returns a string. 
    9394    """ 
    9495    dictionary = dictionary or {} 
     
    100101        context_instance.update(dictionary) 
    101102    else: 
    102         context_instance = Context(dictionary)  
     103        context_instance = Context(dictionary) 
    103104    return t.render(context_instance) 
    104105 
     
    186187        try: 
    187188            t = get_template(template_path) 
    188             self.nodelist = t.nodelis
     189            self.template =
    189190        except Exception, e: 
    190191            if TEMPLATE_DEBUG: 
    191192                raise 
    192             self.nodelist = None 
    193  
    194     def render(self, context): 
    195         if self.nodelist
    196             return self.nodelist.render(context) 
     193            self.template = None 
     194 
     195    def render(self, context): 
     196        if self.template
     197            return self.template.render(context) 
    197198        else: 
    198199            return '' 
    199200 
    200201class IncludeNode(Node): 
    201     def __init__(self, template_path_var): 
    202         self.template_path_var = template_path_var 
     202    def __init__(self, template_name): 
     203        self.template_name = template_name 
    203204 
    204205    def render(self, context): 
    205206         try: 
    206              template_path = resolve_variable(self.template_path_var, context) 
    207              t = get_template(template_path
     207             template_name = resolve_variable(self.template_name, context) 
     208             t = get_template(template_name
    208209             return t.render(context) 
    209210         except Exception, e: 
  • django/branches/new-admin/django/core/template/loaders/app_directories.py

    r1028 r1372  
    2424    if os.path.isdir(template_dir): 
    2525        app_template_dirs.append(template_dir) 
    26          
     26 
    2727# It won't change, so convert it to a tuple to save memory. 
    2828app_template_dirs = tuple(app_template_dirs) 
  • django/branches/new-admin/django/core/template/loaders/eggs.py

    r913 r1372  
    1919        for app in INSTALLED_APPS: 
    2020            try: 
    21                 return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name))  
     21                return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name)) 
    2222            except: 
    2323                pass