Last active
April 13, 2023 03:35
-
-
Save kontza/e45b15b18acf80f75920cdf8131b2c13 to your computer and use it in GitHub Desktop.
Convert either a given YAML file or a YAML from stdin into Java style properties. Requires click, flatdict & pyyaml.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import click | |
import flatdict | |
import yaml | |
import sys | |
class DefaultToStdin(click.Argument): | |
def __init__(self, *args, **kwargs): | |
kwargs["nargs"] = 1 | |
kwargs["type"] = click.File("r") | |
super().__init__(*args, **kwargs) | |
def full_process_value(self, ctx, value): | |
return super().process_value(ctx, value or ("-",)) | |
@click.command( | |
help=("Convert a YAML to Java properties format"), | |
) | |
@click.argument("input_file", cls=DefaultToStdin) | |
@click.option( | |
"--no-values", | |
"-n", | |
default=False, | |
is_flag=True, | |
help="Do not print values, only keys", | |
) | |
def main(input_file, no_values): | |
if not input_file: | |
input_file = (sys.stdin,) | |
doc = yaml.safe_load(input_file) | |
flattened = flatdict.FlatDict(doc, delimiter=".") | |
keys = sorted(flattened.keys()) | |
for key in keys: | |
print(key, end="") | |
if not no_values: | |
print(f" = {flattened[key]}", end="") | |
print() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment