codes_config_get_vals.py.in 1.59 KB
Newer Older
1
#!/usr/bin/python2.7
Jonathan Jenkins's avatar
Jonathan Jenkins committed
2 3 4 5 6 7 8

#
# Copyright (C) 2014 University of Chicago.
# See COPYRIGHT notice in top-level directory.
#
#

9 10
import argparse
import sys
11 12
import os
import imp
13 14
from itertools import islice

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# internal testing: import directly
#import configurator as conf

# internal testing: import directly
#import configurator as conf

# dynamically load from lib path
def import_from(fname):
    path,name = os.path.split(fname)
    name,ext  = os.path.splitext(name) 

    fmod, fname, data = imp.find_module(name, [path])
    return imp.load_module(name,fmod,fname,data)

conf = import_from("@libdir@/configurator.py")
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

def main():
    args = parse_args()

    mod = conf.import_from(args.substitute_py)

    conf.check_cfields(mod)
    
    for pair in mod.cfields:
        if pair[0] == args.token:
            sys.stdout.write(str(pair[1][0]))
            for x in islice(pair[1],1,None):
                sys.stdout.write(" " + str(x))
            sys.stdout.write('\n')
            sys.exit(0) # success
    else: # failure, did not find
        raise ValueError('could not find token "' + args.token + '"')
        

def parse_args():
    parser = argparse.ArgumentParser(\
            description="echo the values of a token file to stdout")
    parser.add_argument("substitute_py",
            help='python file defining "cfields" variable consisting of '
                 'elements of the form '
                 '( replacement_token, [replacements...])')
    parser.add_argument("token",
            help="replacement token to print values out for")
    return parser.parse_args()

if __name__ == "__main__":
    main()