def test_package_number(self): self.default_bytes_builder.package_number = 4660 new_bytes = self.default_bytes_builder.to_bytes() expected_line = self.default_line[0:586] + '0004660' line = mtrlog.MtrLogFormatter().format( mtrreader.MtrDataMessage(new_bytes), self.datetime_read) self.assertEqual(line, expected_line)
def test_splits_minimal(self): self.default_bytes_builder.splits = [(0, 0), (249, 60)] new_bytes = self.default_bytes_builder.to_bytes() expected_line = (self.default_line[0:86] + '000,00000,249,00060,' + self.default_line[106:]) line = mtrlog.MtrLogFormatter().format( mtrreader.MtrDataMessage(new_bytes), self.datetime_read) self.assertEqual(line, expected_line)
def test_mtr_id(self): self.default_bytes_builder.mtr_id = 258 new_bytes = self.default_bytes_builder.to_bytes() expected_line = (self.default_line[0:8] + '"258",' + self.default_line[12:]) line = mtrlog.MtrLogFormatter().format( mtrreader.MtrDataMessage(new_bytes), self.datetime_read) self.assertEqual(line, expected_line)
def test_card_id(self): self.default_bytes_builder.card_id = 66308 new_bytes = self.default_bytes_builder.to_bytes() expected_line = (self.default_line[0:12] + '"066308",' + self.default_line[21:69] + '066308,' + self.default_line[76:]) line = mtrlog.MtrLogFormatter().format( mtrreader.MtrDataMessage(new_bytes), self.datetime_read) self.assertEqual(line, expected_line)
def test_datetime_read(self): # subtract more than a year to get different values for all fields self.default_bytes_builder.datetime_read -= timedelta(days=366 + 32, hours=1, minutes=1, seconds=1) new_bytes = self.default_bytes_builder.to_bytes() new_datetime_read = (self.default_bytes_builder.datetime_read.strftime( '%d.%m.%y %H:%M:%S.000')) expected_line = (self.default_line[0:45] + ('"%s",' % new_datetime_read) + self.default_line[69:]) line = mtrlog.MtrLogFormatter().format( mtrreader.MtrDataMessage(new_bytes), self.datetime_read) self.assertEqual(line, expected_line)
def test_splits_full(self): splits = [] expected_split_string = '' for i in range(0, 50): control_code = i time_at_control = i * 60 splits.append((control_code, time_at_control)) expected_split_string += ('%03d,%05d,' % (control_code, time_at_control)) self.default_bytes_builder.splits = splits new_bytes = self.default_bytes_builder.to_bytes() expected_line = (self.default_line[0:86] + expected_split_string + self.default_line[586:]) line = mtrlog.MtrLogFormatter().format( mtrreader.MtrDataMessage(new_bytes), self.datetime_read) self.assertEqual(line, expected_line)
def test_multiple(self): expected_lines = [] self.default_bytes_builder.package_number = 101 bytes_msg_1 = self.default_bytes_builder.to_bytes() expected_lines.append(self.default_line[0:586] + '0000101') self.default_bytes_builder.package_number = 102 bytes_msg_2 = self.default_bytes_builder.to_bytes() expected_lines.append(self.default_line[0:586] + '0000102') messages = [ mtrreader.MtrDataMessage(bytes_msg_1), mtrreader.MtrDataMessage(bytes_msg_2) ] lines = mtrlog.MtrLogFormatter().format_all(messages, self.datetime_read) self.assertEqual(lines, expected_lines)
def setUp(self): self.datetime_read = datetime.now() self.default_bytes_builder = MtrDataBytesBuilder(mtr_id=1, card_id=1) self.default_line = mtrlog.MtrLogFormatter().format( mtrreader.MtrDataMessage(self.default_bytes_builder.to_bytes()), self.datetime_read)
logger.error(error_message) argparser.error(error_message) except OSError: error_message = ("Could not open Dropbox token file '%s'" % dropbox_token_file) logger.error(error_message) argparser.error(error_message) output_filename = (args.output_file_name.format( datetime.now().strftime('%Y%m%dT%H%M%S'))) mtr_reader.send_spool_all_command() data_messages = mtr_reader.receive() datetime_extracted = datetime.now() log_lines = mtrlog.MtrLogFormatter().format_all(data_messages, datetime_extracted) mtr_log_file_name = write_mtr_log_file(log_lines, output_filename) if destination_args[0] == 'dropbox': try: upload_dir = "" if len(destination_args) >= 3: upload_dir = destination_args[2] if not upload_dir.startswith("/"): upload_dir = "/" + upload_dir upload_mtr_log_file_dropbox(mtr_log_file_name, upload_dir, dropbox_api_token) except Exception: logger.exception("Error when uploading MTR log file to Dropbox") else: try: