Refactoring in progress...

This commit is contained in:
Gino D
2022-12-31 03:04:11 +01:00
parent b7023d57e6
commit 29629aac58

View File

@@ -27,6 +27,7 @@ import sys
import time import time
import json import json
import serial import serial
import signal
import binascii import binascii
import textwrap import textwrap
@@ -147,6 +148,11 @@ class Pyboard(object):
self.serial.write(b'\x04') self.serial.write(b'\x04')
return b'soft reboot\r\n' return b'soft reboot\r\n'
def interupt(self, max_times: int = 2):
for _ in range(0, max_times):
self.serial.write(b'\r\x03')
time.sleep(0.05)
def read_until(self, delimiter: bytes, stream_output: bool = False) -> Optional[bytes]: def read_until(self, delimiter: bytes, stream_output: bool = False) -> Optional[bytes]:
data = self.serial.read(1) data = self.serial.read(1)
@@ -400,6 +406,7 @@ class FileSystem(object):
r = [] r = []
if os.stat(filename)[0] == 0x4000: if os.stat(filename)[0] == 0x4000:
e = ls(filename) e = ls(filename)
if filename != '/':
e.append((filename, -1)) e.append((filename, -1))
for f, s in e: for f, s in e:
if not s == -1: if not s == -1:
@@ -459,7 +466,6 @@ class FileSystem(object):
continue continue
d.append(zd) d.append(zd)
zd = "/".join(d) zd = "/".join(d)
try: try:
os.mkdir(zd) os.mkdir(zd)
r.append(("/" + zd, 1)) r.append(("/" + zd, 1))
@@ -491,14 +497,14 @@ class FileSystem(object):
raise e raise e
PRINT_PREFIX_EXCEPTION = '\t =>' PRINT_PREFIX_EXCEPTION = '\t\t =>'
class Picowatch(object): class Picowatch(object):
_fs: FileSystem _fs: FileSystem
def __init__(self, pyboard: Pyboard): def __init__(self, pyboard: Pyboard):
self._fs = FileSystem(pyboard) self._fs = FileSystem(pyboard)
signal.signal(signal.SIGINT, lambda a, b: pyboard.interupt())
def listing(self, remote: str = '/'): def listing(self, remote: str = '/'):
status, output, exception = self._fs.ls(remote) status, output, exception = self._fs.ls(remote)
@@ -506,29 +512,66 @@ class Picowatch(object):
if status: if status:
for name, size in output: for name, size in output:
if size == -1: if size == -1:
print('[/]', name[1:]) print('[.]', name[1:])
else: else:
print('[·]', name[1:], f'({size}b)') print('[:]', name[1:], f'({size}b)')
else: else:
print('[?]', remote, PRINT_PREFIX_EXCEPTION, exception) print('[?]', remote, PRINT_PREFIX_EXCEPTION, exception)
def upload(self, local: str, remote: str = ''): def contents(self, remote: str):
pass try:
content, checksum = self._fs.get(remote)
for ln in content.decode('utf-8').split('\n'):
print(ln)
except Exception as e:
print('[?]', remote, f'\n{str(e)}')
def upload(self, filepath: str):
local = filepath
if not local.startswith('./'):
local = './' + local
if not local.startswith('.'):
local = '.' + local
queue = []
if os.path.isdir(local):
for root, _, files in os.walk(local, followlinks=True):
for filename in files:
filename = os.path.join(root, filename).replace('\\', '/')
queue.append((filename, filename[1:]))
elif os.path.exists(local):
queue.append((local, filepath))
for filename, remote in queue:
try:
print('[↑]', filename, PRINT_PREFIX_EXCEPTION, self._fs.upload(filename, remote))
except Exception as e:
print('[?]', filename, PRINT_PREFIX_EXCEPTION, str(e))
def download(self, filepath: str): def download(self, filepath: str):
if filepath.startswith('.'):
filepath = filepath[1:]
status, output, exception = self._fs.ls(filepath) status, output, exception = self._fs.ls(filepath)
if status: if status:
for remote, size in output: for remote, size in output:
if size == -1: if size == -1:
os.makedirs(remote, 777, exist_ok=True) os.makedirs(f'.{remote}', 777, exist_ok=True)
for remote, size in output: for remote, size in output:
local = f'.{remote}'
if not size == -1: if not size == -1:
try: try:
print('[↓]', remote, PRINT_PREFIX_EXCEPTION, self._fs.download(remote, f'.{remote}')) print('[↓]', local, PRINT_PREFIX_EXCEPTION, self._fs.download(remote, local))
except Exception as e: except Exception as e:
print('[?]', remote, PRINT_PREFIX_EXCEPTION, str(e)) print('[?]', local, PRINT_PREFIX_EXCEPTION, str(e))
else: else:
print('[?]', filepath, PRINT_PREFIX_EXCEPTION, exception) print('[?]', filepath, PRINT_PREFIX_EXCEPTION, exception)
@@ -537,20 +580,17 @@ class Picowatch(object):
if status: if status:
for remote, checked, message in output: for remote, checked, message in output:
local = f'.{remote}'
if checked: if checked:
print('[-]', remote) print('[-]', local)
else: else:
print('[?]', remote, PRINT_PREFIX_EXCEPTION, message) print('[?]', local, PRINT_PREFIX_EXCEPTION, message)
else: else:
print('[?]', filepath, PRINT_PREFIX_EXCEPTION, exception) print('[?]', filepath, PRINT_PREFIX_EXCEPTION, exception)
def execute(self, filepath: str): def launch(self, filepath: str):
self._fs.launch(filepath)
def watch(self, filepath: str):
pass pass
def watch(self, local: str):
pass
# picowatch = Picowatch(Pyboard('COM5'))
# picowatch.listing('/lib/')