Changeset 13
- Timestamp:
- 13/08/04 14:00:26 (4 years ago)
- Files:
-
- trunk/airspeed.py (modified) (4 diffs)
- trunk/airspeed_test.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/airspeed.py
r12 r13 6 6 7 7 class TemplateSyntaxError(Exception): pass 8 9 10 """ 11 VARIABLE_NAME -> '[a-zA-Z]+' 12 TEXT -> '(?:[^\$#\\]|\\\\|\\\$|\\#)+' 13 TEMPLATE -> BLOCK 14 BLOCK -> TEXT 15 | PLACEHOLDER 16 | IF_DIRECTIVE 17 | BLOCK_DIRECTIVE 18 REFERENCE -> '\$' VARIABLE_VALUE 19 SILENT_REFERENCE -> '\$!' VARIABLE_VALUE 20 VARIABLE_VALUE -> VARIABLE_NAME 21 | VARIABLE_NAME '\.' VARIABLE_VALUE 22 23 24 """ 25 26 8 27 9 28 class Tokeniser: … … 16 35 RE_FLAGS = re.IGNORECASE + re.DOTALL + re.MULTILINE 17 36 EXPRESSION = '(' + NAME_OR_CALL + '(?:\.' + NAME_OR_CALL + ')*)' 37 STRING_LITERAL = "'(?:\\\\|\\'|\\n|\\b|\\t)'" 18 38 PLACEHOLDER_PATTERN = re.compile('^\$(!?)({?)' + EXPRESSION + '(}?)' + REST, RE_FLAGS) 19 39 SET_PATTERN = re.compile('^#set[ \t]*\([ \t]*\$(' + NAME + ')[ \t]*=[ \t]*(\d+|"[^"]+")[ \t]*\)' + REST, RE_FLAGS) … … 218 238 219 239 def merge(self, namespace): 240 output = StringIO.StringIO() 241 self.merge_to(namespace, output) 242 return output.getvalue() 243 244 def merge_to(self, namespace, fileobj): 220 245 output = [] 221 246 if not self.evaluator: … … 223 248 for token_type, token_value in Tokeniser().tokenise(self.content): 224 249 self.evaluator.feed(token_type, token_value) 225 output = StringIO.StringIO() 226 self.evaluator.evaluate(output, namespace) 227 return output.getvalue() 228 250 self.evaluator.evaluate(fileobj, namespace) trunk/airspeed_test.py
r12 r13 25 25 def test_silent_substitution_for_unmatched_values(self): 26 26 template = airspeed.Template("Hello $!name") 27 self.assertEquals("Hello world", template.merge({"name": "world"})) 27 28 self.assertEquals("Hello ", template.merge({})) 28 self.assertEquals("Hello world", template.merge({"name": "world"}))29 29 30 30 def test_embed_substitution_value_in_braces_gets_handled(self): … … 139 139 self.assertEquals("Stuff and more stuff", template.merge({})) 140 140 141 def test_merge_to_stream(self): 142 template = airspeed.Template('Hello $name!') 143 from cStringIO import StringIO 144 output = StringIO() 145 template.merge_to({"name": "Chris"}, output) 146 self.assertEquals('Hello Chris!', output.getvalue()) 147 148 # def test_else_block_evaluated_if_if_expression_false(self): 149 # template = airspeed.Template('#if ($value) true #else false #end') 150 # self.assertEquals(" false ", template.merge({})) 151 152 153 # 154 # TODO: 155 # 156 # Escaped characters in string literals 157 # Directives inside string literals 158 # #else, #elseif 159 # Parameterised calls 160 # #parse, #include 161 # #macro 162 # map literals 163 # Escaped $, # 164 # Sub-object assignment: #set( $customer.Behavior = $primate ) 165 # 166 167 141 168 if __name__ == '__main__': 142 169 reload(airspeed)
