Save wip...

This commit is contained in:
Gino D
2023-01-04 18:28:31 +01:00
parent 967b5b0f93
commit c44c5af856
2 changed files with 48 additions and 28 deletions

3
.picowatch Normal file
View File

@@ -0,0 +1,3 @@
DEVICE = 'COM5'
BAUDRATE = 115200
PROJECT_NAME = 'dist'

View File

@@ -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()
@@ -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,7 +608,7 @@ 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:
@@ -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,16 +752,21 @@ class Picowatch(object):
print('Welcome to Picowatch Terminal')
picowatch = False
while not picowatch:
print('-' * 30)
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(f'Connected to device: {device} at a baudrate of: {baudrate}. Listening to project: {project_name}')
print('-' * 30)
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))
@@ -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