Dump the complete stream into a file or file-like object. Per default unicode strings are written, if you want to encode before writing specify an `encoding`. Example usage:: Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
(self, fp, encoding=None, errors='strict')
| 1204 | self.disable_buffering() |
| 1205 | |
| 1206 | def dump(self, fp, encoding=None, errors='strict'): |
| 1207 | """Dump the complete stream into a file or file-like object. |
| 1208 | Per default unicode strings are written, if you want to encode |
| 1209 | before writing specify an `encoding`. |
| 1210 | |
| 1211 | Example usage:: |
| 1212 | |
| 1213 | Template('Hello {{ name }}!').stream(name='foo').dump('hello.html') |
| 1214 | """ |
| 1215 | close = False |
| 1216 | if isinstance(fp, string_types): |
| 1217 | if encoding is None: |
| 1218 | encoding = 'utf-8' |
| 1219 | fp = open(fp, 'wb') |
| 1220 | close = True |
| 1221 | try: |
| 1222 | if encoding is not None: |
| 1223 | iterable = (x.encode(encoding, errors) for x in self) |
| 1224 | else: |
| 1225 | iterable = self |
| 1226 | if hasattr(fp, 'writelines'): |
| 1227 | fp.writelines(iterable) |
| 1228 | else: |
| 1229 | for item in iterable: |
| 1230 | fp.write(item) |
| 1231 | finally: |
| 1232 | if close: |
| 1233 | fp.close() |
| 1234 | |
| 1235 | def disable_buffering(self): |
| 1236 | """Disable the output buffering.""" |