root/livinglogic.python.xist/src/ll/xist/scripts/dtd2xsc.py @ 2903:232a091e48f9

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

Switch encoding of Python files to UTF-8.

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