- Timestamp:
- 01/28/11 13:40:08 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sara_cmt/sara_cmt/cluster/templatetags/ramonb_extras.py
r12439 r12442 14 14 register = template.Library() 15 15 16 17 16 class NoBlankLinesNode(template.Node): 18 17 """ … … 34 33 return NoBlankLinesNode(nodelist) 35 34 36 37 class MetaNode(template.Node):38 """39 Renderer, which stores the save path to the context.40 """41 42 def __init__(self, tag, values):43 self.tag = tag44 self.values = values45 46 def render(self, context):47 # This is where the work actually happens48 context[self.tag] = self.values49 return ''50 51 52 35 @stringfilter 53 36 def base_net(value): … … 65 48 66 49 register.filter( 'ip_last_digit', ip_last_digit ) 50 51 @register.tag(name='assign') 52 def do_assign(parser,token): 53 54 """ 55 Leipe shit ouwe 56 """ 57 mijn_poep = token.split_contents() 58 tag = mijn_poep[0] 59 new_var = mijn_poep[1] 60 is_teken = mijn_poep[2] 61 assignees = mijn_poep[3:] 62 63 return resolveVariables( new_var, assignees ) 64 65 class resolveVariables(template.Node): 66 67 def __init__(self, varname, varlist ): 68 69 self.varname = varname 70 self.varlist = varlist 71 self.resvars = [ ] 72 73 def render(self, context): 74 75 aarsvars = [ ] 76 77 for a in self.varlist: 78 79 var_str = '' 80 if not (a[0] == a[-1] and a[0] in ('"', "'")): 81 try: 82 a_var = template.Variable( a ) 83 var_str = a_var.resolve(context) 84 except template.VariableDoesNotExist: 85 #raise template.TemplateSyntaxError, 'cannot resolve variable %r' %( str( self.path ) ) 86 pass 87 88 aarsvars.append( str(var_str) ) 89 90 else: 91 a = str( a.strip("'").strip('"') ) 92 aarsvars.append( str(a) ) 93 94 #print aarsvars 95 96 context[ self.varname ] = string.join( aarsvars, '' ) 97 98 #RB: Django render functions not supposed/allowed to raise Exception, I think 99 return '' 67 100 68 101 @register.tag(name='store') … … 80 113 raise template.TemplateSyntaxError, '%r tag requires at least 1 argument' % tag 81 114 82 path = None83 84 if not (path_str[0] == path_str[-1] and path_str[0] in ('"', "'")):85 # RB: Not quoted: must be an variable: attempt to resolve to value86 try:87 path = template.Variable( path_str )88 except template.VariableDoesNotExist:89 raise template.TemplateSyntaxError, '%r tag argument 1: not an variable %r' %( tag, path_str )90 else:91 path_str = ''92 else:93 # RB: remove the quotes94 path_str = path_str[1:-1]95 96 115 # RB: parse the template thing until %endstore found 97 116 nodelist = parser.parse(('endstore',)) … … 99 118 100 119 # RB: Now lets start writing output files 101 return generateStoreOutput(tag, path , path_str, nodelist)120 return generateStoreOutput(tag, path_str, nodelist) 102 121 103 122 class generateStoreOutput(template.Node): 104 123 105 def __init__(self, tag, path , path_str, nodelist):124 def __init__(self, tag, path_str, nodelist): 106 125 self.tag = tag 107 126 self.nodelist = nodelist 108 self.path = path109 127 self.path_str = path_str 110 128 111 129 def render(self, context): 112 if self.path_str == '': 130 131 if (self.path_str[0] == self.path_str[-1] and self.path_str[0] in ('"', "'")): 132 133 mypath_str = str(self.path_str)[1:-1] 134 135 else: 136 # RB: Not quoted: must be an variable: attempt to resolve to value 113 137 try: 114 self.path_str = self.path.resolve(context) 138 pathvar = template.Variable( str(self.path_str) ) 139 mypath_str = pathvar.resolve(context) 115 140 except template.VariableDoesNotExist: 116 raise template.TemplateSyntaxError, '%r tag argument 1: cannot resolve variable %r' %( self.tag, str( self.path ))141 raise template.TemplateSyntaxError, '%r tag argument 1: not an variable %r' %( tag, path_str ) 117 142 118 143 # RB: render template between store tags 119 144 output = self.nodelist.render(context) 120 145 121 if not context.has_key( 'stores' ):122 context['stores'] = {}123 124 146 # RB: store output in context dict for later writing to file 125 context['stores'][ self.path_str ] = output147 context['stores'][ mypath_str ] = output 126 148 127 149 # RB: output generated into context dict, so we return nothing
Note: See TracChangeset
for help on using the changeset viewer.