Generate.py :  » Web-Frameworks » Webware » Webware-1.0.2 » MiddleKit » Design » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » MiddleKit » Design » Generate.py
#!/usr/bin/env python

"""Generate.py

> python Generate.py -h

"""


import os, sys
from getopt import getopt
import FixPath
import MiddleKit
from MiscUtils import StringTypes

if sys.platform == 'win32':
  # without this, I can't see output from uncaught exceptions!
  # perhaps this is caused by the recent incorporation of win32all (via DataTable)?
  sys.stderr = sys.stdout


class Generate:

  def databases(self):
    return ['MSSQL', 'MySQL', 'PostgreSQL']  # @@ 2000-10-19 ce: should build this dynamically

  def main(self, args=sys.argv):
    opt = self.options(args)

    # Make or check the output directory
    outdir = opt['outdir']
    if not os.path.exists(outdir):
      os.mkdir(outdir)
    elif not os.path.isdir(outdir):
      print 'Error: Output target, %s, is not a directory.' % outdir

    # Generate
    if opt.has_key('sql'):
      print 'Generating SQL...'
      self.generate(
        pyClass = opt['db'] + 'SQLGenerator',
        model = opt['model'],
        configFilename = opt.get('config'),
        outdir = os.path.join(outdir, 'GeneratedSQL'))
    if opt.has_key('py'):
      print 'Generating Python...'
      self.generate(
        pyClass = opt['db'] + 'PythonGenerator',
        model = opt['model'],
        configFilename = opt.get('config'),
        outdir=outdir)
    model = MiddleKit.Core.Model.Model(opt['model'],
      configFilename=opt.get('config'), havePythonClasses=0)
    model.printWarnings()

  def usage(self, errorMsg=None):
    progName = os.path.basename(sys.argv[0])
    if errorMsg:
      print '%s: error: %s' % (progName, errorMsg)
    print 'Usage: %s --db DBNAME --model FILENAME [--sql] [--py] [--config FILENAME] [--outdir DIRNAME]' % progName
    print '       %s -h | --help' % progName
    print
    print '       * Known databases include: %s' % ', '.join(self.databases())
    print '       * If neither --sql nor --py are specified, both are generated.'
    print '       * If --outdir is not specified, then the base filename (sans extension) is used.'
    print '       * --config lets you specify a different config filename inside the model.'
    print '         This is mostly useful for the regression test suite.'
    print
    sys.exit(1)

  def options(self, args):
    # Command line dissection
    if type(args) == type(''):
      args = args.split()
    optPairs, files = getopt(args[1:], 'h',
      ['help', 'db=', 'model=', 'sql', 'py', 'config=', 'outdir='])
    if len(optPairs) < 1:
      self.usage('Missing options.')
    if len(files) > 0:
      self.usage('Extra files or options passed.')

    # Turn the cmd line optPairs into a dictionary
    opt = {}
    for key, value in optPairs:
      if len(key) >= 2 and key[:2] == '--':
        key = key[2:]
      elif key[0] == '-':
        key = key[1:]
      opt[key] = value

    # Check for required opt, set defaults, etc.
    if opt.has_key('h') or opt.has_key('help'):
      self.usage()
    if not opt.has_key('db'):
      self.usage('No database specified.')
    if not opt.has_key('model'):
      self.usage('No model specified.')
    if not opt.has_key('sql') and not opt.has_key('py'):
      opt['sql'] = ''
      opt['py'] = ''
    if not opt.has_key('outdir'):
      opt['outdir'] = os.curdir

    return opt

  def generate(self, pyClass, model, configFilename, outdir):
    """Generate code using the given class, model and output directory.

    The pyClass may be a string, in which case a module of the same name is
    imported and the class extracted from that. The model may be a string,
    in which case it is considered a filename of a model.

    """
    if type(pyClass) in StringTypes:
      module = __import__(pyClass, globals())
      pyClass = getattr(module, pyClass)
    generator = pyClass()
    if type(model) in StringTypes:
      generator.readModelFileNamed(model, configFilename=configFilename,
        havePythonClasses=0)
    else:
      generator.setModel(model)
    generator.generate(outdir)


if __name__ == '__main__':
  Generate().main(sys.argv)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.