Skip to content

Instantly share code, notes, and snippets.

@christophchamp
Created May 12, 2017 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophchamp/d1bede325cd5c3daa345638d9db963c8 to your computer and use it in GitHub Desktop.
Save christophchamp/d1bede325cd5c3daa345638d9db963c8 to your computer and use it in GitHub Desktop.
#!/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