_warnings.filterwarnings('error') import os os.chdir(os.path.dirname(os.path.abspath(os.path.normpath(__file__)))) # BEGIN INCLUDE from tdi import html from tdi import factory template = html.from_string(""" <html> <body tdi="body"> some template </body> </html> """) print template.tree template = html.from_file('loading.html') print template.tree stream = open('loading.html') try: template = html.from_stream(stream) finally: stream.close() print template.tree template = html.from_opener(factory.file_opener, 'loading.html') print template.tree
tfile1 = tempfile.NamedTemporaryFile() try: tfile1.write( """<html><body tdi="body" tdi:overlay=">foo">Yey</body></html>""" ) tfile1.flush() tfile2 = tempfile.NamedTemporaryFile() try: tfile2.write("""<tdi tdi:overlay="foo">blub</tdi>""") tfile2.flush() # 2) Load the template from_file template = ( html.from_file(tfile1.name) .overlay(html.from_file(tfile2.name)) ) print template.tree # (... wait for low-timer-resolution systems ...) time.sleep(3) # 3) Update the file tfile1.seek(0) tfile1.truncate() tfile1.write( """<xhtml><body tdi="nobody" tdi:overlay="foo">""" """Yup!</body></xhtml>""" ) tfile1.flush()
import sys import tempfile import time from tdi import html html = html.replace(autoupdate=True) tfile = tempfile.NamedTemporaryFile() try: tfile.write("""<html><body tdi="body">Yey</body></html>""") tfile.flush() # 2) Load the template from_file template = html.from_file(tfile.name) print template.tree # (... wait for low-timer-resolution systems ...) time.sleep(3) # 3) Update the file tfile.seek(0) tfile.truncate() tfile.write("""<html><body tdi="nobody">Yup!</body></html>""") tfile.flush() # 4) Voila print template.tree finally:
#!/usr/bin/env python import warnings as _warnings _warnings.resetwarnings() _warnings.filterwarnings('error') import os os.chdir(os.path.dirname(os.path.abspath(os.path.normpath(__file__)))) # BEGIN INCLUDE from tdi import factory_memoize from tdi import html t1 = html.from_file('loading.html') t2 = html.from_file('loading.html') # t1 and t2 are different template objects here print t1 is t2 # False # 1) Tell the factory that we want memoized calls html = html.replace(memoizer={}) # Wrap into the key provider html = factory_memoize.MemoizedFactory(html) t1 = html.from_file('loading.html') t2 = html.from_file('loading.html') # t1 and t2 are the same objects here print t1 is t2 # True