Changeset 3265
- Timestamp:
- 01/26/12 21:06:49 (4 months ago)
- Files:
-
- 1 modified
-
branches/mcxtrace-1.0/src/mcrun2/main.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/mcxtrace-1.0/src/mcrun2/main.py
r3264 r3265 1 1 #!/usr/bin/env python2.6 2 2 3 from os import path 3 import os.path 4 import logging 5 6 from os.path import isfile, isdir, abspath, dirname, basename 4 7 from optparse import OptionParser, OptionGroup, OptionValueError 5 8 9 from mcstas import McStas 10 11 LOG = logging.getLogger('mcstas') 12 6 13 7 14 # Helper functions 8 def build_checker(accept, update,msg='Invalid value'):9 ''' Build checker from accept() and update() functions'''15 def build_checker(accept, msg='Invalid value'): 16 ''' Build checker from accept() function ''' 10 17 def checker(option, _opt_str, value, parser): 11 18 ''' value must be acceptable ''' … … 13 20 raise OptionValueError('option %s: %s (was: "%s")' % (option, msg, value)) 14 21 # Update parser with accepted value 15 update(parser.values, value)22 setattr(parser.values, option.dest, value) 16 23 return checker 17 24 … … 77 84 help='disable optimising compiler flags for faster compilation') 78 85 86 add('--verbose', 87 action='store_true', default=False, 88 help='enable verbose output') 89 79 90 parser.add_option_group(opt) 80 91 … … 89 100 # Misc options 90 101 check_seed = build_checker(lambda seed: seed != 0, 91 lambda vs, seed: setattr(vs, 'seed', seed),92 102 'SEED cannot be 0') 93 103 … … 109 119 110 120 # 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') 120 134 121 135 add('-d', '--dir', 122 136 metavar='DIR', type=str, 123 action='callback', callback=check_dir ('dir'),137 action='callback', callback=check_dir, 124 138 help='put all data files in directory DIR') 125 139 126 140 add('-f', '--file', 127 141 metavar='FILE', type=str, 128 action='callback', callback=check_file( 'file'),142 action='callback', callback=check_file(exist=False), 129 143 help='put all data in a single file') 130 144 … … 153 167 ''' Main routine ''' 154 168 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 155 180 usage = ('usage: %prog [-cpnN] Instr [-sndftgahi] ' 156 181 'params={val|min,max|min,guess,max}...') 157 182 parser = OptionParser(usage, version='mcrun 0.1') 158 183 159 # Add options160 184 add_mcrun_options(parser) 161 185 add_mcstas_options(parser) … … 165 189 parser.destroy() 166 190 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) 169 204 170 205
