Dave:
Dave Kuhlman wrote:
Hi all,
I have two patches to submit. The first corrects a flaw in the processing of
default values for attributes, which causes a default value to be assigned when
the schema doesn't define one. The other is an attempt to implement support for
import of other schemas, including via http and ftp.
Arne -
The fix for default values sounds valuable.
The extension for importing schemas via http and ftp sounds interesting.
You are aware that generateDS.py already does some include processing,
right?
Yes, I am. The changes to support import were made to process_include.py since
the handling code is already there. It was just a matter of handling the lookup
for http, ftp and file system access. From the testing I've done so far, it
works pretty well except the namespaces are completely screwed up on output.
Every project has different conventions regarding patch submission, so I thought
I'd check if they should be submitted separately or as one patch. Which do you
prefer?
Either together or separate patch files is fine. Do what is easiest for you.
Ok, I've attached 3 patch files, one for each part of the change. The issues
I fixed are:
1) default values are set to 'None' if the attribute has no default value
assigned;
2) if a root element is defined, that name is used for the element name in
the output parts of parseXXX methods;
3) search path definition for external schema files, and
4) support for the import directive.
The process_includes.py module wasn't being installed in 1.15c so I fixed
the setup.py script to do so.
The ability to use `patch` to apply your changes and then test them
helps me quite a bit.
- Dave
Anything to keep the developer happy. :-)
Hope this helps,
Arne
--- generateDS-1.15c/generateDS.py
+++ generateDS-1.15d/generateDS.py
@@ -17,6 +17,7 @@
Default = 'xs:'.
-b <behaviorfilename> Input file name for behaviors added to subclasses
-m Generate properties for member variables
+ --search-path="a:b:c:d" Search these directories for additional schema
files.
--subclass-suffix="XXX" Append XXX to the generated subclass names.
Default="Sub".
--root-element="XXX" Assume XXX is root element of instance docs.
@@ -108,6 +109,7 @@
ElementDict = {}
SaxElementDict = {}
Force = 0
+Dirpath = None
NamespacesDict = {}
Targetnamespace = ""
@@ -2377,38 +2379,38 @@
atype == DateTimeType or \
atype == DateType:
if default is None:
- add(", %s=''" % mappedName)
+ add(", %s=None" % mappedName)
else:
default1 = escape_string(default)
add(", %s='%s'" % (mappedName, default1))
elif atype in IntegerType:
if default is None:
- add(', %s=-1' % mappedName)
+ add(', %s=None' % mappedName)
else:
add(', %s=%s' % (mappedName, default))
elif atype == PositiveIntegerType:
if default is None:
- add(', %s=1' % mappedName)
+ add(', %s=None' % mappedName)
else:
add(', %s=%s' % (mappedName, default))
elif atype == NonPositiveIntegerType:
if default is None:
- add(', %s=0' % mappedName)
+ add(', %s=None' % mappedName)
else:
add(', %s=%s' % (mappedName, default))
elif atype == NegativeIntegerType:
if default is None:
- add(', %s=-1' % mappedName)
+ add(', %s=None' % mappedName)
else:
add(', %s=%s' % (mappedName, default))
elif atype == NonNegativeIntegerType:
if default is None:
- add(', %s=0' % mappedName)
+ add(', %s=None' % mappedName)
else:
add(', %s=%s' % (mappedName, default))
elif atype == BooleanType:
if default is None:
- add(', %s=False' % mappedName)
+ add(', %s=None' % mappedName)
else:
if default in ('false', '0'):
add(', %s=%s' % (mappedName, "False"))
@@ -2416,7 +2418,7 @@
add(', %s=%s' % (mappedName, "True"))
elif atype == FloatType or atype == DoubleType or atype == DecimalType:
if default is None:
- add(', %s=0.0' % mappedName)
+ add(', %s=None' % mappedName)
else:
add(', %s=%s' % (mappedName, default))
else:
@@ -3483,7 +3485,7 @@
def generateMain(outfile, prefix, root):
- name = root.getChildren()[0].getName()
+ name = RootElement or root.getChildren()[0].getName()
elType = cleanupName(root.getChildren()[0].getType())
if RootElement:
rootElement = RootElement
@@ -4098,7 +4100,7 @@
xschemaFileName, behaviorFilename,
processIncludes, superModule='???'):
global DelayedElements, DelayedElements_subclass, AlreadyGenerated,
SaxDelayedElements, \
- AlreadyGenerated_subclass, UserMethodsPath, UserMethodsModule
+ AlreadyGenerated_subclass, UserMethodsPath, UserMethodsModule, Dirpath
DelayedElements = []
DelayedElements_subclass = []
AlreadyGenerated = []
@@ -4118,6 +4120,7 @@
infile = open(xschemaFileName, 'r')
if processIncludes:
import process_includes
+ process_includes.DIRPATH = Dirpath
outfile = StringIO.StringIO()
process_includes.process_include_files(infile, outfile)
outfile.seek(0)
@@ -4163,12 +4166,12 @@
global Force, GenerateProperties, SubclassSuffix, RootElement, \
ValidatorBodiesBasePath, UseOldGetterSetter, \
UserMethodsPath, XsdNameSpace, \
- TEMPLATE_MAIN, TEMPLATE_SUBCLASS_FOOTER
+ TEMPLATE_MAIN, TEMPLATE_SUBCLASS_FOOTER, Dirpath
outputText = True
args = sys.argv[1:]
try:
options, args = getopt.getopt(args, 'hfyo:s:p:a:b:mu:',
- ['help', 'subclass-suffix=', 'root-element=', 'super=',
+ ['help', 'search-path=', 'subclass-suffix=', 'root-element=',
'super=',
'validator-bodies=', 'use-old-getter-setter',
'user-methods=', 'no-process-includes', 'silence',
])
@@ -4198,6 +4201,8 @@
behaviorFilename = option[1]
elif option[0] == '-m':
GenerateProperties = 1
+ elif option[0] == '--search-path':
+ Dirpath = option[1].split(':')
elif option[0] == '--subclass-suffix':
SubclassSuffix = option[1]
elif option[0] == '--root-element':
--- generateDS-1.15c/process_includes.py
+++ generateDS-1.15d/process_includes.py
@@ -11,6 +11,8 @@
Options:
-h, --help Display this help message.
-f, --force Force. If outfile exists, overwrite without asking.
+ -s, --search Search path for schemas. Colon separated list of
directorys where schemas may be found.
+
Examples:
python process_includes.py infile.xsd
python process_includes.py infile.xsd outfile.xsd
@@ -25,6 +27,7 @@
import os
import getopt
import re
+import urllib
#
# Try to import lxml first, and if that fails try ElementTree.
@@ -49,6 +52,7 @@
FORCE = False
NAMESPACE_PAT = re.compile(r'\{.*\}')
+DIRPATH = None
#
@@ -83,7 +87,20 @@
doc.write(outfile)
+def process_path(root, idx, path):
+ count = idx
+ doc = etree.parse(path)
+ node = doc.getroot()
+ process_include_tree(node)
+ children1 = node.getchildren()
+ for child1 in children1:
+ root.insert(count, child1)
+ count += 1
+ return count
+
def process_include_tree(root):
+ global DIRPATH
+
idx = 0
children = root.getchildren()
while idx < len(children):
@@ -97,16 +114,38 @@
root.remove(child)
path = child.attrib['schemaLocation']
if os.path.exists(path):
- doc = etree.parse(path)
- node = doc.getroot()
- process_include_tree(node)
- children1 = node.getchildren()
- for child1 in children1:
- root.insert(idx, child1)
- idx += 1
+ idx = process_path(root, idx, path)
else:
- msg = "Can't find include file %s. Aborting." % (path, )
- raise IOError(msg)
+ for d in DIRPATH:
+ path = os.path.join(d,locn)
+ if os.path.exists(path):
+ idx = process_path(root, idx, path)
+ break
+ else:
+ msg = "Can't find include file %s. Aborting." % (path, )
+ raise IOError(msg)
+ elif tag == 'import' and 'schemaLocation' in child.attrib:
+ root.remove(child)
+ locn = child.attrib['schemaLocation']
+ if locn.startswith('ftp:') or locn.startswith('http:'):
+ try:
+ path, msg = urllib.urlretrieve(locn)
+ idx = process_path(root, idx, path)
+ except:
+ msg = "Can't retrieve import file %s. Aborting." % (locn,
)
+ raise IOError(msg)
+ else:
+ if os.path.exists(locn):
+ idx = process_path(root, idx, locn)
+ else:
+ for d in DIRPATH:
+ path = os.path.join(d,locn)
+ if os.path.exists(path):
+ idx = process_path(root, idx, path)
+ break
+ else:
+ msg = "Can't find import file %s. Aborting." % (locn,
)
+ raise IOError(msg)
else:
process_include_tree(child)
idx += 1
@@ -134,9 +173,10 @@
def main():
global FORCE
+ global DIRPATH
args = sys.argv[1:]
try:
- opts, args = getopt.getopt(args, 'hf', ['help', 'force', ])
+ opts, args = getopt.getopt(args, 'hfs:', ['help', 'force', 'search=',])
except:
usage()
name = 'nobody'
@@ -145,6 +185,8 @@
usage()
elif opt in ('-f', '--force'):
FORCE = True
+ elif opt in ('-s', '--search'):
+ DIRPATH = val.split(':')
if len(args) == 2:
inpath = args[0]
outpath = args[1]
--- generateDS-1.15c/setup.py
+++ generateDS-1.15d/setup.py
@@ -7,7 +7,7 @@
author="Dave Kuhlman",
author_email="dkuhl...@rexx.com",
url="http://www.rexx.com/~dkuhlman",
- py_modules=["generateDS"],
+ py_modules=["generateDS", "process_includes"],
#scripts=["generateDS.py", "process_includes.py"],
)
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users