Apr-21-2019, 05:11 PM
I added code to print out dictionary before and after changes, also cleaned up code a bit
** Note ** Uses f-string which requires python 3.6 or newer
** Note ** Uses f-string which requires python 3.6 or newer
import json import os def replace_acls(self, filename, dn, role): with open(filename, 'r') as f: dn = f.read().replace(role, dn) with open(filename, 'w') as f: f.write(dn) def display_dict(dictname, level=0): ''' Display formatted dictionary ''' indent = " " * (4 * level) for key, value in dictname.items(): if isinstance(value, dict): print(f'\n{indent}{key}') level += 1 display_dict(value, level) else: print(f'{indent}{key}: {value}') if level > 0: level -= 1 def main(): # anchor starting directory os.chdir(os.path.abspath(os.path.dirname(__file__))) with open('acl.json', 'r') as f: data = json.load(f) print(f'\nDictionary before modification', end='') display_dict(data) data['roles']['Deployer'] = ['CN=Dev_Deployer','OU=test','DC=example','DC=com'] print(f'\nDictionary after modification', end='') display_dict(data) with open('acl.json', 'w') as f: json.dump(data, f, indent=2) if __name__ == '__main__': main()Output:
Output:Dictionary before modification
operations
ops_modify
paths: ['ops_set_config']
methods: ['GET']
ops_read
paths: ['ops_get_summary', 'ops_get_request_info']
methods: ['GET']
roles
Server Op: ['emc', 'mgmt', 'fips_read']
Deployer: ['mgmt', 'config']
KP Admin: ['mgmt', 'kp']
permissions
logs: ['ops_read', 'file']
adminusers: ['adminusers.read']
Dictionary after modification
operations
ops_modify
paths: ['ops_set_config']
methods: ['GET']
ops_read
paths: ['ops_get_summary', 'ops_get_request_info']
methods: ['GET']
roles
Server Op: ['emc', 'mgmt', 'fips_read']
Deployer: ['CN=Dev_Deployer', 'OU=test', 'DC=example', 'DC=com']
KP Admin: ['mgmt', 'kp']
permissions
logs: ['ops_read', 'file']
adminusers: ['adminusers.read']