[ZPT] Validation of a ZPT on the file system

Ian Bicking ianb at colorstudy.com
Thu Oct 7 12:13:57 EDT 2004


Hong Yuan wrote:
> The following is an exerpt on this topic from the great book The 
> Definitive Guide to Plone by Andy McKay, available at
> http://cvs.sourceforge.net/viewcvs.py/plone-docs/PloneBook/en/ch6.rst?rev=1.2&view=markup 

Thanks for the pointer.  Here's my slightly improved version of the script:

#!/usr/bin/env python
from Products.PageTemplates.PageTemplate import PageTemplate
import sys
import os

def test_something(filename):
     if os.path.isdir(filename):
         test_dir(filename)
     else:
         test_pt(filename, explicit=True)

def test_dir(dir, explicit=False):
     if VERBOSE:
         print 'Searching directory %s' % dir
     for filename in os.listdir(dir):
         if not explicit and filename.startswith('.'):
             continue
         filename = os.path.join(dir, filename)
         if os.path.isdir(filename):
             test_dir(filename)
         elif filename.endswith('.pt'):
             test_pt(filename)

def test_pt(filename):
     if VERBOSE:
         sys.stdout.write("Checking %s ..." % filename)
         sys.stdout.flush()
     raw_data = open(filename, 'r').read()
     pt = PageTemplate()
     pt.write(raw_data)
     if pt._v_errors:
         if VERBOSE:
             print
         print "*** Error in:", filename
         for error in pt._v_errors[1:]:
             print error
     elif VERBOSE:
         sys.stdout.write('done.\n')
         sys.stdout.flush()

if __name__=='__main__':
     args = sys.argv[1:]
     if '-h' in sys.argv[1:]:
         print "%s [-v] [files...]" % sys.argv[0]
         print "Default is to search the current directory."
         print "If directories, we will search recursively for .pt files"
         print "-v will print out all directories and .pt files checked."
         sys.exit(0)
     else:
         if '-v' in args:
             VERBOSE = True
             args.remove('-v')
         else:
             VERBOSE = False
         if not args:
             args = ['.']
         for arg in args:
             test_something(arg)


-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the ZPT mailing list