#!/usr/bin/env python
"""The script provides a command-line interface to some commonly used
Pyana features.
See the usage string for details on the available options.
"""
import sys
import Pyana
usage = \
"""
Options:
--xml=<source-uri> the xml input file (defaults to stdin)
--xsl=<stylesheet-uri> the xsl stylesheet file (if not set,
XML file must contain a processing
instruction)
--out=<output file> the output file (defaults to stdout)
--validate=<yes|no> validate the xml input using an internal
DTD or schema (defaults to no)
--external-schema=<ns> <uri> validate the xml input using an external
schema (this switch can be used more than
once) e.g.
--external-schema=
"http://www.w3.org/1999/xhtml xhtml1.1.xsd"
<param1>=<value1> ... toplevel xsl parameters specified as
Xpaths e.g.
name='Joe' year=1974
usage: playpyana.py --help
or: playpyana.py [options]
"""
try_info = 'Try "playpyana.py --help" for more information.'
def parse_options(args):
switches = {'external-schema' : []}
other_args = {}
for arg in args:
if arg.startswith('--'):
if arg.find('=') == -1:
print >> sys.stderr, \
'Option "%s" must have a value.' % arg
print >> sys.stderr, try_info
sys.exit(1)
else:
arg = arg[2:]
key, value = arg.split('=', 1)
if key not in ['xml', 'xsl', 'out', 'validate', 'external-schema']:
print >> sys.stderr, \
'Option "--%s" is not recognized.' % key
print >> sys.stderr, try_info
sys.exit(1)
if key == 'external-schema':
switches[key].append(value)
elif switches.has_key(key):
print >> sys.stderr, \
'Option "--%s" appears more than once.' % key
print >> sys.stderr, try_info
sys.exit(1)
else:
switches[key] = value
else:
if arg.find('=') == -1:
print >> sys.stderr, \
'Top level XSLT argument "%s" must have a value.' % arg
print >> sys.stderr, try_info
sys.exit(1)
else:
key, value = arg.split('=', 1)
other_args[key] = value
return switches, other_args
if len(sys.argv) < 2 or '--help' in sys.argv or '-h' in sys.argv:
print usage
sys.exit(0)
pyana_args = {}
switches, xsl_args = parse_options(sys.argv[1:])
xml = switches.get('xml', None)
xsl = switches.get('xsl', None)
out = switches.get('out', None)
validate = switches.get('validate', 'no')
t = Pyana.Transformer()
t.setStylesheetParams(xsl_args)
if validate.lower() == 'yes' or switches['external-schema']:
t.useValidation = 1
elif validate.lower() == 'no':
t.useValidation = 0
else:
print >> sys.stderr, 'Validate option must have a value of "yes" or "no".'
sys.exit(1)
if xml is None:
source = sys.stdin
else:
source = Pyana.URI(xml)
if switches['external-schema']:
print 'preschema: %r' % switches['external-schema']
schema = ' '.join(switches['external-schema'])
print 'schema: %r' % schema
source = t.parseSource(source, externalSchema = schema)
else:
source = t.parseSource(source)
pyana_args['source'] = source
if xsl is None:
pyana_args['style'] = None
else:
pyana_args['style'] = Pyana.URI(xsl)
if out is None:
func = t.transform2Writer
pyana_args['writer'] = sys.stdout
else:
func = t.transform2File
pyana_args['file'] = out
apply(func, (), pyana_args)
|