root/livinglogic.python.xist/src/ll/xist/scripts/dtd2xsc.py @ 2522:05f2f122ce1e

Revision 2522:05f2f122ce1e, 2.4 KB (checked in by Walter Doerwald <walter@…>, 13 years ago)

Turn scripts into modules and add setuptools entrypoints for them.

Add installation dependencies.

Line 
1#! /usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4## Copyright 1999-2005 by LivingLogic AG, Bayreuth/Germany.
5## Copyright 1999-2005 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, xmlname, xmlurl, shareattrs, asmod, 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, xmlname, xmlurl)
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(asmod=asmod, 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("-p", "--prefix", dest="xmlname", help="the XML prefix for this namespace", default="prefix", metavar="PREFIX")
59    p.add_option("-u", "--url", dest="xmlurl", help="the XML namespace name", metavar="URL")
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.xmlname, options.xmlurl, options.shareattrs, options.model, options.defaults)
74
75
76if __name__ == "__main__":
77    sys.exit(main())
Note: See TracBrowser for help on using the browser.