1
2 """
3 Internal class to convert DTDs to python form.
4
5 dtd2py [-m mixinmodule] [-u] <DTDfile>...
6
7 Converts the given DTD to a python form, and stores it in the dtds package
8 directory. That directory should be writable by the user running this.
9 The new dtds module will have the same base name as the DTD, but with dashes and dots converted to underscores.
10
11 Options:
12 -m <modulename> -- specify a mixin module to load. If a class in this
13 module matches the DTD element name plus "Mixin" appended, then that class
14 will be inherited from in the generated element class.
15 -u -- convert all element tag names to uppercase. Otherwise, case is
16 preserved. Some XML applications specify using uppercase, others case
17 preserving. Some XML element names may match python keyword or builtin
18 names, and those DTDs should also be converted to uppercase to avoid syntax
19 errors in the generated file.
20
21 """
22
23 import sys, os
24 import getopt
25
26 from Bio.EUtils import POM
27
29 outfilename = POM.get_mod_file(dtdfile)
30
31 if 1:
32 outfile = open(outfilename, "w")
33 try:
34 dtdp = POM.get_dtd_compiler(outfile, mixin, toupper)
35 dtdp.parse_resource(dtdfile)
36 finally:
37 outfile.close()
38
39
40
41
42
43
44
45
46 -def do_it(filelist, mixin, toupper):
53
54
56 if len(argv) > 1:
57 mixin = None
58 toupper = 0
59 args, files = getopt.getopt(argv[1:], "m:u")
60 for opt, value in args:
61 if opt == "-m":
62 try:
63 mixin = __import__(value)
64 except:
65 print "Warning: could not import mixin module"
66 mixin = None
67 if opt == "-u":
68 toupper = 1
69 do_it(files, mixin, toupper)
70 else:
71 print __doc__
72
73
74 if __name__ == "__main__":
75 main(sys.argv)
76