from agent import WORKINGDIR, logger, VRWLDAP from dotenv import dotenv_values import json from pathlib import Path EXPORT_PATH = Path(f'{WORKINGDIR}/export/') if __name__ == "__main__": config_path = f'{WORKINGDIR}/.env' if not Path(config_path).exists(): print('Missing .env file with credentials. Terminate!') quit() config = dotenv_values(config_path) print('Logging into LDAP server') # Load LDAP client ldap_client = VRWLDAP(config['LDAP_HOST'], int(config['LDAP_PORT']), config['LDAP_USER'], config['LDAP_PASS'], config['LDAP_SSL'], not config['LDAP_MANUAL_GROUPS']) if not ldap_client.check_connection(): logger.error(f'Could not login to the LDAP server. Check connection credentials.') quit() print('Start exporting researcher and study data') # Get the data from the LDAP server (researchers, studies) = ldap_client.export() # convert study dict to list. Is less data and easier for the import later on. studies_list = [] for study_data in studies.values(): contributor_list = [] for contributor in study_data['contributors'].values(): contributor_list.append(contributor) study_data['contributors'] = contributor_list studies_list.append(study_data) # Make sure the export folder does exists if not EXPORT_PATH.exists(): EXPORT_PATH.mkdir() # Clear old export for file in EXPORT_PATH.iterdir(): file.unlink() data = { 'researchers': researchers, 'studies': studies_list, } for data_name, data_set in data.items(): json_data = json.dumps(data_set, indent=2) # Write data to file export_file = Path(f'{EXPORT_PATH}/{data_name}.json') export_file.write_text(json_data) print(f'Exported {data_name} data to json file: {export_file}') print('Done exporting VRW LDAP data')