def save_vocables(cls, vocable_list, xml_file_path, xsd_file_path): delimiter = AppSettings.get_setting_by_name(AppSettings.ATTRIBUTE_VALUE_SEPARATOR_SETTING_NAME) xml_parser = XMLParser() new_xml_root = etree.Element("list") for vocable in vocable_list: vocable_node = FileManager.create_vocable_xml_node(new_xml_root, vocable, delimiter) # # test print # rough_string = etree.tostring(new_xml_root, encoding='unicode') # reparsed = minidom.parseString(rough_string) # pretty_printed = reparsed.toprettyxml(indent='\t', encoding='utf-8') # print(pretty_printed[:600]) # with open('/home/xiaolong/testfile.xml', 'w') as file: # file.write(str(pretty_printed)) # # end if xml_parser.validate_tree(xsd_file_path, new_xml_root): xml_parser.write_xml_file(xml_file_path, new_xml_root) else: raise XMLInvalidException( "The XML is not valid. Could not write vocables to file: " + xml_file_path + " validating against XSD: " + xsd_file_path )
def load_vocables(cls, xml_file_path, xsd_file_path): vocables = [] xmlparser = XMLParser() xml_root = xmlparser.get_xml_element_tree_root(xsd_file_path, xml_file_path) for item in xml_root: vocable = Vocable( first_language_translations=FileManager.get_attribute_value_list_from_string( item.find(FileManager.FIRST_LANGUAGE_TRANSLATIONS_ATTRIBUTE_NAME).text ), first_language_phonetic_scripts=FileManager.get_attribute_value_list_from_string( item.find(FileManager.FIRST_LANGUAGE_PHONETIC_SCRIPT_ATTRIBUTE_NAME).text ), second_language_translations=FileManager.get_attribute_value_list_from_string( item.find(FileManager.SECOND_LANGUAGE_TRANSLATIONS_ATTRIBUTE_NAME).text ), second_language_phonetic_scripts=FileManager.get_attribute_value_list_from_string( item.find(FileManager.SECOND_LANGUAGE_PHONETIC_SCRIPT_ATTRIBUTE_NAME).text ), topics=FileManager.get_attribute_value_list_from_string( item.find(FileManager.TOPICS_ATTRIBUTE_NAME).text ), chapters=FileManager.get_attribute_value_list_from_string( item.find(FileManager.CHAPTERS_ATTRIBUTE_NAME).text ), learn_level=item.find(FileManager.LEARN_LEVEL_ATTRIBUTE_NAME).text, relevance_level=item.find(FileManager.RELEVANCE_LEVEL_ATTRIBUTE_NAME).text, description=item.find(FileManager.DESCRIPTION_ATTRIBUTE_NAME).text, ) vocables.append(vocable) return vocables
def save_vocables(cls, vocable_list, xml_file_path, xsd_file_path): delimiter = AppSettings.get_setting_by_name( AppSettings.ATTRIBUTE_VALUE_SEPARATOR_SETTING_NAME) xml_parser = XMLParser() new_xml_root = etree.Element('list') for vocable in vocable_list: vocable_node = FileManager.create_vocable_xml_node( new_xml_root, vocable, delimiter) # # test print # rough_string = etree.tostring(new_xml_root, encoding='unicode') # reparsed = minidom.parseString(rough_string) # pretty_printed = reparsed.toprettyxml(indent='\t', encoding='utf-8') # print(pretty_printed[:600]) # with open('/home/xiaolong/testfile.xml', 'w') as file: # file.write(str(pretty_printed)) # # end if xml_parser.validate_tree(xsd_file_path, new_xml_root): xml_parser.write_xml_file(xml_file_path, new_xml_root) else: raise XMLInvalidException( 'The XML is not valid. Could not write vocables to file: ' + xml_file_path + ' validating against XSD: ' + xsd_file_path)
def load_settings(cls): xml_settings_file_path = get_full_path('res/settings', 'settings.xml') xsd_settings_file_path = get_full_path('res/settings', 'settings.xsd') AppSettings.settings = {} xmlparser = XMLParser() AppSettings.xml_root = xmlparser.get_xml_element_tree_root(xsd_settings_file_path, xml_settings_file_path) for item in AppSettings.xml_root: AppSettings.settings[item.find('name').text] = item.find('value').text
def load_settings(cls): xml_settings_file_path = get_full_path('res/settings', 'settings.xml') xsd_settings_file_path = get_full_path('res/settings', 'settings.xsd') AppSettings.settings = {} xmlparser = XMLParser() AppSettings.xml_root = xmlparser.get_xml_element_tree_root( xsd_settings_file_path, xml_settings_file_path) for item in AppSettings.xml_root: AppSettings.settings[item.find('name').text] = item.find( 'value').text
def load_vocables(cls, xml_file_path, xsd_file_path): vocables = [] xmlparser = XMLParser() xml_root = xmlparser.get_xml_element_tree_root(xsd_file_path, xml_file_path) for item in xml_root: vocable = Vocable( first_language_translations=FileManager. get_attribute_value_list_from_string( item.find( FileManager.FIRST_LANGUAGE_TRANSLATIONS_ATTRIBUTE_NAME ).text), first_language_phonetic_scripts=FileManager. get_attribute_value_list_from_string( item.find( FileManager. FIRST_LANGUAGE_PHONETIC_SCRIPT_ATTRIBUTE_NAME).text), second_language_translations=FileManager. get_attribute_value_list_from_string( item.find( FileManager.SECOND_LANGUAGE_TRANSLATIONS_ATTRIBUTE_NAME ).text), second_language_phonetic_scripts=FileManager. get_attribute_value_list_from_string( item.find( FileManager. SECOND_LANGUAGE_PHONETIC_SCRIPT_ATTRIBUTE_NAME).text), topics=FileManager.get_attribute_value_list_from_string( item.find(FileManager.TOPICS_ATTRIBUTE_NAME).text), chapters=FileManager.get_attribute_value_list_from_string( item.find(FileManager.CHAPTERS_ATTRIBUTE_NAME).text), learn_level=item.find( FileManager.LEARN_LEVEL_ATTRIBUTE_NAME).text, relevance_level=item.find( FileManager.RELEVANCE_LEVEL_ATTRIBUTE_NAME).text, description=item.find( FileManager.DESCRIPTION_ATTRIBUTE_NAME).text, ) vocables.append(vocable) return vocables
def save_settings(cls): # print('trying to save settings:', AppSettings.settings) xml_settings_file_path = get_full_path('res/settings', 'settings.xml') xsd_settings_file_path = get_full_path('res/settings', 'settings.xsd') xml_parser = XMLParser() new_xml_root = etree.Element('list') for key, value in AppSettings.settings.items(): setting_node = etree.SubElement(new_xml_root, 'setting') name_node = etree.SubElement(setting_node, 'name') name_node.text = key value_node = etree.SubElement(setting_node, 'value') value_node.text = value if xml_parser.validate_tree(xsd_settings_file_path, new_xml_root): xml_parser.write_xml_file(xml_settings_file_path, new_xml_root) else: raise XMLInvalidException('The XML is not valid.')
def test_xml_file_validity(self): assert XMLParser.validate_file( xsd_file_path=get_full_path('res/settings', 'settings.xsd'), xml_file_path=get_full_path('res/settings', 'settings.xml'), ), 'Your XML settings file is not valid against the provided XSD file.'