def write_json_output(directory, filename, json_data): """Write JSON to file whose name is passed.""" try: create_directory(directory) filename = directory + '/' + filename with open(filename, 'w') as outfile: json.dump(json_data, outfile) except OSError: # print('Unable to write JSON data to output file named ' + filename) raise
def test_create_sort_section(self): """ test _create_sort_section """ json_input = get_json_input() xml = _create_sort_section(json_input) create_directory('actual_results') create_directory('expected_results') actual_results_file_name = 'actual_results/test_create_sort_section.xml' write_pnx_file('.', actual_results_file_name, ElementTree.ElementTree(xml)) actual_results = ElementTree.ElementTree(file=actual_results_file_name) expected_results_file_name = 'expected_results/test_create_sort_section.xml' expected_results = ElementTree.ElementTree(file=expected_results_file_name) self.assertTrue(ElementTree.tostring(actual_results.getroot()) == ElementTree.tostring(expected_results.getroot()))
def test_create_pnx_from_json(self): """ run test to create PNX record for a single item from a single JSON file """ json_input = get_json_input() xml_tree = create_pnx_from_json(json_input) create_directory('actual_results') create_directory('expected_results') actual_results_file_name = 'actual_results/test_create_pnx_from_json.xml' expected_results_file_name = 'expected_results/test_create_pnx_from_json.xml' write_pnx_file('.', actual_results_file_name, xml_tree) actual_results = ElementTree.ElementTree(file=actual_results_file_name) expected_results = ElementTree.ElementTree(file=expected_results_file_name) # print(ElementTree.tostring(xml_tree.getroot())) self.assertTrue(ElementTree.tostring(actual_results.getroot()) == ElementTree.tostring(expected_results.getroot()))
def test_create_metadata_rows(self): """ test _create_metadata_rows """ json_data = get_json_input() metadata_results = _create_metadata_rows(json_data) create_directory('actual_results') with open('./actual_results/test_create_metadata_rows.json', 'w') as outfile: json.dump(metadata_results, outfile) with open('./expected_results/test_create_metadata_rows.json', 'r') as expected_file: expected_results = json.load(expected_file) expected_file.close() with open('./actual_results/test_create_metadata_rows.json', 'r') as actual_file: actual_results = json.load(actual_file) actual_file.close() self.assertTrue(actual_results == expected_results)
def test_create_metadata_array_row(self): """ test _create_metadata_array_row(json_data, 'bibliography', 'Bibliography') """ json_data = get_json_input() row = _create_metadata_array_row(json_data, 'bibliography', 'Bibliography') create_directory('actual_results') with open('./actual_results/test_create_metadata_array_row.json', 'w') as outfile: json.dump(row, outfile) with open('./expected_results/test_create_metadata_array_row.json', 'r') as expected_file: expected_results = json.load(expected_file) expected_file.close() with open('./actual_results/test_create_metadata_array_row.json', 'r') as actual_file: actual_results = json.load(actual_file) actual_file.close() self.assertTrue(actual_results == expected_results)
def write_main_csv(csv_directory, json_data): """ write main.csv """ csv_filename = 'main.csv' create_directory(csv_directory) with open(csv_directory + '/' + csv_filename, 'w') as main_csv: fieldnames = ['Label', 'Description', 'License', 'Attribution', 'Sequence_filename', 'Sequence_label', 'Sequence_viewing_experience', 'unique_identifier', 'Metadata_label', 'Metadata_value'] writer = csv.DictWriter(main_csv, fieldnames=fieldnames, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) writer.writeheader() writer.writerow(_create_first_row(json_data)) writer.writerows(_create_metadata_rows(json_data)) main_csv.close()
def recreate_pnx_record(primo_doc_id): """ Create a new PNX record given an existing PNX record """ repository = 'snite' # pnx_xml = get_pnx_given_filename(pnx_filename) try: pnx_xml = get_pnx_xml_given_docid(primo_doc_id) except error.HTTPError: print('HTTPError encountered') pass # if we didn't get a valid pnx entry, we can't do anything with it else: unique_identifier = get_unique_identifier_from_original_pnx(pnx_xml) # print('unique_identifier = ', unique_identifier) corrected_pnx_xml = modify_existing_pnx_record(pnx_xml, repository, unique_identifier) pnx_directory = 'pnx' create_directory(pnx_directory) write_pnx_file(pnx_directory, unique_identifier + '.xml', corrected_pnx_xml)
def write_pnx_file(pnx_directory, filename, xml_tree): """ write pnx xml to file """ # tree = ET.ElementTree(root) create_directory(pnx_directory) xml_tree.write(pnx_directory + '/' + filename)
def save_string_of_xml_to_disk(folder_name, file_name, xml_as_string): """ Write string of xml to disk """ create_directory(folder_name) full_path_file_name = get_full_path_file_name(folder_name, file_name) with open(full_path_file_name, "w") as xml_file: xml_file.write(xml_as_string)
def write_xml_output_file(folder_name, file_name, xml_tree): """ Write xml to output file """ create_directory(folder_name) full_path_file_name = get_full_path_file_name(folder_name, file_name) xml_tree.write(full_path_file_name, encoding="utf-8", xml_declaration=True)