Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created August 17, 2023 11:00
Show Gist options
  • Save mvallebr/0871ec67289147789dda2232d68884b8 to your computer and use it in GitHub Desktop.
Save mvallebr/0871ec67289147789dda2232d68884b8 to your computer and use it in GitHub Desktop.
from typing import Dict
import os
def read_properties(file_name: str) -> Dict[str,str]:
"""
Read a property file with 1 key/value per line - no multiline supported
Multiple lines with the same key would return the last line
"""
with open(file_name) as f:
l = [line.split("=") for line in f.readlines()]
return {key.strip(): value.strip() for key, value in l}
def read_env_properties(file_prefix: str="config",
config_folder: str = os.getcwd(),
env_var_name: str = "ENVIRONMENT") -> Dict[str,str]:
"""
Given file_prefix set to "config" and the value of env var set to dev, it would read
"config.dev.properties" files from current directory.
"""
path = os.path.join(config_folder, f"{file_prefix}.{os.environ[env_var_name]}.properties")
return read_properties(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment