From c44c5af8569a13ce3295da5fc0a3fca8baef0c4f Mon Sep 17 00:00:00 2001 From: Gino D Date: Wed, 4 Jan 2023 18:28:31 +0100 Subject: [PATCH] Save wip... --- .picowatch | 3 +++ picowatch.py | 73 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 .picowatch diff --git a/.picowatch b/.picowatch new file mode 100644 index 0000000..9fb1748 --- /dev/null +++ b/.picowatch @@ -0,0 +1,3 @@ +DEVICE = 'COM5' +BAUDRATE = 115200 +PROJECT_NAME = 'dist' \ No newline at end of file diff --git a/picowatch.py b/picowatch.py index b21441e..c39c886 100644 --- a/picowatch.py +++ b/picowatch.py @@ -33,10 +33,11 @@ import textwrap import subprocess from serial import Serial +from dotenv import dotenv_values from typing import List, Optional, Tuple, Union -BUFFER_SIZE: int = 126 +BUFFER_SIZE: int = 256 class Tab(): @@ -156,7 +157,7 @@ class Pyboard(object): except: time.sleep(1) else: - raise Exception(f'Failed to access {device}') + raise Exception(f'Failed to access device: {device}') def close(self): self.serial.close() @@ -341,7 +342,7 @@ class FileSystem(object): sys.stdout.write(ubinascii.hexlify(result)) """) output = binascii.unhexlify(output) - + return (output, self.checksum(filename, output)) def download(self, source: str, destination: str) -> str: @@ -385,8 +386,7 @@ class FileSystem(object): return checksum def ls(self, dirname: str = '/') -> Tuple[int, List, str]: - if not dirname.startswith('/'): - dirname = '/' + dirname + dirname = dirname.strip('./').strip('/') output = self.terminal(f""" try: @@ -403,6 +403,8 @@ class FileSystem(object): if not dirname.endswith("/"): dirname += "/" for t in os.ilistdir(dirname): + if dirname.startswith('/'): + dirname = dirname[1:] if t[1] == 0x4000: e.append((dirname + t[0] + '/', -1)) e.extend(ls(dirname + t[0] + '/')) @@ -443,6 +445,8 @@ class FileSystem(object): if not dirname.endswith("/"): dirname += "/" for t in os.ilistdir(dirname): + if dirname.startswith('/'): + dirname = dirname[1:] if t[1] == 0x4000: e.append((dirname + t[0] + '/', -1)) e.extend(ls(dirname + t[0] + '/')) @@ -549,14 +553,17 @@ class Picowatch(object): def __init__(self, pyboard: Pyboard, project_name: str = 'src'): self.filesystem = FileSystem(pyboard) - project_name = project_name.strip('./').strip('/') - if project_name == '/': - raise Exception('Project name is incorrect!') + if not project_name or project_name == '/': + raise Exception('Project name is incorrect, can not be empty or only a forward slash "/"!') self.project_name = project_name.replace(os.sep, '/') self.project_dirname = os.path.join(os.getcwd(), self.project_name).replace(os.sep, '/') + + if not os.path.isdir(self.project_dirname): + raise Exception('Project is not a directory!') + signal.signal(signal.SIGINT, lambda a, b: self.interupt()) def interupt(self): @@ -567,7 +574,7 @@ class Picowatch(object): def listing(self, filepath: str = '/'): filepath = filepath.strip('./') - tab = Tab(4, 30, 15, 30) + tab = Tab(4, 30, 15, 100) tab.labels('[ ]', 'Filename', 'Size (kb)', 'Exception') status, output, exception = self.filesystem.ls(filepath) @@ -576,7 +583,7 @@ class Picowatch(object): if size == -1: tab.print('[*]', filename, '-') else: - tab.print('[*]', filename, f'{size}b') + tab.print('[*]', filename, f'{round(size / 1024, 2)} kb') else: tab.print('[?]', filepath, '', str(exception)) @@ -601,9 +608,9 @@ class Picowatch(object): elif os.path.exists(source): queue.append((source, filepath)) - tab = Tab(4, 30, 15, 30) + tab = Tab(4, 30, 15, 100) tab.labels('[ ]', 'Filename', 'Checksum', 'Exception') - + for source, destination in queue: destination = destination.strip('/') @@ -614,7 +621,7 @@ class Picowatch(object): def download(self, filepath: str): filepath = filepath.strip('./').strip('/') - tab = Tab(4, 30, 15, 30) + tab = Tab(4, 30, 15, 100) tab.labels('', 'Filename', 'Checksum', 'Exception') status, output, exception = self.filesystem.ls(filepath) @@ -624,9 +631,11 @@ class Picowatch(object): os.makedirs(os.path.join(self.project_dirname, remote), 777, exist_ok=True) for remote, size in output: + destination = os.path.join(self.project_dirname, remote).replace(os.sep, '/') + if not size == -1: try: - tab.print('[↓]', remote, self.filesystem.download(remote, os.path.join(self.project_dirname, remote))) + tab.print('[↓]', remote, self.filesystem.download(remote, destination)) except Exception as e: tab.print('[?]', remote, '', str(e)) else: @@ -634,7 +643,7 @@ class Picowatch(object): def delete(self, filepath: str): filepath = filepath.strip('./') - tab = Tab(4, 30, 30) + tab = Tab(4, 30, 100) tab.labels('[ ]', 'Filename', 'Exception') status, output, exception = self.filesystem.rm(filepath) @@ -691,7 +700,7 @@ class Picowatch(object): tab.print('[+]' if status == 1 else '[-]', filename) def push(self): - tab = Tab(4, 30, 15, 30) + tab = Tab(4, 30, 15, 100) tab.labels('[ ]', 'Filename', 'Checksum', 'Exception') changes = self.status(return_output=True) @@ -743,19 +752,24 @@ class Picowatch(object): print('Welcome to Picowatch Terminal') picowatch = False -while not picowatch: - print('-' * 30) - device = input('Port: ').strip() - baudrate = input('Baudrate (115200): ').strip() or 115200 - project_name = input('Project name (src): ').strip() or 'src' +try: + env = dotenv_values('.picowatch') + picowatch = Picowatch(Pyboard(env["DEVICE"], env["BAUDRATE"]), env["PROJECT_NAME"]) + print(f'Connected automatically to device: {env["DEVICE"]} at a baudrate of: {env["BAUDRATE"]}. Listening to project: ./{env["PROJECT_NAME"]}') +except: + while not picowatch: + print('-' * 50) + device = input('Port: ').strip() + baudrate = input('Baudrate (115200): ').strip() or 115200 + project_name = input('Project name (src): ').strip() or 'src' + + try: + picowatch = Picowatch(Pyboard(device, baudrate), project_name) + print('-' * 50) + print(f'Connected to device: {picowatch} at a baudrate of: {baudrate}. Listening to project: ./{project_name}') + except Exception as e: + print(str(e)) - try: - picowatch = Picowatch(Pyboard(device, baudrate), project_name) - print(f'Connected to device: {device} at a baudrate of: {baudrate}. Listening to project: {project_name}') - print('-' * 30) - except Exception as e: - print(str(e)) - picowatch.interupt() while True: @@ -766,6 +780,8 @@ while True: match message.strip().split(' '): case ['exit']: sys.exit() + case ['help']: + print('TODO') case ['reboot']: picowatch.terminal('help()') case ['ls' | 'list', *source]: @@ -785,6 +801,7 @@ while True: case ['push']: picowatch.push() case ['compile', filename]: + # https://pypi.org/project/mpy-cross/ pass case ['install', package_name]: pass