def import_settings_from_file(self):
     '''Reading settings from to .json 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_path = f"json_files\{self.display_files('json')}"
                 list_of_rotors, steckerbrett, reflector = read_json_file(input_path)
                 # after settings are imported, initiate program
                 return list_of_rotors, steckerbrett, reflector
             if choice == '2':
                 input_path = input('\nWrite file path with extension .json to insert settings into Enigma: ')
                 if input_path:
                     list_of_rotors, steckerbrett, reflector = read_json_file(input_path)
                     # after settings are imported, initiate program
                     return list_of_rotors, steckerbrett, reflector
                 else:  # if input_file is empty, raise error
                     raise FileNotFound('File was not found')
             else:
                 print(self._option)
         except FileNotFound as Message:
             print(f'{Message}. {self._insert_file_path}')
         except DecodeError as Message:
             print(f'{Message}. Inserted file might contain incorrect data.')
Exemple #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)
Exemple #3
0
def test_read_json_file_wrong_format():
    '''TXT file imported to read as json'''
    path = 'processedText.txt'
    with pytest.raises(DecodeError):
        read_json_file(path)
Exemple #4
0
def test_read_json_file_extension_missing():
    path = 'settings.'
    with pytest.raises(FileNotFound):
        read_json_file(path)
Exemple #5
0
def test_read_json_file_not_found():
    path = 'settings2021.json'
    with pytest.raises(FileNotFound):
        read_json_file(path)