root/livinglogic.python.xist/src/ll/xist/scripts/dtd2xsc.py @ 2644:a9c9fff25126

Revision 2644:a9c9fff25126, 2.2 KB (checked in by Walter Doerwald <walter@…>, 13 years ago)

Update ll.xist.xnd and ll.xist.ns.tld to work with the new namespace
handling. Fix tests.

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