[ZPT] CVS: Zope3/lib/python/Zope/TAL - driver.py:1.25.18.3.4.1

Barry Warsaw barry@wooz.org
Mon, 10 Jun 2002 15:56:55 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/TAL
In directory cvs.zope.org:/tmp/cvs-serv13496/lib/python/Zope/TAL

Modified Files:
      Tag: fdrake-tal-i18n-branch
	driver.py 
Log Message:
Move the usage information into the module docstring and add a
"standard" usage() function.  Note that if we decide to backport this
into Zope2, we'll need to de-Python-2.1-ify it.

main(): Moved -h (html) option to -H so we can use -h/--help as the
standard ways to get help from a script.

Also, remove the version test stuff and the -n flag.  Some local
variable renaming for clarity.

Added the -i flag to display the pre-interpolation value of i18n
message strings, e.g. "${name} lives in ${country}"


=== Zope3/lib/python/Zope/TAL/driver.py 1.25.18.3 => 1.25.18.3.4.1 ===
 """
 Driver program to test METAL and TAL implementation.
+
+Usage: driver.py [options] [file]
+Options:
+    -h / --help
+        Print this message and exit.
+    -H / --html
+    -x / --xml
+        Explicitly choose HTML or XML input.  The default is to automatically
+        select based on the file extension.  These options are mutually
+        exclusive.
+    -l
+        Lenient structure insertion.
+    -m
+        Macro expansion only
+    -s
+        Print intermediate opcodes only
+    -t
+        Leave TAL/METAL attributes in output
+    -i
+        Leave I18N substitution strings un-interpolated
 """
 
 import os
@@ -30,64 +50,70 @@
 
 FILE = "tests/input/test01.xml"
 
+def usage(code, msg=''):
+    # Python 2.1 required
+    print >> sys.stderr, __doc__
+    if msg:
+        print >> sys.stderr, msg
+    sys.exit(code)
+
 def main():
-    versionTest = 1
     macros = 0
     mode = None
     showcode = 0
     showtal = -1
     strictinsert = 1
+    i18nInterpolate = 1
     try:
-        opts, args = getopt.getopt(sys.argv[1:], "hxlmnst")
+        opts, args = getopt.getopt(sys.argv[1:], "hHxlmsti",
+                                   ['help', 'html', 'xml'])
     except getopt.error, msg:
-        sys.stderr.write("\n%s\n" % str(msg))
-        sys.stderr.write(
-            "usage: driver.py [-h|-x] [-l] [-m] [-n] [-s] [-t] [file]\n")
-        sys.stderr.write("-h/-x -- HTML/XML input (default auto)\n")
-        sys.stderr.write("-l -- lenient structure insertion\n")
-        sys.stderr.write("-m -- macro expansion only\n")
-        sys.stderr.write("-n -- turn off the Python 1.5.2 test\n")
-        sys.stderr.write("-s -- print intermediate code\n")
-        sys.stderr.write("-t -- leave tal/metal attributes in output\n")
-        sys.exit(2)
-    for o, a in opts:
-        if o == '-h':
+        usage(2, msg)
+    for opt, arg in opts:
+        if opt in ('-h', '--help'):
+            usage(0)
+        if opt in ('-H', '--html'):
+            if mode == 'xml':
+                usage(1, '--html and --xml are mutually exclusive')
             mode = "html"
-        if o == '-l':
+        if opt == '-l':
             strictinsert = 0
-        if o == '-m':
+        if opt == '-m':
             macros = 1
-        if o == '-n':
+        if opt == '-n':
             versionTest = 0
-        if o == '-x':
+        if opt in ('-x', '--xml'):
+            if mode == 'html':
+                usage(1, '--html and --xml are mutually exclusive')
             mode = "xml"
-        if o == '-s':
+        if opt == '-s':
             showcode = 1
-        if o == '-t':
+        if opt == '-t':
             showtal = 1
-    if not versionTest:
-        if sys.version[:5] != "1.5.2":
-            sys.stderr.write(
-                "Use Python 1.5.2 only; use -n to disable this test\n")
-            sys.exit(2)
+        if opt == '-i':
+            i18nInterpolate = 0
     if args:
         file = args[0]
     else:
         file = FILE
     it = compilefile(file, mode)
-    if showcode: showit(it)
-    else: interpretit(it, tal=(not macros), showtal=showtal,
-                      strictinsert=strictinsert)
+    if showcode:
+        showit(it)
+    else:
+        interpretit(it, tal=(not macros), showtal=showtal,
+                    strictinsert=strictinsert,
+                    i18nInterpolate=i18nInterpolate)
 
 def interpretit(it, engine=None, stream=None, tal=1, showtal=-1,
-                strictinsert=1):
+                strictinsert=1, i18nInterpolate=1):
     from TALInterpreter import TALInterpreter
     program, macros = it
     assert TALDefs.isCurrentVersion(program)
     if engine is None:
         engine = DummyEngine.DummyEngine(macros)
     TALInterpreter(program, macros, engine, stream, wrap=0,
-                   tal=tal, showtal=showtal, strictinsert=strictinsert)()
+                   tal=tal, showtal=showtal, strictinsert=strictinsert,
+                   i18nInterpolate=i18nInterpolate)()
 
 def compilefile(file, mode=None):
     assert mode in ("html", "xml", None)