I want to use the following syntax for substituting environment variables into settings, like {env [key]}
>>> person = {'first':'Reuven', 'last':'Lerner'} >>> "Your name is {p[first]}.".format(p=person) Settings I read from a text file in which JSON is saved
if (self.settings is None) and os.path.exists(self.settings_file): with open(self.settings_file, 'r', encoding='utf8') as f: file_content = f.read() file_content_env = file_content.format(env=environment) self.settings = json.loads(file_content_env) but it falls with an error.
file_content_env = file_content.format(env=environment) KeyError: '\n "Type"' How to load settings correctly? Or maybe someone pokes a better solution?
Added an example that crash
environment = { "date": time.strftime("%Y%m%d"), "datetime": time.strftime("%Y%m%d_%H%M"), "workdir": os.path.dirname("") } file_content = """[ { "Type": "test", "Name": "Task from {env[datetime]}", "Path": "{env[workdir]}\\test" } ]""" file_content_env = file_content.format(env=environment)
settings = json.load(f); settings["Path"] = ...settings = json.load(f); settings["Path"] = ...- jfssettings = json.load(f)Step # 2 make changes from environment:settings["Path"] = environment["workdir"] / "test"(used pathlib for convenience). Everything. - jfs