Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
import yaml
import json
"""
DESCRIPTION: The goal of this script is to find the outermost/top-level key in
the following YAML document, given only the sub-group/sub-key name.
EXAMPLE: Say you only know the sub-group "mars" or "pluto", and you want to
know which top-level key they belong to (i.e., "apps" and "services",
respectively). The find_category() function will do just that, albeit, very
inefficiently.
FIXME: There must be a more efficient way to do this!
$ cat directory.yml
---
env: dev
apps:
mars:
app_name: online_billpay
app_version: 1.4.3
jupiter:
app_name: login_page
app_version: 4.2.7
services:
pluto:
app_name: reverse_proxy
app_version: 9.5.10
"""
ENVS = ['dev', 'qa', 'production']
def find_category(grouping):
for category, app_info in yml_data.iteritems():
if app_info in ENVS:
continue
for group_name, variables in app_info.iteritems():
if group_name == grouping:
return category
with open('directory.yml') as dir_yml:
yml_data = yaml.load(dir_yml)
print find_category("mars") # => apps
print find_category("pluto") # => services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment