Rémy,

Here is that fix I mentioned.

This fix basically says: Go ahead and keep on processing (the
following steps of the process) even if there are error or warning
messages.  We want it to do that because, in the case of wmdr.xsd,
there are warning messages that we want to ignore, and I can't, in
general, distinguish between warning and error messages, ignorable
messages, etc.

I was concerned that (1) the user might make changes to the XML
schema, (2) the schema might cause generateDS.py to fail to generate
the library/module, and (3) the django file generation process might
use a previously generated library/module without the user noticing
it.  With this fix, we clean up and delete those files (the module
itself and both Python 2 and Python 3 compiled/pyc files).  Then, if
generateDS.py does fail, that will cause a failure at a later stage,
and the user will forced to be aware of it.

A patched file is attached.  Let me know if this works for you.

I'll try to do some more testing, some clean up, and create a new
release in a few days.

Dave

On Mon, May 29, 2017 at 12:12:20PM +0200, Rémy Gibault wrote:
> Hi Dave,
> 
> Sorry to disturb you again,
> 
> I'm trying to generate Django models and forms now.
> I have followed instructions from chapter 15.1.1 of this web page :
> https://pythonhosted.org/generateDS/
> 
> 
> * ./gends_run_gen_django.py -f -v -p /var/opt/generateDS/generateDS.py*
> */home/wigos/projects/generateDS/wmdr/xsd/wmdr.xsd*
> 
> 
> The output is :
> 
> 
> schema_name_stem: wmdr
> bindings_file_name: wmdrlib.py
> *** running /var/opt/generateDS/generateDS.py -f -o wmdrlib.py
> --member-specs=list /home/wigos/projects/generateDS/wmdr/xsd/wmdr.xsd
> *** error ***
> *** warning.  Removing child with duplicate name: "Boolean"
> *** warning.  Removing child with duplicate name: "AbstractObject"
> *** warning.  Removing child with duplicate name: "Process"
> *** warning.  Removing child with duplicate name: "RangeBounds"
> *** error ***
> 
> 
> 
> a file wmdrlib.py is generated
> 
> -rw-r--r--. 1 root  root  5684019 May 29 09:14 wmdrlib.py
> 
> 
> *but I don't find any models.py or forms.py file.*
> 
> Any idea?
> 
> *Rémy *
> 
> 

[snip]


-- 

Dave Kuhlman
http://www.davekuhlman.org
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Synopsis:
    Generate Django models.py and forms.py from XML schema.
Usage:
    python gends_run_gen_django.py [options] <schema_file>
Options:
    -h, --help      Display this help message.
    -f, --force     Overwrite the following files without asking:
                        <schema>lib.py
                        generateds_definedsimpletypes.py
                        models.py
                        forms.py
    -p, --path-to-generateDS-script=/path/to/generateDS.py
                    Path to the generateDS.py script.
    -v, --verbose   Display additional information while running.
    -s, --script    Write out (display) the command lines used.  Can
                    be captured and used in a shell script, for example.
Examples:
    python gends_run_gen_django.py my_schema.xsd
    python gends_run_gen_django.py -f -p ../generateDS.py my_other_schema.xsd

"""


#
# Imports

import sys
import getopt
import os
from subprocess import Popen, PIPE
from glob import glob


#
# Globals and constants


#
# Functions for external use


#
# Classes

class GenerateDjangoError(Exception):
    pass


#
# Functions for internal use and testing

def generate(options, schema_file_name):
    schema_name_stem = os.path.splitext(os.path.split(schema_file_name)[1])[0]
    bindings_file_name = '%slib.py' % (schema_name_stem, )
    bindings_file_stem = os.path.splitext(bindings_file_name)[0]
    model_file_name = 'models.py'
    form_file_name = 'forms.py'
    admin_file_name = 'admin.py'
    dbg_msg(options, 'schema_name_stem: %s\n' % (schema_name_stem, ))
    dbg_msg(options, 'bindings_file_name: %s\n' % (bindings_file_name, ))
    if options['force']:
        file_names = (
            glob(bindings_file_name) +
            glob('%s.pyc' % bindings_file_stem) +
            glob('__pycache__/%s.*.pyc' % bindings_file_stem)
        )
        for file_name in file_names:
            dbg_msg(options, 'removing: %s\n' % file_name)
            os.remove(file_name)
    else:
        flag1 = exists(bindings_file_name)
        flag2 = exists(model_file_name)
        flag3 = exists(form_file_name)
        flag4 = exists(admin_file_name)
        if (flag1 or flag2 or flag3 or flag4):
            return
    args = (
        options['path'],
        '-f',
        '-o', '%s' % (bindings_file_name, ),
        '--member-specs=list',
        schema_file_name,
    )
    if not run_cmd(options, args):
        return
    args = (
        './gends_extract_simple_types.py', '-f',
        schema_file_name,
    )
    if not run_cmd(options, args):
        return
    args = (
        './gends_generate_django.py', '-f',
        bindings_file_stem,
    )
    if not run_cmd(options, args):
        return


def run_cmd(options, args):
    msg = '%s\n' % (' '.join(args), )
    dbg_msg(options, '*** running %s' % (msg, ))
    if options['script']:
        write_msg(options, msg)
    process = Popen(args, stderr=PIPE, stdout=PIPE)
    content1 = process.stderr.read()
    content2 = process.stdout.read()
    if content1:
        sys.stderr.write('*** error ***\n')
        sys.stderr.write(content1.decode('utf-8'))
        sys.stderr.write('*** error ***\n')
        #return False
    if content2:
        dbg_msg(options, '*** message ***\n')
        dbg_msg(options, content2.decode('utf-8'))
        dbg_msg(options, '*** message ***\n')
        #return True
    return True


def exists(file_name):
    if os.path.exists(file_name):
        msg = 'File %s exists.  Use -f/--force to overwrite.\n' % (file_name, )
        sys.stderr.write(msg)
        return True
    return False


def dbg_msg(options, msg):
    if options['verbose']:
        if isinstance(msg, str):
            sys.stdout.write(msg)
        else:
            sys.stdout.write(msg.decode('utf-8'))


def write_msg(options, msg):
    if isinstance(msg, str):
        sys.stdout.write(msg)
    else:
        sys.stdout.write(msg.decode('utf-8'))


def usage():
    sys.exit(__doc__)


def main():
    args = sys.argv[1:]
    try:
        opts, args = getopt.getopt(args, 'hvfp:s', [
            'help', 'verbose', 'script',
            'force', 'path-to-generateDS-script=',
        ])
    except:
        usage()
    options = {}
    options['force'] = False
    options['verbose'] = False
    options['script'] = False
    options['path'] = './generateDS.py'
    for opt, val in opts:
        if opt in ('-h', '--help'):
            usage()
        elif opt in ('-f', '--force'):
            options['force'] = True
        elif opt in ('-v', '--verbose'):
            options['verbose'] = True
        elif opt in ('-s', '--script'):
            options['script'] = True
        elif opt in ('-p', '--path-to-generateDS-script'):
            options['path'] = val
    if not os.path.exists(options['path']):
        sys.exit(
            '\n*** error: Cannot find generateDS.py.  '
            'Use "-p path" command line option.\n')
    if len(args) != 1:
        usage()
    schema_name = args[0]
    generate(options, schema_name)


if __name__ == '__main__':
    #import pdb; pdb.set_trace()
    main()
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to