Refactoring codes...

This commit is contained in:
Gino D
2023-01-06 21:30:08 +01:00
parent cbcc6d2b8d
commit bbc9bcf34f

View File

@@ -35,10 +35,11 @@ import subprocess
from serial import Serial from serial import Serial
from dotenv import dotenv_values from dotenv import dotenv_values
from typing import List, Optional, Tuple, Union, Iterable from typing import List, Optional, Tuple, Union
BUFFER_SIZE: int = 256 BUFFER_SIZE: int = 256
LISTENING_TO: str = os.getcwd()
class Tab(): class Tab():
bordered: bool = False bordered: bool = False
@@ -618,9 +619,9 @@ class Picowatch(object):
queue = [] queue = []
if filepath == '/': if filepath == '/':
filepath = os.getcwd().replace(os.sep, '/') filepath = LISTENING_TO.replace(os.sep, '/')
else: else:
filepath = os.path.join(os.getcwd(), filepath.strip('./').strip('/')).replace(os.sep, '/') filepath = os.path.join(LISTENING_TO, filepath.strip('./').strip('/')).replace(os.sep, '/')
if os.path.isfile(filepath): if os.path.isfile(filepath):
queue = [(filepath, os.stat(filepath)[6])] queue = [(filepath, os.stat(filepath)[6])]
@@ -676,7 +677,7 @@ class Picowatch(object):
tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception') tab.head('[ ]', 'Filename', 'Size (kb)', 'Checksum', 'Exception')
for source, size in self.internal_ls(filepath): for source, size in self.internal_ls(filepath):
destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/') destination = source.replace(LISTENING_TO.replace(os.sep, '/'), '').strip('/')
try: try:
tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination)) tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination))
@@ -691,10 +692,10 @@ class Picowatch(object):
if status: if status:
for remote, size in output: for remote, size in output:
if size == -1: if size == -1:
os.makedirs(os.path.join(os.getcwd(), remote), 777, exist_ok=True) os.makedirs(os.path.join(LISTENING_TO, remote), 777, exist_ok=True)
for remote, size in output: for remote, size in output:
destination = os.path.join(os.getcwd(), remote).replace(os.sep, '/') destination = os.path.join(LISTENING_TO, remote).replace(os.sep, '/')
if not size == -1: if not size == -1:
try: try:
@@ -726,7 +727,7 @@ class Picowatch(object):
with os.fdopen(fh, 'wb') as tmp: with os.fdopen(fh, 'wb') as tmp:
tmp.write(content) tmp.write(content)
subprocess.Popen(f'code --diff "{tempname}" "{os.path.join(os.getcwd(), filepath)}"', stdout=subprocess.PIPE, shell=True).communicate() subprocess.Popen(f'code --diff "{tempname}" "{os.path.join(LISTENING_TO, filepath)}"', stdout=subprocess.PIPE, shell=True).communicate()
finally: finally:
input('Press Enter to delete temp file.') input('Press Enter to delete temp file.')
os.remove(tempname) os.remove(tempname)
@@ -756,11 +757,14 @@ class Picowatch(object):
if return_output: if return_output:
return changes return changes
tab = Tab(4, 40) tab = Tab(4, 8, 40)
tab.head('[ ]', 'Filename') tab.head('[ ]', 'Status', 'Filename')
for status, filename in changes: for status, filename in changes:
tab.line('[+]' if status == 1 else '[-]', filename) if status == 1:
tab.line('[-]', 'EDITED', filename)
else:
tab.line('[+]', 'DELETED', filename)
def push(self): def push(self):
tab = Tab(4, 30, 15, 100) tab = Tab(4, 30, 15, 100)
@@ -786,7 +790,7 @@ class Picowatch(object):
queue.extend(self.internal_ls(filepath)) queue.extend(self.internal_ls(filepath))
for source, size in queue: for source, size in queue:
destination = source.replace(os.getcwd().replace(os.sep, '/'), '').strip('/') destination = source.replace(LISTENING_TO.replace(os.sep, '/'), '').strip('/')
try: try:
tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination)) tab.line('[↑]', destination, f'{round(size / 1024, 2)} kb', self.filesystem.upload(source, destination))
@@ -804,7 +808,7 @@ class Picowatch(object):
print(f'MicroPython File from "{filename}" created!') print(f'MicroPython File from "{filename}" created!')
def test(self, filename: str): def test(self, filename: str):
with open(os.path.join(os.getcwd(), filename), 'r') as fh: with open(os.path.join(LISTENING_TO, filename), 'r') as fh:
self.filesystem.terminal(fh.read(), stream_output=True) self.filesystem.terminal(fh.read(), stream_output=True)
def launch(self, filename: str): def launch(self, filename: str):
@@ -812,7 +816,7 @@ class Picowatch(object):
print('Welcome to Picowatch Terminal') print('Welcome to Picowatch Terminal')
print(f'Listening to project: {os.getcwd().replace(os.sep, "/")}/') print(f'Listening to project: {LISTENING_TO.replace(os.sep, "/")}/')
picowatch = False picowatch = False
try: try:
@@ -830,7 +834,7 @@ except:
print('-' * 50) print('-' * 50)
print(f'Connected to device: {device} and baudrate: {baudrate}') print(f'Connected to device: {device} and baudrate: {baudrate}')
with open(os.path.join(os.getcwd(), '.picowatch'), 'w+') as fh: with open(os.path.join(LISTENING_TO, '.picowatch'), 'w+') as fh:
fh.write(f'DEVICE = "{device}"\n') fh.write(f'DEVICE = "{device}"\n')
fh.write(f'BAUDRATE = {baudrate}\n') fh.write(f'BAUDRATE = {baudrate}\n')
except Exception as e: except Exception as e:
@@ -845,10 +849,12 @@ while True:
for message in unmessage.split('&'): for message in unmessage.split('&'):
match message.strip().split(' '): match message.strip().split(' '):
case ['exit']: case ['exit' | '\x18']:
sys.exit() sys.exit()
case ['help']: case ['help']:
print('TODO') print('TODO')
case ['whois']:
print('TODO')
case ['reboot']: case ['reboot']:
picowatch.terminal('help()') picowatch.terminal('help()')
case ['ls' | 'list', *source]: case ['ls' | 'list', *source]: