Changeset 2145


Ignore:
Timestamp:
Jan 12, 2010 12:03:47 AM (10 years ago)
Author:
wehart
Message:

A stab at refactoring the pyomo command-line script, to make
it easier to write related scripts.

Unfortunately, I started doing this with a Jython installation, which
has some issues running PyUtilib?. Thus, this is not well-tested.

Location:
coopr.pyomo/trunk
Files:
2 added
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/scripts/util.py

    r2093 r2145  
    198198
    199199
    200 def run_pyomo(options, args):
    201     #
    202     # Run the a command that executes a Pyomo script
    203     #
    204     if options.help_components:
     200#
     201# Print information about modeling components supported by Pyomo
     202#
     203def print_components(options):
     204    print ""
     205    print "----------------------------------------------------------------"
     206    print "Pyomo Model Components:"
     207    print "----------------------------------------------------------------"
     208    components = pyomo.model_components()
     209    index = pyutilib.misc.sort_index(components)
     210    for i in index:
    205211        print ""
    206         print "----------------------------------------------------------------"
    207         print "Pyomo Model Components:"
    208         print "----------------------------------------------------------------"
    209         components = pyomo.model_components()
    210         index = pyutilib.misc.sort_index(components)
    211         for i in index:
    212             print ""
    213             print " "+components[i][0]
    214             for line in textwrap.wrap(components[i][1], 59):
    215                 print "    "+line
     212        print " "+components[i][0]
     213        for line in textwrap.wrap(components[i][1], 59):
     214            print "    "+line
     215    print ""
     216    print "----------------------------------------------------------------"
     217    print "Pyomo Virtual Sets:"
     218    print "----------------------------------------------------------------"
     219    pyomo_sets = pyomo.predefined_sets()
     220    index = pyutilib.misc.sort_index(pyomo_sets)
     221    for i in index:
    216222        print ""
    217         print "----------------------------------------------------------------"
    218         print "Pyomo Virtual Sets:"
    219         print "----------------------------------------------------------------"
    220         pyomo_sets = pyomo.predefined_sets()
    221         index = pyutilib.misc.sort_index(pyomo_sets)
    222         for i in index:
    223             print ""
    224             print " "+pyomo_sets[i][0]
    225             print "    "+pyomo_sets[i][1]
    226         return
    227 
    228     #
    229     # Process command-line options
     223        print " "+pyomo_sets[i][0]
     224        print "    "+pyomo_sets[i][1]
     225
     226#
     227# Setup Pyomo execution environment
     228#
     229def setup_environment(options):
     230    #
     231    # Disable garbage collection
    230232    #
    231233    if options.disable_gc:
    232234        gc.disable()
     235    #
     236    # Setup verbose debugging mode
     237    #
    233238    if options.verbose:
    234239       pyomo.set_debugging("verbose")
     240    #
     241    # Setup debugging for specific Pyomo components
     242    #
    235243    if options.debug is not None:
    236244       for val in options.debug:
    237245         pyomo.set_debugging( val )
     246    #
     247    # Setup I/O redirect to a logfile
     248    #
    238249    if not options.logfile is None:
    239250        pyutilib.misc.setup_redirect(options.logfile)
     251    #
     252    # Setup management for temporary files
     253    #
    240254    if not options.tempdir is None:
    241255        if not os.path.exists(options.tempdir):
    242256            raise ValueError, "Directory for temporary files does not exist: "+options.tempdir
    243257        pyutilib.services.TempfileManager.tempdir = options.tempdir
    244 
     258    #
     259    # Configure exception management
     260    #
    245261    filter_excepthook=True
    246262    def pyomo_excepthook(etype,value,tb):
     
    265281           print "    "+item[3]
    266282      sys.exit(1)
    267    
    268283    sys.excepthook = pyomo_excepthook
    269284
    270     #
    271     # Apply preprocessing steps
    272     #
     285#
     286# Execute preprocessing files
     287#
     288def apply_preprocessing(options):
    273289    for file in options.preprocess:
    274290        preprocess = pyutilib.misc.import_file(file)
     
    282298       parser.print_help()
    283299       return
     300
     301#
     302# Create instance of Pyomo model
     303#
     304def create_problem(options, args):
     305    #
     306    # Verify that files exist
     307    #
    284308    for file in args:
    285309      if not os.path.exists(file):
    286          print "File "+file+" does not exist!"
    287          sys.exit(1)
    288 
     310         raise IOError, "File "+file+" does not exist!"
    289311    #
    290312    # Create Model
     
    314336          userdata = pyutilib.misc.import_file(args[1])
    315337          if "modeldata" not in dir(userdata):
    316              print ""
    317              print "pyomo: No 'modeldata' object created in module "+args[1]
    318              sys.exit(0)
     338             raise SystemExit, "No 'modeldata' object created in module "+args[1]
    319339          if userdata.modeldata is None:
    320              print ""
    321              raise SystemExit, "Exiting pyomo: 'modeldata' object equals 'None' in module "+args[1]
     340             raise SystemExit, "'modeldata' object equals 'None' in module "+args[1]
    322341          userdata.modeldata.read(model)
    323342          instance = model.create(userdata.modeldata)
  • coopr.pyomo/trunk/scripts/pyomo

    r2004 r2145  
    5454except SystemExit:
    5555    pass
    56 #except ValueError, str:
    57     #print str
    58 #except IOError, str:
    59     #print str
    60 #except pyutilib.common.ApplicationError, str:
    61     #print str
    62 #except RuntimeError:
    63     #pass
    64 #except:
    65    #print "Encountered unhandled exception"
    66    #traceback.print_exc()
     56except:
     57   print "Encountered unhandled exception"
     58   traceback.print_exc()
Note: See TracChangeset for help on using the changeset viewer.