playpyana.py :  » Language-Interface » Pyana » Pyana-0.9.2 » Scripts » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Pyana 
Pyana » Pyana 0.9.2 » Scripts » playpyana.py
#!/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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.