Changeset 3265

Show
Ignore:
Timestamp:
01/26/12 21:06:49 (4 months ago)
Author:
erkn
Message:

bump

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/mcxtrace-1.0/src/mcrun2/main.py

    r3264 r3265  
    11#!/usr/bin/env python2.6 
    22 
    3 from os import path 
     3import os.path 
     4import logging 
     5 
     6from os.path import isfile, isdir, abspath, dirname, basename 
    47from optparse import OptionParser, OptionGroup, OptionValueError 
    58 
     9from mcstas import McStas 
     10 
     11LOG = logging.getLogger('mcstas') 
     12 
    613 
    714# Helper functions 
    8 def build_checker(accept, update, msg='Invalid value'): 
    9     ''' Build checker from accept() and update() functions ''' 
     15def build_checker(accept, msg='Invalid value'): 
     16    ''' Build checker from accept() function ''' 
    1017    def checker(option, _opt_str, value, parser): 
    1118        ''' value must be acceptable ''' 
     
    1320            raise OptionValueError('option %s: %s (was: "%s")' % (option, msg, value)) 
    1421        # Update parser with accepted value 
    15         update(parser.values, value) 
     22        setattr(parser.values, option.dest, value) 
    1623    return checker 
    1724 
     
    7784        help='disable optimising compiler flags for faster compilation') 
    7885 
     86    add('--verbose', 
     87        action='store_true', default=False, 
     88        help='enable verbose output') 
     89 
    7990    parser.add_option_group(opt) 
    8091 
     
    89100    # Misc options 
    90101    check_seed = build_checker(lambda seed: seed != 0, 
    91                                lambda vs, seed: setattr(vs, 'seed', seed), 
    92102                               'SEED cannot be 0') 
    93103 
     
    109119 
    110120    # Data options 
    111     def check_path(opt_name, is_valid): 
    112         ''' Build function for checking existence of a path 
    113             provided for option opt_name ''' 
    114         return build_checker(is_valid, 
    115                              lambda vs, path: setattr(vs, opt_name, path), 
    116                              'invalid path') 
    117  
    118     check_dir = lambda opt: check_path(opt, path.isdir) 
    119     check_file = lambda opt: check_path(opt, path.isfile) 
     121    dir_exists = lambda path: isdir(abspath(path)) 
     122    check_dir = build_checker(dir_exists, 'invalid path') 
     123    def check_file(exist=True): 
     124        ''' Validate the path to a file ''' 
     125        if exist: 
     126            is_valid = isfile 
     127        else: 
     128            def is_valid(path): 
     129                ''' Ensure that path to file exists and filename is provided ''' 
     130                if not dir_exists(dirname(path)): 
     131                    return False 
     132                return not isdir(abspath(path)) 
     133        return build_checker(is_valid, 'invalid path') 
    120134 
    121135    add('-d', '--dir', 
    122136        metavar='DIR', type=str, 
    123         action='callback', callback=check_dir('dir'), 
     137        action='callback', callback=check_dir, 
    124138        help='put all data files in directory DIR') 
    125139 
    126140    add('-f', '--file', 
    127141        metavar='FILE', type=str, 
    128         action='callback', callback=check_file('file'), 
     142        action='callback', callback=check_file(exist=False), 
    129143        help='put all data in a single file') 
    130144 
     
    153167    ''' Main routine ''' 
    154168 
     169    # Setup logging 
     170    formatter = logging.Formatter('%(asctime)s - %(message)s') 
     171 
     172    ch = logging.StreamHandler() 
     173    ch.setLevel(logging.INFO) 
     174    ch.setFormatter(formatter) 
     175 
     176    LOG.setLevel(logging.DEBUG) 
     177    LOG.addHandler(ch) 
     178 
     179    # Add options 
    155180    usage = ('usage: %prog [-cpnN] Instr [-sndftgahi] ' 
    156181             'params={val|min,max|min,guess,max}...') 
    157182    parser = OptionParser(usage, version='mcrun 0.1') 
    158183 
    159     # Add options 
    160184    add_mcrun_options(parser) 
    161185    add_mcstas_options(parser) 
     
    165189    parser.destroy() 
    166190 
    167     print options 
    168     print args 
     191    if options.verbose: 
     192        ch.setLevel(logging.DEBUG) 
     193 
     194    # Extract instrument and parameters 
     195    if len(args) == 0: 
     196        raise OptionValueError('No instrument file specified.') 
     197    instr = args[0] 
     198    params = args[1:] 
     199 
     200    # Run McStas 
     201    mcstas = McStas(instr) 
     202    mcstas.prepare(options) 
     203    mcstas.run(options) 
    169204 
    170205