def input_txt_file(self):
        '''Imports text from file'''

        while True:
            # present availabe options to user
            print('\n' + tabulate(self.input_file_options('txt'), tablefmt='fancy_grid'))
            choice = input('\nInsert a number of option that you want to use: ')
            try:
                if choice == '1':
                    input_file = f"txt_files\{self.display_files('txt')}"
                    ciphered_text = read_txt_file(input_file)
                    return ciphered_text
                elif choice == '2':
                    print(self._return_message)
                    input_file = input('\nWrite file path with extension .txt to insert into Enigma: ')
                    #
                    if input_file == 'y':
                        # return to main menu
                        break
                    ciphered_text = read_txt_file(input_file)
                    return ciphered_text
                else:
                    print(self._option)
            except (FileNotFound, ValueError) as Message:
                print(f'{Message}. {self._insert_file_path}')
            except WrongNumberOfLines as Message:
                print(f'{Message}. {self._insert_file_path}')
            except NoTextToProcess as Message:
                print(f'{Message}. Please, insert file with message')
            except NoAsciiDetected as Message:
                print(f'{Message}. {self._insert_file_path}')
            except ValueError:
                print(f'{EnumerateValueError}.Please choose option again')
Example #2
0
    def run_program(self):
        '''
        Main program.
        '''
        # Message to print after program processed data
        text_to_print = ""
        variables_collected = False

        # if user imported .json file with settings, other settings that
        # were inserted manually will not be taken into account
        if not self.jsonBrowseFileName:
            try:
                list_of_rotors, steckerbrett, reflector = self.get_settings_from_boxes(
                )
                variables_collected = True
            except (SteckerbrettRepeatedValues,
                    SteckerbrettWrongFormat) as Message:
                self.print_messages(Message)

        # If inserted path to .json file is not empty:
        if self.jsonBrowseFileName:
            text_to_print += 'Due to the settings being imported, settings inserted manually are not considered<br>Exporting settings is disabled<hr>'

            # if json was imported, disable exporting
            self.export_button_json.setEnabled(False)

            try:
                list_of_rotors, steckerbrett, reflector = read_json_file(
                    self.jsonBrowseFileName)
                variables_collected = True
            except FileNotFound as Message:
                self.print_messages(Message)

        if variables_collected:
            # If inserted path to .txt file is not empty:
            try:
                ciphered_text = read_txt_file(self.txtBrowseFileName)

                # get processed text
                self.processed_text = self.process_data(
                    list_of_rotors, steckerbrett, reflector, ciphered_text)

                text_to_print += f'Processed text: <p style="color:#2D2"><b>{self.processed_text}</b></p> If you wish to export processed data, use buttons below<br>'
                self.print_messages(text_to_print)

                # Enable EXPORT txt button
                self.export_button_txt.setEnabled(True)

                # export buttons are located in the __init__
                # because I observed multiple clicks when calling
                # button.click.connect function from here
                # it does not affect buttons being disabled
            except (FileNotFoundError, FileNotFound, WrongNumberOfLines,
                    NoTextToProcess, NoAsciiDetected,
                    SteckerbrettRepeatedValues, SteckerbrettValueError,
                    ReflectorValueIsUndefined, InvalidRotorValues,
                    NoReflectorSelected, InvalidRotorQuantity) as Message:
                self.export_button_txt.setEnabled(False)
                self.export_button_json.setEnabled(False)
                self.print_messages(Message)
Example #3
0
def test_read_txt_file_not_found_extension_missing():
    path = 'text2021'
    with pytest.raises(FileNotFound):
        read_txt_file(path)
Example #4
0
def test_read_txt_file_not_found_no_extension():
    path = 'text2021.'
    with pytest.raises(FileNotFound):
        read_txt_file(path)
Example #5
0
def test_read_txt_file_not_found():
    path = 'text2021.txt'
    with pytest.raises(FileNotFound):
        read_txt_file(path)