def excel(load=None, save=None, logfile=None): """Interact with the excel module to utilize spreadsheets.""" if logfile: if os.path.exists(logfile): print('WARNING: The file specified by logfile \'{0}\' already ' 'exists. Would you like to overwrite it?' .format(logfile)) while True: choice = input('Y/n: ').upper() if choice == 'Y' or choice == 'YES' or choice == '': break elif choice == 'N' or choice == 'NO': print('Action cancelled due to invalid logfile.') sys.exit(0) else: print('Invalid input, please answer \'Y\' if you want to ' 'overwrite the file, or \'N\' if you do not. Would' 'you like to overwite the file \'{0}\'?' .format(logfile)) stream = open(logfile, 'w', encoding='utf-8') else: stream = sys.stdout if load and save: raise ValueError('Cannot load and save at the same time!') if load: if os.path.exists(load): swb = SeedsWorkbook() swb.load(load) else: raise FileNotFoundError('The file \'{0}\' does not exist!' .format(load)) swb.save_all_sheets_to_db(stream=stream) if save: if os.path.exists(save): print('WARNING: The file {0} exists. Would you like to overwrite ' 'it?'.format(save)) while True: choice = input('Y/n: ').upper() if choice == 'Y' or choice == 'YES' or choice == '': break elif choice == 'N' or choice == 'NO': print('Save cancelled. Exiting.') sys.exit(0) else: print('Invalid input, please answer \'Y\' if you want to ' 'overwrite the file, or \'N\' if you do not. Would' 'you like to overwite the file \'{0}\'?' .format(save)) swb = SeedsWorkbook() print('*** BEGIN saving all data to worksheet \'{0}\'. ***' .format(save), file=stream) swb.add_all_data_to_sheets(stream=stream) swb.beautify_all_sheets() swb.save(save) print('*** END saving all data to worksheet \'{0}\'. ***' .format(save), file=stream) if stream is not sys.stdout: stream.close()
def test_save_all_sheets_to_db(self, m_pkt, m_cv, m_sr, m_bn, m_cn, m_idx): """Call save_to_db for each worksheet.""" messages = StringIO() swb = SeedsWorkbook() swb.save_all_sheets_to_db(stream=messages) messages.seek(0) msgs = messages.read() m_idx.assert_called_with(stream=messages) m_cn.assert_called_with(stream=messages) m_bn.assert_called_with(stream=messages) m_cv.assert_called_with(stream=messages) m_sr.assert_called_with(stream=messages) m_pkt.assert_called_with(stream=messages) assert '-- BEGIN saving all worksheets to database. --' in msgs assert '-- END saving all worksheets to database. --' in msgs