root/livinglogic.python.xist/src/ll/xist/scripts/dtd2xsc.py @ 2789:e5c02fe0e72c

Revision 2789:e5c02fe0e72c, 2.2 KB (checked in by Walter Doerwald <walter@…>, 12 years ago)

Merge in rel-2-15-3.

Line 
1#! /usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4## Copyright 1999-2007 by LivingLogic AG, Bayreuth/Germany.
5## Copyright 1999-2007 by Walter Dörwald
6##
7## All Rights Reserved
8##
9## See xist/__init__.py for the license
10
11
12"""
13<par>Module that helps to create &xist; namespace modules from &dtd;s.
14Needs <app>xmlproc</app> from the <app>PyXML</app> package.
15For usage information type:</par>
16<prog>
17dtd2xsc --help
18</prog>
19"""
20
21
22__version__ = "$Revision$"[11:-2]
23# $Source$
24
25
26import sys, os.path, optparse
27
28from xml.parsers.xmlproc import dtdparser
29
30from ll import url
31from ll.xist import xsc, parsers, xnd
32
33
34def dtd2xsc(dtdurl, outurl, verbose, xmlns, shareattrs, model, defaults):
35    if verbose:
36        print "Parsing DTD %s ..." % dtdurl
37    d = dtdparser.load_dtd(dtdurl.url)
38
39    if verbose:
40        print "Converting ..."
41    data = xnd.fromdtd(d, xmlns)
42
43    if shareattrs=="dupes":
44        data.shareattrs(False)
45    elif shareattrs=="all":
46        data.shareattrs(True)
47
48    if verbose:
49        print "Writing to %s ..." % outurl
50    file = outurl.openwrite()
51    file.write(data.aspy(model=model, defaults=defaults))
52    file.close()
53
54
55def main():
56    p = optparse.OptionParser(usage="usage: %prog [options] inputurl.dtd")
57    p.add_option("-o", "--output", dest="output", metavar="FILE", help="write output to FILE")
58    p.add_option("-v", "--verbose", action="store_true", dest="verbose")
59    p.add_option("-x", "--xmlns", dest="xmlns", help="the namespace name for this module")
60    p.add_option("-a", "--shareattrs", dest="shareattrs", help="Should identical attributes be shared among elements?", choices=("none", "dupes", "all"), default="dupes")
61    p.add_option("-m", "--model", dest="model", default="once", help="Add sims information to the namespace", choices=("no", "all", "once"))
62    p.add_option("-d", "--defaults", action="store_true", dest="defaults", help="Output default values for attributes")
63
64    (options, args) = p.parse_args()
65    if len(args) != 1:
66        p.error("incorrect number of arguments")
67        return 1
68    input = url.URL(args[0])
69    if options.output is None:
70        output = url.File(input.withext("py").file)
71    else:
72        output = url.URL(options.output)
73    dtd2xsc(input, output, options.verbose, options.xmlns, options.shareattrs, options.model, options.defaults)
74
75
76if __name__ == "__main__":
77    sys.exit(main())
Note: See TracBrowser for help on using the browser.