def test_generate_output_file_no_extension(self):

        input_path = 'test'
        expected = 'test.csv'

        result = generate_output_file(input_path)

        assert result == expected
    def test_generate_output_file_csv(self):

        input_path = 'test.csv'
        expected = 'test.csv'

        result = generate_output_file(input_path)

        assert result == expected
Exemple #3
0
    def open_input_file_dialog(self):
        ''' Opens and input file dialog and sets the output based on that.

            The output file path will be the same as the input, but renamed
            .csv instead of .txt.
        '''

        dialog = QFileDialog(self)
        # TODO - While testing anyway
        dialog.setDirectory('.')
        dialog.setFileMode(QFileDialog.ExistingFile)
        dialog.setNameFilter('Text files (*.txt)')

        if dialog.exec_():
            input_files = dialog.selectedFiles()
            input_file = input_files[0]
            self.input_path.setText(input_file)

            out_path = generate_output_file(input_file)

            self.output_path.setText(out_path)
Exemple #4
0
def main():
    description = '''
        Converts a file of JWPCE dictionary definitions to an Anki CSV.

        If output is not provided it will be generated from the input file
        name.

        For example "test.txt" will become "test.csv". If the ouptut file
        already exists then it will not be overwritten unless "--force" is
        provided.
    '''

    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('input', help='Text input')
    parser.add_argument('--output', '-o', help='CSV output')
    parser.add_argument('--force', '-f', action='store_true',
                        help='Overwrites output if it already exists')

    args = parser.parse_args()

    input_path = args.input
    output_path = args.output
    force = args.force

    if not output_path:
        output_path = generate_output_file(input_path)

    try:
        input_path, output_path = validate(input_path, output_path)
    except OutputExistsError as e:
        if not force:
            print('Error: ' + str(e))
            return 1
    except ValidateError as e:
        print('Error: ' + str(e))
        return 1

    contents = read_file(input_path)
    write_file(output_path, contents)