def runner(): Log.logger.debug('Start application.... ') try: args = parser.parse_args() f_path, f_out, mode = (args.file, args.out, args.mode) except: Log.logger.error('Missing required parameters') Log.logger.debug('Stop application....') exit() if mode != 'lines' and mode != 'words': Log.logger.error('Invalid mode!') Log.logger.debug('Stop application....') exit() if not os.path.exists(f_path): Log.logger.error('File not found!') Log.logger.debug('Stop application....') exit() elif f_path == f_out: Log.logger.error('File must be different!') Log.logger.debug('Stop application....') exit() elif os.path.exists(f_out): Log.logger.debug('Output file {} exists'.format(f_out)) Log.logger.debug('Waiting for user confirmation...') choise = input( 'Data will be added to the existing file. Conitnue [y/n] ?\n') if choise.lower() == 'y': Log.logger.debug('Data will be added to the existing file') pass elif choise.lower() == 'n': Log.logger.debug( 'Negative answer is selected. The application will be closed') Log.logger.debug('Stop application....') exit() elif choise.lower() != 'n' and choise.lower() != 'y': Log.logger.error('Invalid choise') Log.logger.debug('Stop application....') exit() if mode == 'lines': Log.logger.debug( 'User choise: find lines in {} and put them in {}'.format( f_path, f_out)) obj = Analyzer(args.file, args.out) obj.lines_processing() Log.logger.debug('Stop application....') elif mode == 'words': Log.logger.debug( 'User choise: find unique words in {} and put them in {}'.format( f_path, f_out)) obj = Analyzer(args.file, args.out) obj.words_processing() Log.logger.debug('Stop application....')
class LineProcessingTest(unittest.TestCase): def setUp(self): self.textfile = 'in' self.emptyfile = 'empty' self.file_out = 'out' self.data = 'qwerty \n !,.?:QweRty;[] \n q1W34erT78y \n soFTeq \n 9Sof \n teq9 sofTEQ' with open(self.textfile, 'w') as f: f.write(self.data) with open(self.emptyfile, 'w') as f: f.write('') def tearDown(self): os.unlink(self.textfile) os.unlink(self.emptyfile) try: os.unlink(self.file_out) except FileNotFoundError: pass def test_FileCreateToWriteLines(self): self.tmp = Analyzer(self.textfile, self.file_out) assert os.path.exists(self.file_out) is False self.tmp.lines_processing() assert os.path.exists(self.file_out) is True def test_FileOpenToWriteLines(self): self.tmp = Analyzer(self.textfile, self.emptyfile) assert os.path.exists(self.emptyfile) is True self.tmp.lines_processing() assert os.path.exists(self.emptyfile) is True def test_WriteValidDataToEmptyFile(self): self.tmp = Analyzer(self.textfile, self.emptyfile).lines_processing() self.lines_in_data = len(self.data.split('\n')) with open(self.emptyfile) as f: self.lines_in_file = sum(1 for line in f) self.assertEqual(self.lines_in_data, self.lines_in_file) def test_WriteValidDataToNonEmptyFile(self): assert os.path.exists(self.file_out) is False self.tmp = Analyzer(self.textfile, self.file_out).lines_processing() assert os.path.exists(self.file_out) is True self.tmp = Analyzer(self.textfile, self.file_out).lines_processing() assert os.path.exists(self.file_out) is True with open(self.file_out) as f: self.lines_in_file = sum(1 for line in f) self.lines_in_data = len((self.data+self.data).split('\n')) self.assertEqual(self.lines_in_data, self.lines_in_file)