def main(): print(TITLE) csv_files = [file for file in ls() if file.endswith(output_extension) ] # Load all csv file names from the Microbit if not csv_files: print(" No '{}' files to copy!".format(output_extension)) return 0 name = input(" Enter target name: ") name = name.capitalize() if name else "Unnamed" # Create sub-directory in RAW_Data directory dir_name = os.path.join( "RAW_Data", "{} {}".format(name, datetime.now().strftime("%d %m %Y %H-%M"))) os.makedirs(dir_name, exist_ok=True) # Make the sub-directory if it doesn't exist print() for i, file in enumerate(csv_files): print(" Progress: {:<50} ({}/{})".format( "█" * int(50 * (i + 1) / len(csv_files)), i + 1, len(csv_files)), end="\r") f_name = "{}{}{}".format( name, i, output_extension) # Prepare file name at destination directory get(file, os.path.join( dir_name, f_name)) # Copy file from Microbit to given directory as f_name time.sleep(1) rm(file) time.sleep(1) # Remove file from Microbit print("\n\n {} files moved to '{}'".format(i + 1, dir_name)) return 0
def test_get_with_error(): """ Ensure an IOError is raised if stderr returns something. """ with mock.patch('microfs.execute', return_value=(b'', b'error')): with pytest.raises(IOError) as ex: microfs.get('foo.txt') assert ex.value.args[0] == 'error'
def test_get_with_error(): """ Ensure an IOError is raised if stderr returns something. """ mock_serial = mock.MagicMock() with mock.patch("microfs.execute", return_value=(b"", b"error")): with pytest.raises(IOError) as ex: microfs.get("foo.txt", mock_serial) assert ex.value.args[0] == "error"
def test_get_no_target(): """ Ensure a successful get results in the expected file getting written on the local file system with the expected content. In this case, since no target is provided, use the name of the remote file. """ commands = [ "\n".join([ "try:", " from microbit import uart as u", "except ImportError:", " try:", " from machine import UART", " u = UART(0, {})".format(microfs.SERIAL_BAUD_RATE), " except Exception:", " try:", " from sys import stdout as u", " except Exception:", " raise Exception('Could not find UART module in device.')" ]), "f = open('{}', 'rb')".format('hello.txt'), "r = f.read", "result = True", "while result:\n result = r(32)\n if result:\n u.write(result)\n", "f.close()", ] with mock.patch('microfs.execute', return_value=(b'hello', b'')) as exe: mo = mock.mock_open() with mock.patch('microfs.open', mo, create=True): assert microfs.get('hello.txt') exe.assert_called_once_with(commands, None) mo.assert_called_once_with('hello.txt', 'wb') handle = mo() handle.write.assert_called_once_with(b'hello')
def get_and_create(): fichier = 'data.csv' get(fichier, target=None, serial=None) input_excel = os.path.join(settings.MEDIA_ROOT, 'data.csv') f = open(input_excel, 'r') nLine = 2 lines = f.readlines()[nLine-1:] del lines[0] if Report.objects.count() == 0: id_max = 0 else: id_max = Report.objects.latest('id').id for line in lines: array = line.split(',') ligne = Temperature() ligne.report_id = id_max + 1 ligne.temperature = int(array[1]) index = line.index(",") + 6 millis = line[index:] ligne.registered_date = dt.datetime.now(timezone.utc) + dt.timedelta(milliseconds = -float(millis)) ligne.save() f.close()
def test_get(): """ Ensure a successful get results in the expected file getting written on the local file system with the expected content. """ with mock.patch('microfs.execute', return_value=(b'hello', b'')) as exe: mo = mock.mock_open() with mock.patch('microfs.open', mo, create=True): assert microfs.get('hello.txt') command = "with open('hello.txt') as f:\n print(f.read())" exe.assert_called_once_with(command) mo.assert_called_once_with('hello.txt', 'w') handle = mo() handle.write.assert_called_once_with('hello')
def test_get(): """ Ensure a successful get results in the expected file getting written on the local file system with the expected content. """ mock_serial = mock.MagicMock() commands = [ "\n".join([ "try:", " from microbit import uart as u", "except ImportError:", " try:", " from machine import UART", " u = UART(0, {})".format(microfs.SERIAL_BAUD_RATE), " except Exception:", " try:", " from sys import stdout as u", " except Exception:", " raise Exception('Could not find UART module in device.')", ]), "f = open('{}', 'rb')".format("hello.txt"), "r = f.read", "result = True", "\n".join([ "while result:", " result = r(32)", " if result:", " u.write(repr(result))", ]), "f.close()", ] with mock.patch("microfs.execute", return_value=(b"b'hello'", b"")) as exe: mo = mock.mock_open() with mock.patch("microfs.open", mo, create=True): assert microfs.get("hello.txt", "local.txt", mock_serial) exe.assert_called_once_with(commands, mock_serial) mo.assert_called_once_with("local.txt", "wb") handle = mo() handle.write.assert_called_once_with(b"hello")
def test_get_no_target(): """ Ensure a successful get results in the expected file getting written on the local file system with the expected content. In this case, since no target is provided, use the name of the remote file. """ commands = [ "from microbit import uart", "f = open('{}', 'rb')".format('hello.txt'), "r = f.read", "result = True", "while result:\n result = r(32)\n if result:\n uart.write(result)\n", "f.close()", ] with mock.patch('microfs.execute', return_value=(b'hello', b'')) as exe: mo = mock.mock_open() with mock.patch('microfs.open', mo, create=True): assert microfs.get('hello.txt') exe.assert_called_once_with(commands, None) mo.assert_called_once_with('hello.txt', 'wb') handle = mo() handle.write.assert_called_once_with(b'hello')
def test_get(): """ Ensure a successful get results in the expected file getting written on the local file system with the expected content. """ mock_serial = mock.MagicMock() commands = [ "from microbit import uart", "f = open('{}', 'rb')".format('hello.txt'), "r = f.read", "result = True", "while result:\n result = r(32)\n if result:\n uart.write(result)\n", "f.close()", ] with mock.patch('microfs.execute', return_value=(b'hello', b'')) as exe: mo = mock.mock_open() with mock.patch('microfs.open', mo, create=True): assert microfs.get('hello.txt', 'local.txt', mock_serial) exe.assert_called_once_with(commands, mock_serial) mo.assert_called_once_with('local.txt', 'wb') handle = mo() handle.write.assert_called_once_with(b'hello')
def download(*args, **kwargs): dryrun = kwargs['dryrun'] or False for f in microfs.ls(): logging.info('Downloading file %s', f) if not dryrun: microfs.get(f)