Skip to content

Instantly share code, notes, and snippets.

@davzaman
Last active January 5, 2021 21:49
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 davzaman/91b6c789e296798207e1cbc724c1c477 to your computer and use it in GitHub Desktop.
Save davzaman/91b6c789e296798207e1cbc724c1c477 to your computer and use it in GitHub Desktop.
Parse Guild YAML file to execute your project on the command-line without logging anything (for debugging/testing/etc purposes). Also included is an example bash script you can create to use this for your project.
import sys
import yaml
from parse_yaml import parse
with open("guild.yml", "r") as f:
res = parse(yaml.safe_load(f))
d = {f"--{k.replace('_','-')}": str(v) for k, v in res.items()}
if d["--toggle-var-on"] == 'False':
del d["--toggle-var-on"]
sys.argv += [item for k in d for item in (k, d[k])]
import sys
import yaml
from collections import ChainMap
def parse(obj, base_prefix=""):
"""
The results of loading the guild yml file is a list of dictionaries.
Each item in the list is an object in the file represented as a dict.
-config: common-flags will become {"config": "common-flags", ...}
-flags: k: v, k: v, ... will be {"flags": {k: v, k: v}}
This all goes into the same dict object.
For multiple config groupings we'll have different objects.
"""
# Grabs the config objects from the yaml file and then merges them.
# the if: grab only config objects.
# the chainmap will merge all the flag dictionaries from each group.
# if it encounters the same name later, it keeps the first one
flags = dict(
ChainMap(*[flag_group["flags"] for flag_group in obj if "config" in flag_group])
)
for k, v in flags.items():
# ingore the $includes, because bash will think it's a var
if k != "$include":
# if the yaml has a list, just pick the first one for testing purposes
if isinstance(v, list):
v = v[0]
print('{}="{}"'.format(k.replace("-", "_"), v))
return {k: v[0] if isinstance(v, list) else v for k, v in flags.items()}
if __name__ == "__main__":
with open(sys.argv[1], "r") as f:
parse(yaml.safe_load(f))
## Debugging
# with open("guild.yml") as f:
# parse(yaml.safe_load(f))
#!/bin/bash
# parses the yaml file and plugs in to command to run without logging to guild
eval $(python parse_yaml.py guild.yml)
# OVERRIDING VARIABLES
var_to_override=3
other_var_to_override=True
args=(
--some-var=$some_var
--var-name-in-yml=$var_name_in_yml
${@:1} # rest of args passed to this script minus the first
)
# TOGGLE FLAGS
((var_toggle_on = true)) && args+=( --var-toggle-on )
# prints out the following command, for debugging
set -x
python my_python_file.py "${args[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment