GitLab maintenance scheduled for Today, 2020-08-11, from 17:00 to 18:00 CT - Services will be unavailable during this time.

codes_config_get_vals.py.in 1.49 KB
Newer Older
1 2 3
#!/usr/bin/python2.7
import argparse
import sys
4 5
import os
import imp
6 7
from itertools import islice

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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")
23 24 25 26 27 28 29 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

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()