def get_attribute_value_list_from_string(cls, string): attribute_value_separator = AppSettings.get_setting_by_name(AppSettings.ATTRIBUTE_VALUE_SEPARATOR_SETTING_NAME) stripped_characters = AppSettings.get_setting_by_name(AppSettings.STRIPPED_CHARACTERS_SETTING_NAME) value_list = string.split(sep=attribute_value_separator, maxsplit=-1) # stripped_value_list = [value.strip(stripped_characters) for value in value_list] # return stripped_value_list return value_list
def load_vocables(cls): xml_file_path = AppSettings.get_setting_by_name( AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) xsd_file_path = AppSettings.get_setting_by_name( AppSettings.XSD_VOCABLE_FILE_PATH_SETTING_NAME) VocableManager.vocables = FileManager.load_vocables( xml_file_path, xsd_file_path) VocableManager.search_result = VocableManager.vocables
def get_attribute_value_list_from_string(cls, string): attribute_value_separator = AppSettings.get_setting_by_name( AppSettings.ATTRIBUTE_VALUE_SEPARATOR_SETTING_NAME) stripped_characters = AppSettings.get_setting_by_name( AppSettings.STRIPPED_CHARACTERS_SETTING_NAME) value_list = string.split(sep=attribute_value_separator, maxsplit=-1) # stripped_value_list = [value.strip(stripped_characters) for value in value_list] # return stripped_value_list return value_list
def exit_application(self, widget, event): strtrue = str(True) save_vocables = AppSettings.get_setting_by_name( AppSettings.SAVE_VOCABLES_ON_EXIT_SETTING_NAME) == strtrue show_dialog = AppSettings.get_setting_by_name( AppSettings.DIALOG_SHOW_SAVE_VOCABLES_CONFIRMATION_SETTING_NAME ) == strtrue if show_dialog and VocableManager.vocables_changed: save_vocables_confirmation_dialog = SaveVocablesBeforeExitConfirmationDialog( self) save_vocables = save_vocables_confirmation_dialog.run( ) == Gtk.ResponseType.YES AppSettings.change_setting_by_name( AppSettings.SAVE_VOCABLES_ON_EXIT_SETTING_NAME, save_vocables) save_vocables_confirmation_dialog.destroy() if save_vocables: VocableManager.save_vocables(VocableManager.vocables) exit_on_exit_confirmation = AppSettings.get_setting_by_name( AppSettings.EXIT_ON_EXIT_SETTING_NAME) == strtrue show_exit_confirmation = AppSettings.get_setting_by_name( AppSettings.DIALOG_SHOW_EXIT_CONFIRMATION_SETTING_NAME) if show_exit_confirmation == strtrue: ExitConfirmationDialog.__init__ = timefunction( ExitConfirmationDialog.__init__) # decoration exit_confirmation_dialog = ExitConfirmationDialog(self) exit_confirmation_dialog.run = timefunction( exit_confirmation_dialog.run) # decoration exit_on_exit_confirmation = exit_confirmation_dialog.run( ) == Gtk.ResponseType.YES AppSettings.change_setting_by_name( AppSettings.EXIT_ON_EXIT_SETTING_NAME, exit_on_exit_confirmation) exit_confirmation_dialog.destroy() if exit_on_exit_confirmation: # print("Clicked YES") AppSettings.save_settings() Gtk.main_quit() sys.exit() else: # print("Clicked NO") pass return GTKSignal.DO_NOT_PROPAGATE
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 create_sortable_vocable_tree_view_model(self): vocable_tree_view_model = Gtk.ListStore( str, str, str, str, str) # index, fl, flps, slps, sl displayed_vocable_attributes_list = [] delimiter = AppSettings.get_setting_by_name( AppSettings.ATTRIBUTE_VALUE_SEPARATOR_SETTING_NAME) for vocable in VocableManager.search_result: displayed_vocable_attributes_list.append( (delimiter.join(vocable.first_language_translations), delimiter.join(vocable.first_language_phonetic_scripts), delimiter.join(vocable.second_language_phonetic_scripts), delimiter.join(vocable.second_language_translations))) for list_index, vocable in enumerate( displayed_vocable_attributes_list): string_index = zero_fill( str(list_index), len_of_number(len(displayed_vocable_attributes_list))) row = [string_index] for attribute in vocable: row.append(attribute) vocable_tree_view_model.append(row) # vocable_tree_view_model.append(Vocable('fl','flps','sl','slps','t','c','ll','rl','d')) # This does not work because of number of columns issue # make it sortable sortable_vocable_tree_view_model = Gtk.TreeModelSort( model=vocable_tree_view_model) return sortable_vocable_tree_view_model
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 test_xml_file_invariance(self): # get file content xml_file_content = None xml_file_path = AppSettings.get_setting_by_name(AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) with open(xml_file_path, mode='rb') as vocable_file: xml_file_content = vocable_file.read() # check if file content is still the same new_xml_file_content = None xml_file_path = AppSettings.get_setting_by_name(AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) with open(xml_file_path, mode='rb') as vocable_file: new_xml_file_content = vocable_file.read() assert xml_file_content == new_xml_file_content, \ 'Loading and saving the vocables leads to changes in the XML vocable file, ' \ 'although the vocables didn\'t change. These changes might be whitespace character differences.'
def initialize_widgets(self): self.label = Gtk.Label('There are changes to the vocables of the currently opened dictionary.' '\nDo you wish to save those changes?') self.remember_decision_checkbutton = Gtk.CheckButton('Remember my decision.') self.remember_decision_checkbutton.set_active( not bool(AppSettings.get_setting_by_name(AppSettings.DIALOG_SHOW_SAVE_VOCABLES_CONFIRMATION_SETTING_NAME)) )
def initialize_widgets(self): self.label = Gtk.Label( 'Do you really want to exit Xiaolong Dictionary?') self.remember_decision_checkbutton = Gtk.CheckButton( 'Remember my decision.') self.remember_decision_checkbutton.set_active(not bool( AppSettings.get_setting_by_name( AppSettings.DIALOG_SHOW_EXIT_CONFIRMATION_SETTING_NAME)))
def test_xml_file_invariance(self): # get file content xml_file_content = None xml_file_path = AppSettings.get_setting_by_name( AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) with open(xml_file_path, mode='rb') as vocable_file: xml_file_content = vocable_file.read() # check if file content is still the same new_xml_file_content = None xml_file_path = AppSettings.get_setting_by_name( AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) with open(xml_file_path, mode='rb') as vocable_file: new_xml_file_content = vocable_file.read() assert xml_file_content == new_xml_file_content, \ 'Loading and saving the vocables leads to changes in the XML vocable file, ' \ 'although the vocables didn\'t change. These changes might be whitespace character differences.'
def test_change_setting_by_name(self): """ To test the method AppSettings.change_setting_by_name, we have to assert the following: 1.Trying to change a setting, which does not exist should throw a SettingUnknownException. 2.Trying to change a setting, which does exist, should result in the setting having the changed value. """ # make sure we have a string, which is not a key in the dictionary random_word_length = 10 random_word = RandomStringCreator.randomword(random_word_length) while random_word in AppSettings.settings: random_word_length += 1 random_word = RandomStringCreator.randomword(random_word_length) # check for 1. (Does the method throw the correct exception?) try: AppSettings.change_setting_by_name(random_word, 'abc') assert False, 'Could change a not existing setting.' except SettingUnknownException: assert random_word not in AppSettings.settings, \ 'The method AppSettings.change_setting_by_name falsely throws exception SettingUnknownException.' # check for 2. (Does the method set the correct value for the setting?) if len(AppSettings.settings.keys()) == 0: test_setting_name = 'test_setting_name' test_setting_value = 'test_setting_value' new_value = test_setting_value + '_1' AppSettings.add_setting_by_name(test_setting_name, test_setting_value) AppSettings.change_setting_by_name(test_setting_name, new_value) assert AppSettings.get_setting_by_name(test_setting_name) == new_value, \ 'The method AppSettings.change_setting_by_name not set the correct value.' else: existing_dictionary_key = list(AppSettings.settings.keys())[0] try: old_value = AppSettings.get_setting_by_name( existing_dictionary_key) new_value = old_value + '_1' AppSettings.change_setting_by_name(existing_dictionary_key, new_value) assert AppSettings.get_setting_by_name(existing_dictionary_key) == new_value, \ 'The method AppSettings.change_setting_by_name does change to the correct value.' except SettingUnknownException: assert False, 'AppSettings.change_setting_by_name a SettingUnknownException, ' \ 'although the accessed key existed in the settings dictionary.'
def test_change_setting_by_name(self): """ To test the method AppSettings.change_setting_by_name, we have to assert the following: 1.Trying to change a setting, which does not exist should throw a SettingUnknownException. 2.Trying to change a setting, which does exist, should result in the setting having the changed value. """ # make sure we have a string, which is not a key in the dictionary random_word_length = 10 random_word = RandomStringCreator.randomword(random_word_length) while random_word in AppSettings.settings: random_word_length += 1 random_word = RandomStringCreator.randomword(random_word_length) # check for 1. (Does the method throw the correct exception?) try: AppSettings.change_setting_by_name(random_word, 'abc') assert False, 'Could change a not existing setting.' except SettingUnknownException: assert random_word not in AppSettings.settings, \ 'The method AppSettings.change_setting_by_name falsely throws exception SettingUnknownException.' # check for 2. (Does the method set the correct value for the setting?) if len(AppSettings.settings.keys()) == 0: test_setting_name = 'test_setting_name' test_setting_value = 'test_setting_value' new_value = test_setting_value + '_1' AppSettings.add_setting_by_name(test_setting_name, test_setting_value) AppSettings.change_setting_by_name(test_setting_name, new_value) assert AppSettings.get_setting_by_name(test_setting_name) == new_value, \ 'The method AppSettings.change_setting_by_name not set the correct value.' else: existing_dictionary_key = list(AppSettings.settings.keys())[0] try: old_value = AppSettings.get_setting_by_name(existing_dictionary_key) new_value = old_value + '_1' AppSettings.change_setting_by_name(existing_dictionary_key, new_value) assert AppSettings.get_setting_by_name(existing_dictionary_key) == new_value, \ 'The method AppSettings.change_setting_by_name does change to the correct value.' except SettingUnknownException: assert False, 'AppSettings.change_setting_by_name a SettingUnknownException, ' \ 'although the accessed key existed in the settings dictionary.'
def test_get_setting_by_name(self): """ To test the get_setting_by_name method, we have to assert the following: 1. trying to access a setting, which does not exist raises the appropriate exception 2. trying to access a setting, which does exist, will return the correct settings value To assert 2., we can set a settings value using the change_setting_by_name or add_setting_by_name. """ # make sure we have a string, which is not a key in the dictionary random_word_length = 10 random_word = RandomStringCreator.randomword(random_word_length) while random_word in AppSettings.settings: random_word_length += 1 random_word = RandomStringCreator.randomword(random_word_length) # check for 1. (does it throw the correct exception?) try: AppSettings.get_setting_by_name(random_word) assert False, 'Could get a value for a not existing setting.' except SettingUnknownException: assert random_word not in AppSettings.settings, \ 'The method AppSettings.get_setting_by_name falsely throws exception SettingUnknownException.' # check for 2. if len(AppSettings.settings.keys()) == 0: test_setting_name = 'test_setting_name' test_setting_value = 'test_setting_value' AppSettings.add_setting_by_name(test_setting_name, test_setting_value) assert AppSettings.get_setting_by_name(test_setting_name) == test_setting_value, \ 'The method AppSettings.get_setting_by_name does not return the correct value.' else: existing_dictionary_key = list(AppSettings.settings.keys())[0] try: returned_value = AppSettings.get_setting_by_name( existing_dictionary_key) expected_value = AppSettings.settings[existing_dictionary_key] assert returned_value == expected_value, \ 'The method AppSettings.get_setting_by_name does not return the correct value.' except SettingUnknownException: assert False, 'Exception raised although the accessed key existed in the settings dictionary.'
def test_get_setting_by_name(self): """ To test the get_setting_by_name method, we have to assert the following: 1. trying to access a setting, which does not exist raises the appropriate exception 2. trying to access a setting, which does exist, will return the correct settings value To assert 2., we can set a settings value using the change_setting_by_name or add_setting_by_name. """ # make sure we have a string, which is not a key in the dictionary random_word_length = 10 random_word = RandomStringCreator.randomword(random_word_length) while random_word in AppSettings.settings: random_word_length += 1 random_word = RandomStringCreator.randomword(random_word_length) # check for 1. (does it throw the correct exception?) try: AppSettings.get_setting_by_name(random_word) assert False, 'Could get a value for a not existing setting.' except SettingUnknownException: assert random_word not in AppSettings.settings, \ 'The method AppSettings.get_setting_by_name falsely throws exception SettingUnknownException.' # check for 2. if len(AppSettings.settings.keys()) == 0: test_setting_name = 'test_setting_name' test_setting_value = 'test_setting_value' AppSettings.add_setting_by_name(test_setting_name, test_setting_value) assert AppSettings.get_setting_by_name(test_setting_name) == test_setting_value, \ 'The method AppSettings.get_setting_by_name does not return the correct value.' else: existing_dictionary_key = list(AppSettings.settings.keys())[0] try: returned_value = AppSettings.get_setting_by_name(existing_dictionary_key) expected_value = AppSettings.settings[existing_dictionary_key] assert returned_value == expected_value, \ 'The method AppSettings.get_setting_by_name does not return the correct value.' except SettingUnknownException: assert False, 'Exception raised although the accessed key existed in the settings dictionary.'
def exit_application(self, widget, event): strtrue = str(True) save_vocables = AppSettings.get_setting_by_name(AppSettings.SAVE_VOCABLES_ON_EXIT_SETTING_NAME) == strtrue show_dialog = AppSettings.get_setting_by_name( AppSettings.DIALOG_SHOW_SAVE_VOCABLES_CONFIRMATION_SETTING_NAME ) == strtrue if show_dialog and VocableManager.vocables_changed: save_vocables_confirmation_dialog = SaveVocablesBeforeExitConfirmationDialog(self) save_vocables = save_vocables_confirmation_dialog.run() == Gtk.ResponseType.YES AppSettings.change_setting_by_name(AppSettings.SAVE_VOCABLES_ON_EXIT_SETTING_NAME, save_vocables) save_vocables_confirmation_dialog.destroy() if save_vocables: VocableManager.save_vocables(VocableManager.vocables) exit_on_exit_confirmation = AppSettings.get_setting_by_name(AppSettings.EXIT_ON_EXIT_SETTING_NAME) == strtrue show_exit_confirmation = AppSettings.get_setting_by_name(AppSettings.DIALOG_SHOW_EXIT_CONFIRMATION_SETTING_NAME) if show_exit_confirmation == strtrue: ExitConfirmationDialog.__init__ = timefunction(ExitConfirmationDialog.__init__) # decoration exit_confirmation_dialog = ExitConfirmationDialog(self) exit_confirmation_dialog.run = timefunction(exit_confirmation_dialog.run) # decoration exit_on_exit_confirmation = exit_confirmation_dialog.run() == Gtk.ResponseType.YES AppSettings.change_setting_by_name(AppSettings.EXIT_ON_EXIT_SETTING_NAME, exit_on_exit_confirmation) exit_confirmation_dialog.destroy() if exit_on_exit_confirmation: # print("Clicked YES") AppSettings.save_settings() Gtk.main_quit() sys.exit() else: # print("Clicked NO") pass return GTKSignal.DO_NOT_PROPAGATE
def __init__(self, model=None): super().__init__(model=model) for index, column_title in enumerate(self.columns_titles): # create Gtk.TreeViewColumns self.columns.append(Gtk.TreeViewColumn(title=column_title)) # add Gtk.TreeViewColumns to the TreeView self.append_column(self.columns[-1]) # create Gtk.CellRendererTexts self.cell_renderers.append(Gtk.CellRendererText()) # style the CellRendererTexts self.cell_renderers[-1].set_alignment(xalign=0.0, yalign=0.0) # ??? self.columns[-1].pack_start(self.cell_renderers[-1], self.column_expand[index]) self.columns[-1].add_attribute( cell_renderer=self.cell_renderers[-1], attribute='text', column=index) self.set_search_column(index) self.columns[-1].set_sort_column_id(index) self.columns[-1].set_reorderable(True) self.columns[-1].set_resizable(True) # setting the width for all columns except the index column to 100 initial_width = -1 try: initial_width = AppSettings.get_setting_by_name( AppSettings.INITIAL_TREEVIEW_COLUMN_WIDTH) initial_width = int(initial_width, base=10) except SettingUnknownException: print('ERROR: SETTING NOT FOUND!') initial_width = 100 except ValueError: print('Could not convert string to integer value.') initial_width = 100 for column_index in range(4): self.columns[column_index + 1].set_fixed_width(initial_width) self.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE) self.connect_signal_handlers() self.connect_selection_signal_handlers() # popup menu on right click self.popup_menu_ui = PopupMenuProvider.get_vocable_treeview_popup_menu( )
def __init__(self, model=None): super().__init__(model=model) for index, column_title in enumerate(self.columns_titles): # create Gtk.TreeViewColumns self.columns.append(Gtk.TreeViewColumn(title=column_title)) # add Gtk.TreeViewColumns to the TreeView self.append_column(self.columns[-1]) # create Gtk.CellRendererTexts self.cell_renderers.append(Gtk.CellRendererText()) # style the CellRendererTexts self.cell_renderers[-1].set_alignment(xalign=0.0, yalign=0.0) # ??? self.columns[-1].pack_start(self.cell_renderers[-1], self.column_expand[index]) self.columns[-1].add_attribute( cell_renderer=self.cell_renderers[-1], attribute='text', column=index ) self.set_search_column(index) self.columns[-1].set_sort_column_id(index) self.columns[-1].set_reorderable(True) self.columns[-1].set_resizable(True) # setting the width for all columns except the index column to 100 initial_width = -1 try: initial_width = AppSettings.get_setting_by_name(AppSettings.INITIAL_TREEVIEW_COLUMN_WIDTH) initial_width = int(initial_width, base=10) except SettingUnknownException: print('ERROR: SETTING NOT FOUND!') initial_width = 100 except ValueError: print('Could not convert string to integer value.') initial_width = 100 for column_index in range(4): self.columns[column_index+1].set_fixed_width(initial_width) self.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE) self.connect_signal_handlers() self.connect_selection_signal_handlers() # popup menu on right click self.popup_menu_ui = PopupMenuProvider.get_vocable_treeview_popup_menu()
def __init__(self, first_language_translations=[], first_language_phonetic_scripts=[], second_language_translations=[], second_language_phonetic_scripts=[], topics=[], chapters=[], learn_level='0', relevance_level='0', description='---'): self.first_language_translations = first_language_translations self.first_language_phonetic_scripts = first_language_phonetic_scripts self.second_language_translations = second_language_translations self.second_language_phonetic_scripts = second_language_phonetic_scripts self.topics = topics self.chapters = chapters self.learn_level = learn_level self.relevance_level = relevance_level self.description = description Vocable.delimiter = AppSettings.get_setting_by_name( AppSettings.ATTRIBUTE_VALUE_SEPARATOR_SETTING_NAME)
def __init__( self, first_language_translations=[], first_language_phonetic_scripts=[], second_language_translations=[], second_language_phonetic_scripts=[], topics=[], chapters=[], learn_level='0', relevance_level='0', description='---' ): self.first_language_translations = first_language_translations self.first_language_phonetic_scripts = first_language_phonetic_scripts self.second_language_translations = second_language_translations self.second_language_phonetic_scripts = second_language_phonetic_scripts self.topics = topics self.chapters = chapters self.learn_level = learn_level self.relevance_level = relevance_level self.description = description Vocable.delimiter = AppSettings.get_setting_by_name(AppSettings.ATTRIBUTE_VALUE_SEPARATOR_SETTING_NAME)
def save_vocables(cls, vocable_list): xml_file_path = AppSettings.get_setting_by_name( AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) xsd_file_path = AppSettings.get_setting_by_name( AppSettings.XSD_VOCABLE_FILE_PATH_SETTING_NAME) FileManager.save_vocables(vocable_list, xml_file_path, xsd_file_path)
def load_vocables(cls): xml_file_path = AppSettings.get_setting_by_name(AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) xsd_file_path = AppSettings.get_setting_by_name(AppSettings.XSD_VOCABLE_FILE_PATH_SETTING_NAME) VocableManager.vocables = FileManager.load_vocables(xml_file_path, xsd_file_path) VocableManager.search_result = VocableManager.vocables
def save_vocables(cls, vocable_list): xml_file_path = AppSettings.get_setting_by_name(AppSettings.XML_VOCABLE_FILE_PATH_SETTING_NAME) xsd_file_path = AppSettings.get_setting_by_name(AppSettings.XSD_VOCABLE_FILE_PATH_SETTING_NAME) FileManager.save_vocables(vocable_list, xml_file_path, xsd_file_path)