Skip to content

Commit

Permalink
unified helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ribejara-te committed Jan 16, 2025
1 parent 15e2852 commit 54165b6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"jinja2>=3.1.4",
"packaging>=24.2",
"python-hcl2>=5.1.1",
"pyyaml>=6.0.2",
]

[project.scripts]
Expand Down
22 changes: 16 additions & 6 deletions src/stacks/helpers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import pathlib

import hcl2
import yaml

from .crypto import decrypt
from .merge import merge


def config_read(patterns, decoderfunc, **decoderargs):
def config_read(patterns, should_decrypt, decoderfunc, **decoderargs):
"""Read configuration files in 'patterns' using 'decoderfunc' and return their merged contents.
Keyword arguments:
patterns[list]: patterns to configuration files, in ascending order of priority
should_decrypt[bool]: whether to decrypt data or not
decoderfunc[function]: function that parses a given configuration file into a data structure
decoderargs[dict]: keyword arguments to pass to decoderfunc
"""
Expand All @@ -24,15 +26,19 @@ def config_read(patterns, decoderfunc, **decoderargs):
if path.is_file():
with open(path, "r") as f:
data = merge(data, decoderfunc(f, **decoderargs))
return decrypt(data)
return decrypt(data) if should_decrypt else data


def json_read(patterns):
return config_read(patterns, json.load)
def json_read(patterns, should_decrypt=True):
return config_read(patterns, should_decrypt, json.load)


def hcl2_read(patterns):
return config_read(patterns, hcl2.load)
def yaml_read(patterns, should_decrypt=True):
return config_read(patterns, should_decrypt, yaml.safe_load)


def hcl2_read(patterns, should_decrypt=True):
return config_read(patterns, should_decrypt, hcl2.load)


def config_write(data, path, encoderfunc, **encoderargs):
Expand All @@ -50,3 +56,7 @@ def config_write(data, path, encoderfunc, **encoderargs):

def json_write(data, path):
config_write(data, path, json.dump, indent=2)


def yaml_write(data, path):
config_write(data, path, yaml.dump, indent=2, width=1000)
46 changes: 46 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 54165b6

Please sign in to comment.