def test_validate_no_output(self): input_path = 'test.txt' output_path = '' expected_msg = 'Output file not specified.' with self.assertRaises(ValidateError, msg=expected_msg): validate(input_path, output_path)
def convert(self): ''' Converts the input file and writes to the output file. ''' input_path = self.input_path.text() # Should be set below when opening input the file. output_path = self.output_path.text() try: input_path, output_path = validate(input_path, output_path) except OutputExistsError as e: # E128 Technically this should be indented more, but I feel that # hurts readability in this case. overwrite = QMessageBox.warning(self, self.tr('JWPCE conversion'), self.tr('The output file already exits. Overwrite it?'), QMessageBox.Yes | QMessageBox.No) # noqa: E128 if overwrite == QMessageBox.No: return except ValidateError as e: QMessageBox.warning(self, self.tr('JWPCE conversion'), self.tr(str(e)), QMessageBox.Ok) return # TODO - add in some kind of progress indicator? contents = read_file(input_path) write_file(output_path, contents) QMessageBox.information(self, self.tr('JWPCE conversion'), self.tr('The conversion is complete.'), QMessageBox.Ok)
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)