2019-04-10 17:42:47 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-04-03 23:31:54 +02:00
|
|
|
"""Utility to return YAML value from key in YAML file."""
|
|
|
|
|
|
|
|
import optparse
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
def get_yaml_value(yaml_file, yaml_key):
|
2018-03-27 20:30:46 +02:00
|
|
|
"""Return string value for 'yaml_key' from 'yaml_file'."""
|
2018-04-03 23:31:54 +02:00
|
|
|
with open(yaml_file, "r") as ystream:
|
|
|
|
yaml_dict = yaml.safe_load(ystream)
|
|
|
|
return str(yaml_dict.get(yaml_key, ""))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Execute Main program."""
|
|
|
|
|
|
|
|
parser = optparse.OptionParser(description=__doc__)
|
|
|
|
parser.add_option("--yamlFile", dest="yaml_file", default=None, help="YAML file to read")
|
2018-03-27 20:30:46 +02:00
|
|
|
parser.add_option("--yamlKey", dest="yaml_key", default=None,
|
|
|
|
help="Top level YAML key to provide the value")
|
2018-04-03 23:31:54 +02:00
|
|
|
|
|
|
|
(options, _) = parser.parse_args()
|
|
|
|
if not options.yaml_file:
|
|
|
|
parser.error("Must specifiy '--yamlFile'")
|
|
|
|
if not options.yaml_key:
|
|
|
|
parser.error("Must specifiy '--yamlKey'")
|
|
|
|
|
|
|
|
print(get_yaml_value(options.yaml_file, options.yaml_key))
|
|
|
|
|
2018-03-27 20:30:46 +02:00
|
|
|
|
2018-04-03 23:31:54 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|