def test_save_kwargs(self): doc = odml.load(self.file) file_name = "%s_copy" % self.file # Test unsupported kwarg does not raise an exception odml.save(doc, file_name, unsupported_kwarg="I do not matter") os.remove(file_name)
def _convert_file(cls, input_path, output_path, res_format): """ Converts a file from given input_path to res_format. Will raise a ValueError if the provided output format (res_format) is not supported. :param input_path: full path including file name of the file to be converted. :param output_path: full path including file name of the output file. If required the file extension will be adjusted to the output format. :param res_format: Format the input file will be converted to. Only formats listed in constant CONVERSION_FORMATS are supported. """ if res_format == "v1_1": VersionConverter(input_path).write_to_file(output_path) elif res_format == "odml": if not output_path.endswith(".odml"): file_path, _ = os.path.splitext(output_path) output_path = file_path + ".odml" odml.save(odml.load(input_path), output_path) elif res_format in CONVERSION_FORMATS: if not output_path.endswith(CONVERSION_FORMATS[res_format]): file_path, _ = os.path.splitext(output_path) output_path = file_path + CONVERSION_FORMATS[res_format] RDFWriter(odml.load(input_path)).write_file( output_path, res_format) else: raise ValueError("Format for output files is incorrect. " "Please choose from the list: {}".format( list(CONVERSION_FORMATS)))
def setUp(self): """ Set up local temporary terminology files in a temporary folder """ tmp_dir = create_test_dir(__file__) tmp_name = os.path.basename(tmp_dir) main_name = "%s_main.xml" % tmp_name main_file_path = os.path.join(tmp_dir, main_name) main_url = "file://%s" % pathname2url(main_file_path) include_name = "%s_include.xml" % tmp_name include_file_path = os.path.join(tmp_dir, include_name) include_url = "file://%s" % pathname2url(include_file_path) include_doc = Document() _ = Section(name="include_sec", type="test", parent=include_doc) save(include_doc, include_file_path) main_doc = Document() _ = Section(name="main_sec", type="test", include=include_url, parent=main_doc) save(main_doc, main_file_path) self.main_terminology_url = main_url self.temp_dir_base = tmp_name
def test_invalid_parser(self): with self.assertRaises(NotImplementedError): odml.load(self.file, 'html') doc = odml.load(self.file) with self.assertRaises(NotImplementedError): odml.save(doc, self.file + '_copy_html', 'html') with self.assertRaises(NotImplementedError): odml.display(doc, 'html')
def test_invalid_parser(self): with self.assertRaises(ValueError): odml.load(self.file, 'html') doc = odml.load(self.file) with self.assertRaises(ValueError): odml.save(doc, self.file + '_copy_html', 'html') with self.assertRaises(ValueError): odml.display(doc, 'html')
def test_invalid_parser(self): with self.assertRaises(NotImplementedError): odml.load(self.file, "html") doc = odml.load(self.file) with self.assertRaises(NotImplementedError): file_name = "%s_copy_html" % self.file odml.save(doc, file_name, "html") with self.assertRaises(NotImplementedError): odml.display(doc, "html")
def save(self, uri, file_type=None): # Mandatory document validation before save to avoid # not being able to open an invalid document. self.remove_validation() validation = odml.validation.Validation(self.document) self.document.validation_result = validation for err in self.document.validation_result.errors: if err.is_error: self.window._info_bar.show_info( "Invalid document. Please fix errors (red) before saving.") self.validate() return self.document.clean() parser = None if file_type: parser = get_parser_for_file_type(file_type) if not parser: parser = get_parser_for_uri(uri) file_path = uri_to_path(uri) ext = get_extension(file_path) if ext != parser: file_path += ".%s" % parser.lower() try: odml.save(self.document, file_path, parser) except Exception as exc: self.window._info_bar.show_info("Save failed: %s" % exc) return # undo the clean self.document.finalize() # Finalize also removes all pseudo_values for any unchanged terminology # entries, rendering these Properties unmodifiable. Re-initialize # the pseudo_values for these Properties. for sec in self.document.sections: handle_section_import(sec) self.window._info_bar.show_info("%s was saved" % (os.path.basename(file_path))) self.edited = len(self.command_manager) return True
def _convert_file(cls, input_path, output_path, res_format): """ Convert a file from given input_path to res_format. """ if res_format == "v1_1": VersionConverter(input_path).write_to_file(output_path) elif res_format == "odml": if not output_path.endswith(".odml"): p, _ = os.path.splitext(output_path) output_path = p + ".odml" odml.save(odml.load(input_path), output_path) elif res_format in cls._conversion_formats: if not output_path.endswith(cls._conversion_formats[res_format]): p, _ = os.path.splitext(output_path) output_path = p + cls._conversion_formats[res_format] RDFWriter(odml.load(input_path)).write_file(output_path, res_format)
def _convert_file(cls, input_path, output_path, res_format): """ Convert a file from given input_path to res_format. """ if res_format == "v1_1": VersionConverter(input_path).write_to_file(output_path) elif res_format == "odml": if not output_path.endswith(".odml"): p, _ = os.path.splitext(output_path) output_path = p + ".odml" odml.save(odml.load(input_path), output_path) elif res_format in CONVERSION_FORMATS: if not output_path.endswith(CONVERSION_FORMATS[res_format]): p, _ = os.path.splitext(output_path) output_path = p + CONVERSION_FORMATS[res_format] RDFWriter(odml.load(input_path)).write_file( output_path, res_format) else: raise ValueError("Format for output files is incorrect. " "Please choose from the list: {}".format( list(CONVERSION_FORMATS)))
def test_float(self): val_in = [1, 2.2, 3.3, "4"] val_odml = [1.0, 2.2, 3.3, 4.0] parent_sec = self.doc.sections[0] _ = odml.Property(name="float test", dtype="float", value=val_in, parent=parent_sec) # Test correct json save and load. odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") self.assertEqual(jdoc.sections[0].properties[0].dtype, odml.dtypes.DType.float) self.assertEqual(jdoc.sections[0].properties[0].values, val_odml) self.assertEqual(self.doc, jdoc) # Test correct xml save and load. odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) self.assertEqual(xdoc.sections[0].properties[0].dtype, odml.dtypes.DType.float) self.assertEqual(xdoc.sections[0].properties[0].values, val_odml) self.assertEqual(self.doc, xdoc) # Test correct yaml save and load. odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") self.assertEqual(ydoc.sections[0].properties[0].dtype, odml.dtypes.DType.float) self.assertEqual(ydoc.sections[0].properties[0].values, val_odml) self.assertEqual(self.doc, ydoc)
def test_tuple(self): val_type = "3-tuple" val_in = "(1; 1; 1)" val_odml = ["1", "1", "1"] parent_sec = self.doc.sections[0] _ = odml.Property(name="tuple test single", dtype=val_type, value=val_in, parent=parent_sec) # Test correct json save and load. odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") self.assertEqual(jdoc.sections[0].properties[0].dtype, val_type) self.assertEqual(jdoc.sections[0].properties[0].values, [val_odml]) self.assertEqual(self.doc, jdoc) # Test correct xml save and load. odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) self.assertEqual(xdoc.sections[0].properties[0].dtype, val_type) self.assertEqual(xdoc.sections[0].properties[0].values, [val_odml]) self.assertEqual(self.doc, xdoc) # Test correct yaml save and load. odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") self.assertEqual(ydoc.sections[0].properties[0].dtype, val_type) self.assertEqual(ydoc.sections[0].properties[0].values, [val_odml]) self.assertEqual(self.doc, ydoc)
def test_date(self): date_string = '2018-08-31' date = dt.date(2018, 8, 31) val_in = date_string vals_in = [None, "", date_string, date] parent_sec = self.doc.sections[0] _ = odml.Property(name="date test single", dtype="date", value=val_in, parent=parent_sec) _ = odml.Property(name="date test", dtype="date", value=vals_in, parent=parent_sec) # Test correct json save and load. odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") self.assertEqual(jdoc.sections[0].properties[0].dtype, odml.dtypes.DType.date) self.assertIsInstance(jdoc.sections[0].properties[0].values[0], dt.date) self.assertEqual(jdoc.sections[0].properties[1].dtype, odml.dtypes.DType.date) for val in jdoc.sections[0].properties[1].value: self.assertIsInstance(val, dt.date) self.assertEqual(jdoc.sections[0].properties[1].values[2], date) self.assertEqual(jdoc.sections[0].properties[1].values[3], date) self.assertEqual(self.doc, jdoc) # Test correct xml save and load. odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) self.assertEqual(xdoc.sections[0].properties[0].dtype, odml.dtypes.DType.date) self.assertIsInstance(xdoc.sections[0].properties[0].values[0], dt.date) self.assertEqual(xdoc.sections[0].properties[1].dtype, odml.dtypes.DType.date) for val in xdoc.sections[0].properties[1].value: self.assertIsInstance(val, dt.date) self.assertEqual(xdoc.sections[0].properties[1].values[2], date) self.assertEqual(xdoc.sections[0].properties[1].values[2], date) self.assertEqual(self.doc, xdoc) # Test correct yaml save and load. odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") self.assertEqual(ydoc.sections[0].properties[0].dtype, odml.dtypes.DType.date) self.assertIsInstance(ydoc.sections[0].properties[0].values[0], dt.date) self.assertEqual(ydoc.sections[0].properties[1].dtype, odml.dtypes.DType.date) for val in ydoc.sections[0].properties[1].value: self.assertIsInstance(val, dt.date) self.assertEqual(ydoc.sections[0].properties[1].values[2], date) self.assertEqual(ydoc.sections[0].properties[1].values[2], date) self.assertEqual(self.doc, ydoc)
def test_time(self): time_string = '12:34:56' time = dt.time(12, 34, 56) val_in = time_string vals_in = [None, "", time_string, time] parent_sec = self.doc.sections[0] _ = odml.Property(name="time test single", dtype="time", value=val_in, parent=parent_sec) _ = odml.Property(name="time test", dtype="time", value=vals_in, parent=parent_sec) # Test correct json save and load. odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") self.assertEqual(jdoc.sections[0].properties[0].dtype, odml.dtypes.DType.time) self.assertIsInstance(jdoc.sections[0].properties[0].values[0], dt.time) self.assertEqual(jdoc.sections[0].properties[1].dtype, odml.dtypes.DType.time) for val in jdoc.sections[0].properties[1].value: self.assertIsInstance(val, dt.time) self.assertEqual(jdoc.sections[0].properties[1].values[2], time) self.assertEqual(jdoc.sections[0].properties[1].values[3], time) self.assertEqual(self.doc, jdoc) # Test correct xml save and load. odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) self.assertEqual(xdoc.sections[0].properties[0].dtype, odml.dtypes.DType.time) self.assertIsInstance(xdoc.sections[0].properties[0].values[0], dt.time) self.assertEqual(xdoc.sections[0].properties[1].dtype, odml.dtypes.DType.time) for val in xdoc.sections[0].properties[1].value: self.assertIsInstance(val, dt.time) self.assertEqual(xdoc.sections[0].properties[1].values[2], time) self.assertEqual(xdoc.sections[0].properties[1].values[2], time) self.assertEqual(self.doc, xdoc) # Test correct yaml save and load. odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") self.assertEqual(ydoc.sections[0].properties[0].dtype, odml.dtypes.DType.time) self.assertIsInstance(ydoc.sections[0].properties[0].values[0], dt.time) self.assertEqual(ydoc.sections[0].properties[1].dtype, odml.dtypes.DType.time) for val in ydoc.sections[0].properties[1].value: self.assertIsInstance(val, dt.time) self.assertEqual(ydoc.sections[0].properties[1].values[2], time) self.assertEqual(ydoc.sections[0].properties[1].values[2], time) self.assertEqual(self.doc, ydoc)
def save_load(self): """ Helper method to save and load the current state of the document with all supported parsers. :return: jdoc ... document loaded from JSON file xdoc ... document loaded from XML file ydoc ... document loaded from YAML file """ odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") return jdoc, xdoc, ydoc
def save_load(self): """ Helper method to save and load the current state of the document with all supported parsers. :return: jdoc ... document loaded from JSON file xdoc ... document loaded from XML file ydoc ... document loaded from YAML file """ odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") return jdoc, xdoc, ydoc
def test_sec_cardinality(self): """ Test saving and loading of Section sections cardinality variants to and from all supported file formats. """ doc = odml.Document() sec_empty = "card_empty" sec_max = "card_max" sec_min = "card_min" sec_full = "card_full" card_dict = { sec_empty: None, sec_max: (None, 10), sec_min: (2, None), sec_full: (1, 5) } _ = odml.Section(name=sec_empty, type="test", parent=doc) _ = odml.Section(name=sec_max, sec_cardinality=card_dict[sec_max], type="test", parent=doc) _ = odml.Section(name=sec_min, sec_cardinality=card_dict[sec_min], type="test", parent=doc) _ = odml.Section(name=sec_full, sec_cardinality=card_dict[sec_full], type="test", parent=doc) # Test saving to and loading from an XML file odml.save(doc, self.xml_file) xml_doc = odml.load(self.xml_file) self._test_cardinality_load("sec_cardinality", xml_doc, card_dict, sec_empty, sec_max, sec_min, sec_full) # Test saving to and loading from a JSON file odml.save(doc, self.json_file, "JSON") json_doc = odml.load(self.json_file, "JSON") self._test_cardinality_load("sec_cardinality", json_doc, card_dict, sec_empty, sec_max, sec_min, sec_full) # Test saving to and loading from a YAML file odml.save(doc, self.yaml_file, "YAML") yaml_doc = odml.load(self.yaml_file, "YAML") self._test_cardinality_load("sec_cardinality", yaml_doc, card_dict, sec_empty, sec_max, sec_min, sec_full)
def test_bool(self): val_in = True vals_in = [None, "", [], {}, False, True, "TRUE"] vals_odml = [False, False, False, False, False, True, True] parent_sec = self.doc.sections[0] _ = odml.Property(name="bool test single", dtype="boolean", value=val_in, parent=parent_sec) _ = odml.Property(name="bool test", dtype="boolean", value=vals_in, parent=parent_sec) # Test correct json save and load. odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") self.assertEqual(jdoc.sections[0].properties[0].dtype, odml.dtypes.DType.boolean) self.assertEqual(jdoc.sections[0].properties[0].values, [val_in]) self.assertEqual(jdoc.sections[0].properties[1].dtype, odml.dtypes.DType.boolean) self.assertEqual(jdoc.sections[0].properties[1].values, vals_odml) self.assertEqual(self.doc, jdoc) # Test correct xml save and load. odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) self.assertEqual(xdoc.sections[0].properties[0].dtype, odml.dtypes.DType.boolean) self.assertEqual(xdoc.sections[0].properties[0].values, [val_in]) self.assertEqual(xdoc.sections[0].properties[1].dtype, odml.dtypes.DType.boolean) self.assertEqual(xdoc.sections[0].properties[1].values, vals_odml) self.assertEqual(self.doc, xdoc) # Test correct yaml save and load. odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") self.assertEqual(ydoc.sections[0].properties[0].dtype, odml.dtypes.DType.boolean) self.assertEqual(ydoc.sections[0].properties[0].values, [val_in]) self.assertEqual(ydoc.sections[0].properties[1].dtype, odml.dtypes.DType.boolean) self.assertEqual(ydoc.sections[0].properties[1].values, vals_odml) self.assertEqual(self.doc, ydoc)
def test_str(self): val_in = "single value" vals_in = [None, "", [], {}, 1, True, "text"] vals_odml = ["", "", "", "", "1", "True", "text"] parent_sec = self.doc.sections[0] _ = odml.Property(name="string test single", dtype="string", value=val_in, parent=parent_sec) _ = odml.Property(name="string test", dtype="string", value=vals_in, parent=parent_sec) # Test correct json save and load. odml.save(self.doc, self.json_file, "JSON") jdoc = odml.load(self.json_file, "JSON") self.assertEqual(jdoc.sections[0].properties[0].dtype, odml.dtypes.DType.string) self.assertEqual(jdoc.sections[0].properties[0].values, [val_in]) self.assertEqual(jdoc.sections[0].properties[1].dtype, odml.dtypes.DType.string) self.assertEqual(jdoc.sections[0].properties[1].values, vals_odml) self.assertEqual(self.doc, jdoc) # Test correct xml save and load. odml.save(self.doc, self.xml_file) xdoc = odml.load(self.xml_file) self.assertEqual(xdoc.sections[0].properties[0].dtype, odml.dtypes.DType.string) self.assertEqual(xdoc.sections[0].properties[0].values, [val_in]) self.assertEqual(xdoc.sections[0].properties[1].dtype, odml.dtypes.DType.string) self.assertEqual(xdoc.sections[0].properties[1].values, vals_odml) self.assertEqual(self.doc, xdoc) # Test correct yaml save and load. odml.save(self.doc, self.yaml_file, "YAML") ydoc = odml.load(self.yaml_file, "YAML") self.assertEqual(ydoc.sections[0].properties[0].dtype, odml.dtypes.DType.string) self.assertEqual(ydoc.sections[0].properties[0].values, [val_in]) self.assertEqual(ydoc.sections[0].properties[1].dtype, odml.dtypes.DType.string) self.assertEqual(ydoc.sections[0].properties[1].values, vals_odml) self.assertEqual(self.doc, ydoc)
def test_cardinality(self): """ Test saving and loading of property values cardinality variants to and from all supported file formats. """ doc = odml.Document() sec = odml.Section(name="sec", type="sometype", parent=doc) prop_empty = "prop_cardinality_empty" prop_max = "prop_cardinality_max" prop_max_card = (None, 10) prop_min = "prop_cardinality_min" prop_min_card = (2, None) prop_full = "prop_full" prop_full_card = (1, 5) _ = odml.Property(name=prop_empty, parent=sec) _ = odml.Property(name=prop_max, val_cardinality=prop_max_card, parent=sec) _ = odml.Property(name=prop_min, val_cardinality=prop_min_card, parent=sec) _ = odml.Property(name=prop_full, val_cardinality=prop_full_card, parent=sec) # Test saving to and loading from an XML file odml.save(doc, self.xml_file) xml_doc = odml.load(self.xml_file) xml_prop = xml_doc.sections["sec"].properties[prop_empty] self.assertIsNone(xml_prop.val_cardinality) xml_prop = xml_doc.sections["sec"].properties[prop_max] self.assertEqual(xml_prop.val_cardinality, prop_max_card) xml_prop = xml_doc.sections["sec"].properties[prop_min] self.assertEqual(xml_prop.val_cardinality, prop_min_card) xml_prop = xml_doc.sections["sec"].properties[prop_full] self.assertEqual(xml_prop.val_cardinality, prop_full_card) # Test saving to and loading from a JSON file odml.save(doc, self.json_file, "JSON") json_doc = odml.load(self.json_file, "JSON") json_prop = json_doc.sections["sec"].properties[prop_empty] self.assertIsNone(json_prop.val_cardinality) json_prop = json_doc.sections["sec"].properties[prop_max] self.assertEqual(json_prop.val_cardinality, prop_max_card) json_prop = json_doc.sections["sec"].properties[prop_min] self.assertEqual(json_prop.val_cardinality, prop_min_card) json_prop = json_doc.sections["sec"].properties[prop_full] self.assertEqual(json_prop.val_cardinality, prop_full_card) # Test saving to and loading from a YAML file odml.save(doc, self.yaml_file, "YAML") yaml_doc = odml.load(self.yaml_file, "YAML") yaml_prop = yaml_doc.sections["sec"].properties[prop_empty] self.assertIsNone(yaml_prop.val_cardinality) yaml_prop = yaml_doc.sections["sec"].properties[prop_max] self.assertEqual(yaml_prop.val_cardinality, prop_max_card) yaml_prop = yaml_doc.sections["sec"].properties[prop_min] self.assertEqual(yaml_prop.val_cardinality, prop_min_card) yaml_prop = yaml_doc.sections["sec"].properties[prop_full] self.assertEqual(yaml_prop.val_cardinality, prop_full_card)
def test_load_save(self): doc = odml.load(self.file) self.assertTrue(isinstance(doc, odml.doc.BaseDocument)) odml.save(doc, self.file + '_copy') os.remove(self.file + '_copy')
def test_load_save(self): doc = odml.load(self.file) self.assertTrue(isinstance(doc, odml.doc.BaseDocument)) odml.save(doc, self.file + '_copy') os.remove(self.file + '_copy')
# APPEND PROPERTIES WIHT VALUES parent.append(odml.Property(name="Type", value="Genuine People Personality", dtype=odml.DType.string, definition="Type of robot")) parent.append(odml.Property(name="Manufacturer", value="Sirius Cybernetics Corporation", dtype=odml.DType.string, definition="Manufacturer of robots")) # SET NEW PARENT NODE parent = doc['TheStarship']['Cybernetics']['Eddie'] # APPEND SUBSECTIONS # APPEND PROPERTIES WIHT VALUES parent.append(odml.Property(name="Type", value="Genuine People Personality", dtype=odml.DType.string, definition="Type of robot")) parent.append(odml.Property(name="Manufacturer", value="Sirius Cybernetics Corporation", dtype=odml.DType.string, definition="Manufacturer of robots")) odml.save(doc, save_to)
def test_load_save(self): doc = odml.load(self.file) self.assertTrue(isinstance(doc, odml.doc.BaseDocument)) file_name = "%s_copy" % self.file odml.save(doc, file_name) os.remove(file_name)
def odmlwrite(nix_file, filename): odml_doc, nix_sections = get_odml_doc(nix_file) nix_to_odml_recurse(nix_sections, odml_doc) odml.save(odml_doc, filename)