def main(path): properties = Properties() with open(path, "rb") as f: properties.load(f, "utf-8") for key in properties.iterkeys(): if os.environ.get(key.replace('.', '_')): properties[key] = os.environ[key.replace('.', '_')] with open(path, "wb") as f: f.truncate(0) properties.store(f, encoding="utf-8")
def properties_loader_as_dict(file_path): properties = Properties() with open(file_path, 'rb') as cfg_file: properties.load(cfg_file, "utf-8") # change composite key as 'a.b.c=v' to dict '{a:{b:{c:v}}}' rv = dict() for k in properties.iterkeys(): val = properties[k] for subkey in k.split('.')[::-1]: tmp = dict() tmp[subkey] = val val = tmp _dict_merge(rv, tmp) return rv