mdl_validate_model.py :  » Database » Modeling-Framework » Modeling-0.9 » Modeling » scripts » 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 » Database » Modeling Framework 
Modeling Framework » Modeling 0.9 » Modeling » scripts » mdl_validate_model.py
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-

#-----------------------------------------------------------------------------
# Modeling Framework: an Object-Relational Bridge for python
#
# Copyright (c) 2001-2004 Sbastien Bigaret <sbigaret@users.sourceforge.net>
# All rights reserved.
#
# This file is part of the Modeling Framework.
#
# This code is distributed under a "3-clause BSD"-style license;
# see the LICENSE file for details.
#-----------------------------------------------------------------------------

"""
  TBD docstring
"""
__version__='$Revision: 932 $'[11:-2]

from Modeling import ModelValidation
from Modeling.ModelValidation import NOT_SUPPORTED,ERROR,WARNING,INFO,DEBUG


import getopt, sys

def log(msg):
  if not quiet_mode:
    sys.stderr.write(msg+'\n')

def tracebackInfoFromStack(exc_traceback):
  from traceback import extract_tb,format_list
  from Modeling.utils import isListOrTuple
  tb=None
  str='%s\n%s\n'%sys.exc_info()[:2]
  try:
    tb=sys.exc_info()[-1]
    str+=reduce(lambda a,b: a+b, format_list(extract_tb(tb)))
    return str
  finally:
    del tb

def usage(prgName):
  _usage="""
  %s [options] model.(xml|py)
Validate a model. Errors and warnings messages are written on sys.stderr.
The model file can be either a xml model file or a PyModel.

Return status:

  0  if the model is valid,
  1  if the model has errors (no matter whether there are warnings)
  2  if some warnings were issued (except with --ignore-warnings)
  3  in case the model cannot be loaded

Options:
--------
  Default is to print informational, warning and error messages
  
  -q --quiet            ignore info, only prints warnings and errors
  -w --ignore-warnings  only print errors (see also: Return status above)
  -Q                    really quiet: do not print anything
  -h --help             gives this help
  -v --verbose          prints extra info. on the runtime steps
  """ % prgName
  sys.stderr.write(_usage)

def validate_model(model, ignore_levels=None):
  """
  """
  errors=MV.ModelValidationException(ignore_levels=ignore_levels)
  MV.validateModel(model, errors)
  return errors

# Global variable
quiet_mode=0

def main(args):
  me=args[0]
  try: options, args = getopt.getopt(sys.argv[1:],
                                     'hQqvw',
                                     ["help", "quiet", "verbose",
                                      "ignore-warnings",])
  except: usage(me); return 1
  global quiet_mode
  ignore_warnings=verbose=0
  ignore_levels=[DEBUG]
  for k, v in options:
    if k in ('-h', '--help'): usage(me); return 0
    if k in ('-Q', ): quiet_mode=1; verbose=0; continue
    if k in ('-q', '--quiet'):
      ignore_levels.append(INFO)
      continue
    if k in ('-v', '--verbose'): verbose=1; continue
    if k in ('-w', '--ignore_warnings'):
      ignore_levels.append(INFO)
      ignore_levels.append(WARNING)
      ignore_warnings=1
      continue

  if len(args)!=1: usage(me) ; return 1

  model_file=args[0]
  # load the model
  if verbose: log("Loading the model...")
  try:
    from Modeling import Model
    model=Model.loadModel(model_file)
    if not model:
      raise RuntimeError, "Abnormal: got no exception but Model is None"
  except Exception, exc:
    log("Serious: couldn't load the model")
    log(tracebackInfoFromStack(exc))
    return 3
  if verbose: log("Done.")
  if verbose: log("Validating...")
  errors=validate_model(model, ignore_levels)
  if verbose: log("Done.")

  log(str(errors))
  
  err_levels=errors.levels_of_errors()
  if not errors.levels_of_errors() or err_levels==[INFO]:
    return 0
  try: err_levels.remove(INFO)
  except ValueError: pass
  if err_levels==[WARNING]:
    if ignore_warnings: return 0
    else: return 2
  return 1

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