def main(): vocable_file_path = input('Vocable file path:') xsd_file_path = input('XSD file path:') words_file_path = input('Word list file path:') attribute_name = input('Attribute name:') words_attribute_name = input('Words attribute name:') attribute_value = input('Attribute value:') if vocable_file_path == '': vocable_file_path = '/home/xiaolong/Development/PycharmProjects/xld-attribute-adder/res/vocables.xml' if xsd_file_path == '': xsd_file_path = '/home/xiaolong/Development/PycharmProjects/xld-attribute-adder/res/xld-vocables-schema.xsd' if words_file_path == '': words_file_path = '/home/xiaolong/Development/PycharmProjects/xld-attribute-adder/res/HSK2' if attribute_name == '': attribute_name = 'chapters' if words_attribute_name == '': words_attribute_name = 'secondLanguageTranslations' if attribute_value == '': attribute_value = 'HSK2-2012' print('inputs received:\n') print('vocable file path: ', vocable_file_path, sep=' ', end='\n') print('xsd file path: ', xsd_file_path, sep=' ', end='\n') print('words file path: ', words_file_path, sep=' ', end='\n') print('attribute name: ', attribute_name, sep=' ', end='\n') print('words attribute name: ', words_attribute_name, sep=' ', end='\n') print('attribute_value: ', attribute_value, sep=' ', end='\n') xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name) VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xld_attribute_adder.xml_root)
def test_write_vocable_file (self): print('current working directory:', os.getcwd()) vocable_file_path = self.create_test_vocables_file() print('--- using vocable file path:', vocable_file_path) list_of_words = ['你','好','吗','我','很'] words_list_file_path = self.create_test_words_file(list_of_words) xsd_file_path = self.project_directory + 'res/xld-vocables-schema.xsd' attribute_name = 'chapters' attribute_value = 'HSK1-2012' words_attribute_name = 'secondLanguageTranslations' xml_parser = XMLParser() xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path) xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name) VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root) assert os.path.isfile(vocable_file_path), 'no file exists' # if the xml is not valid anymore when reading the file, the write function might have done something wrong try: xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path) except XMLParserException as exception: assert False else: assert True
def test_write_vocable_file(self): print("current working directory:", os.getcwd()) vocable_file_path = self.create_test_vocables_file() print("--- using vocable file path:", vocable_file_path) list_of_words = ["你", "好", "吗", "我", "很"] words_list_file_path = self.create_test_words_file(list_of_words) xsd_file_path = self.project_directory + "res/xld-vocables-schema.xsd" attribute_name = "chapters" attribute_value = "HSK1-2012" words_attribute_name = "secondLanguageTranslations" xml_parser = XMLParser() xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path) xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name) VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root) assert os.path.isfile(vocable_file_path), "no file exists" # if the xml is not valid anymore when reading the file, the write function might have done something wrong try: xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path) except XMLParserException as exception: assert False else: assert True
def test_add_values_to_attribute_of_vocables(self): xml_parser = XMLParser() list_of_words = ["你", "好", "吗", "我", "很"] test_word_list_file_path = self.create_test_words_file(list_of_words) test_vocable_file_path = self.create_test_vocables_file() xsd_file_path = self.project_directory + "res/xld-vocables-schema.xsd" # TODO: check if the chapter is not in the vocables already # attribute value hasn't been added yet, so it should not already be in the vocables # this assertion is necessary to check if the add_values_to_attribute_of_vocables really changes something xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path) for vocable in xml_root: vocable_word_attribute_text = vocable.find(self.words_attribute_name).text # is this vocable one of the world list? if WordListHelper.is_in_list( WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True ): print(vocable_word_attribute_text, "is in word list") attribute_text = vocable.find(self.attribute_name).text # does this vocable have the added attribute value? regex = "\s*" + self.attribute_value + "\s*$" print("regex:", regex) print("attribute_text:", attribute_text) assert re.search(regex, attribute_text) is None, ( "attribute value has already added to vocable " + vocable.find(self.words_attribute_name).text ) # now add the attribute value xld_attribute_adder = XLDAttributeAdder(test_vocable_file_path, xsd_file_path, test_word_list_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables( self.attribute_name, self.attribute_value, self.words_attribute_name ) VocableFileWriter.write(VocableFileWriter, xsd_file_path, test_vocable_file_path, xld_attribute_adder.xml_root) # check the vocables again, to see if something has been added xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path) for vocable in xml_root: vocable_word_attribute_text = vocable.find(self.words_attribute_name).text # is this vocable one of the world list? if WordListHelper.is_in_list( WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True ): print(vocable_word_attribute_text, "is in word list") attribute_text = vocable.find(self.attribute_name).text # does this vocable have the added attribute value? regex = "\s*" + self.attribute_value + "\s*$" print("regex:", regex) print("attribute_text:", attribute_text) assert re.search(regex, attribute_text) is not None, ( "attribute value has not been added to vocable " + vocable.find(self.words_attribute_name).text )
def test_add_values_to_attribute_of_vocables (self): xml_parser = XMLParser() list_of_words = ['你','好','吗','我','很'] test_word_list_file_path = self.create_test_words_file(list_of_words) test_vocable_file_path = self.create_test_vocables_file() xsd_file_path = self.project_directory + 'res/xld-vocables-schema.xsd' # TODO: check if the chapter is not in the vocables already # attribute value hasn't been added yet, so it should not already be in the vocables # this assertion is necessary to check if the add_values_to_attribute_of_vocables really changes something xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path) for vocable in xml_root: vocable_word_attribute_text = vocable.find(self.words_attribute_name).text # is this vocable one of the world list? if WordListHelper.is_in_list(WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True): print(vocable_word_attribute_text, 'is in word list') attribute_text = vocable.find(self.attribute_name).text # does this vocable have the added attribute value? regex = '\s*' + self.attribute_value + '\s*$' print('regex:', regex) print('attribute_text:', attribute_text) assert re.search(regex, attribute_text) is None, 'attribute value has already added to vocable ' + vocable.find(self.words_attribute_name).text # now add the attribute value xld_attribute_adder = XLDAttributeAdder(test_vocable_file_path, xsd_file_path, test_word_list_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables(self.attribute_name, self.attribute_value, self.words_attribute_name) VocableFileWriter.write(VocableFileWriter, xsd_file_path, test_vocable_file_path, xld_attribute_adder.xml_root) # check the vocables again, to see if something has been added xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path) for vocable in xml_root: vocable_word_attribute_text = vocable.find(self.words_attribute_name).text # is this vocable one of the world list? if WordListHelper.is_in_list(WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True): print(vocable_word_attribute_text, 'is in word list') attribute_text = vocable.find(self.attribute_name).text # does this vocable have the added attribute value? regex = '\s*' + self.attribute_value + '\s*$' print('regex:', regex) print('attribute_text:', attribute_text) assert re.search(regex, attribute_text) is not None, 'attribute value has not been added to vocable ' + vocable.find(self.words_attribute_name).text
def test_write_vocable_file_failing (self): print('current working directory:', os.getcwd()) vocable_file_path = self.create_test_vocables_file() print('--- using vocable file path:', vocable_file_path) list_of_words = ['你','好','吗','我','很'] words_list_file_path = self.create_test_words_file(list_of_words) xsd_file_path = self.project_directory + 'res/xld-vocables-schema.xsd' attribute_name = 'chapters' attribute_value = 'HSK1-2012' words_attribute_name = 'secondLanguageTranslations' xml_parser = XMLParser() xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path) xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name) # adding invalid tags with texts invalid_vocable_element = etree.SubElement(xml_root, 'vocable') abc_element = etree.SubElement(invalid_vocable_element, 'abc') abc_element.text = 'abc' unknowntag_element = etree.SubElement(invalid_vocable_element, 'unknowntag') unknowntag_element.text = 'unknowntag' # the tree should be invalid at this point assert xml_parser.validate_tree(xsd_file_path, xml_root) == False, 'Your validation does not work properly.' # if the xml is not valid anymore when reading the file, the write function might have done something wrong try: VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root) except XMLInvalidException as exception: assert True else: assert False assert os.path.isfile(vocable_file_path), 'no file exists' xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
def test_write_vocable_file_failing(self): print("current working directory:", os.getcwd()) vocable_file_path = self.create_test_vocables_file() print("--- using vocable file path:", vocable_file_path) list_of_words = ["你", "好", "吗", "我", "很"] words_list_file_path = self.create_test_words_file(list_of_words) xsd_file_path = self.project_directory + "res/xld-vocables-schema.xsd" attribute_name = "chapters" attribute_value = "HSK1-2012" words_attribute_name = "secondLanguageTranslations" xml_parser = XMLParser() xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path) xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name) # adding invalid tags with texts invalid_vocable_element = etree.SubElement(xml_root, "vocable") abc_element = etree.SubElement(invalid_vocable_element, "abc") abc_element.text = "abc" unknowntag_element = etree.SubElement(invalid_vocable_element, "unknowntag") unknowntag_element.text = "unknowntag" # the tree should be invalid at this point assert xml_parser.validate_tree(xsd_file_path, xml_root) == False, "Your validation does not work properly." # if the xml is not valid anymore when reading the file, the write function might have done something wrong try: VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root) except XMLInvalidException as exception: assert True else: assert False assert os.path.isfile(vocable_file_path), "no file exists" xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
def main(): vocable_file_path = input('Vocable file path:') xsd_file_path = input('XSD file path:') words_file_path = input('Word list file path:') attribute_name = input('Attribute name:') words_attribute_name = input('Words attribute name:') attribute_value = input('Attribute value:') if vocable_file_path == '': vocable_file_path = '/home/xiaolong/Development/PycharmProjects/xld-attribute-adder/res/vocables.xml' if xsd_file_path == '': xsd_file_path = '/home/xiaolong/Development/PycharmProjects/xld-attribute-adder/res/xld-vocables-schema.xsd' if words_file_path == '': words_file_path = '/home/xiaolong/Development/PycharmProjects/xld-attribute-adder/res/HSK2' if attribute_name == '': attribute_name = 'chapters' if words_attribute_name == '': words_attribute_name = 'secondLanguageTranslations' if attribute_value == '': attribute_value = 'HSK2-2012' print('inputs received:\n') print('vocable file path: ', vocable_file_path, sep=' ', end='\n') print('xsd file path: ', xsd_file_path, sep=' ', end='\n') print('words file path: ', words_file_path, sep=' ', end='\n') print('attribute name: ', attribute_name, sep=' ', end='\n') print('words attribute name: ', words_attribute_name, sep=' ', end='\n') print('attribute_value: ', attribute_value, sep=' ', end='\n') xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_file_path) xld_attribute_adder.add_values_to_attribute_of_vocables( attribute_name, attribute_value, words_attribute_name) VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xld_attribute_adder.xml_root)