import os, re, sys
from ..configuration import config

_region_re = re.compile('\.(xls|xlsx)')

def _get_load_regions():
    path = os.path.join(config.rule_dir, 'offense_crosswalks', 'wvfbi')
    return [_region_re.sub('', file) for file in os.listdir(path)]

def load_rules(cmd):
    args = cmd.args
    if len(args.region) == 0 and not args.wvfbi and not args.all_regions:
        print "Please specify one or more regions to load (example) -r mn -r az -r fl). Use -R load all regions."
        sys.exit(1)
    
    regions = _get_load_regions() if args.all_regions else args.region
    
    opt = {'wvfbi': args.wvfbi,
           'no_replace':args.no_replace,
           'verbose': args.verbose,
           'sentence': args.sentence,
           'offense': args.offense
           }
    if regions:
        for region in regions:
            _opt = opt.copy()
            _opt['region'] = region
            cmd.task_queue.put(_opt)
    else:
        opt['region'] = None
        cmd.task_queue.put(opt)
        
    cmd.start()
    cmd.task_queue.join()    
    cmd.flush()
        
        