Skip to content

Commit

Permalink
Run pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
skorokithakis committed Apr 11, 2020
1 parent 190bc75 commit 429469f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 49 deletions.
10 changes: 8 additions & 2 deletions mp/mpfexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def teardown(self):
def setup(self):

self.enter_raw_repl()
self.exec_("try:\n import uos\nexcept ImportError:\n import os as uos\nimport sys")
self.exec_(
"try:\n import uos\nexcept ImportError:\n import os as uos\nimport sys"
)
self.exec_(
"try:\n import ubinascii\nexcept ImportError:\n import binascii as ubinascii"
)
Expand Down Expand Up @@ -492,7 +494,11 @@ def ls(self, add_files=True, add_dirs=True, add_details=False):
hit = MpFileExplorer.ls(self, True, True, True)
self.__cache(self.dir, hit)

files = [f for f in hit if ((add_files and f[1] == "F") or (add_dirs and f[1] == "D"))]
files = [
f
for f in hit
if ((add_files and f[1] == "F") or (add_dirs and f[1] == "D"))
]
files.sort(key=lambda x: (x[1], x[0]))
if not add_details:
files = [f[0] for f in files]
Expand Down
9 changes: 7 additions & 2 deletions mp/mpfshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
##


import tempfile
import argparse
import cmd
import glob
Expand All @@ -32,6 +31,7 @@
import os
import platform
import sys
import tempfile

import colorama
import serial
Expand Down Expand Up @@ -700,7 +700,11 @@ def do_putc(self, args):
if len(s_args) > 1:
rfile_name = s_args[1]
else:
rfile_name = (lfile_name[:lfile_name.rfind(".")] if "." in lfile_name else lfile_name) + ".mpy"
rfile_name = (
lfile_name[: lfile_name.rfind(".")]
if "." in lfile_name
else lfile_name
) + ".mpy"

_, tmp = tempfile.mkstemp()

Expand All @@ -711,6 +715,7 @@ def do_putc(self, args):
self.__error(str(e))

os.unlink(tmp)

complete_putc = complete_mpyc


Expand Down
2 changes: 1 addition & 1 deletion mp/pyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):

def enter_raw_repl(self):

time.sleep(0.5) # allow some time for board to reset
time.sleep(0.5) # allow some time for board to reset
self.con.write(b"\r\x03\x03") # ctrl-C twice: interrupt any running program

# flush input (without relying on serial.flushInput())
Expand Down
93 changes: 50 additions & 43 deletions mp/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ def __init__(self, serial_instance, echo=False, eol="crlf"):

self.serial = serial_instance

convert = {
"cr": CONVERT_CR,
"lf": CONVERT_LF,
"crlf": CONVERT_CRLF
}
convert = {"cr": CONVERT_CR, "lf": CONVERT_LF, "crlf": CONVERT_CRLF}

self.console = console
self.echo = echo
Expand All @@ -69,17 +65,18 @@ def set_rx_encoding(self, enc):
def set_tx_encoding(self, enc):
pass


else:

# see if we could use the new Miniterm implementation
from serial.tools.miniterm import Miniterm, ConsoleBase, unichr
import os
if os.name == 'nt': # noqa

if os.name == "nt": # noqa
import codecs
import sys
import msvcrt
import ctypes
import platform

class Out(object):
"""file-like wrapper that uses os.write"""
Expand All @@ -95,71 +92,81 @@ def write(self, s):

class Console(ConsoleBase):
fncodes = {
';': '\1bOP', # F1
'<': '\1bOQ', # F2
'=': '\1bOR', # F3
'>': '\1bOS', # F4
'?': '\1b[15~', # F5
'@': '\1b[17~', # F6
'A': '\1b[18~', # F7
'B': '\1b[19~', # F8
'C': '\1b[20~', # F9
'D': '\1b[21~', # F10
";": "\1bOP", # F1
"<": "\1bOQ", # F2
"=": "\1bOR", # F3
">": "\1bOS", # F4
"?": "\1b[15~", # F5
"@": "\1b[17~", # F6
"A": "\1b[18~", # F7
"B": "\1b[19~", # F8
"C": "\1b[20~", # F9
"D": "\1b[21~", # F10
}
navcodes = {
'H': '\x1b[A', # UP
'P': '\x1b[B', # DOWN
'K': '\x1b[D', # LEFT
'M': '\x1b[C', # RIGHT
'G': '\x1b[H', # HOME
'O': '\x1b[F', # END
'R': '\x1b[2~', # INSERT
'S': '\x1b[3~', # DELETE
'I': '\x1b[5~', # PGUP
'Q': '\x1b[6~', # PGDN
"H": "\x1b[A", # UP
"P": "\x1b[B", # DOWN
"K": "\x1b[D", # LEFT
"M": "\x1b[C", # RIGHT
"G": "\x1b[H", # HOME
"O": "\x1b[F", # END
"R": "\x1b[2~", # INSERT
"S": "\x1b[3~", # DELETE
"I": "\x1b[5~", # PGUP
"Q": "\x1b[6~", # PGDN
}

def __init__(self):
super(Console, self).__init__()
self._saved_ocp = ctypes.windll.kernel32.GetConsoleOutputCP()
self._saved_icp = ctypes.windll.kernel32.GetConsoleCP()
ctypes.windll.kernel32.SetConsoleOutputCP(65001)
ctypes.windll.kernel32.SetConsoleCP(65001)
# ANSI handling available through SetConsoleMode since Windows 10 v1511
# ANSI handling available through SetConsoleMode since Windows 10 v1511
# https://en.wikipedia.org/wiki/ANSI_escape_code#cite_note-win10th2-1
# if platform.release() == '10' and int(platform.version().split('.')[2]) > 10586:
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
import ctypes.wintypes as wintypes
if not hasattr(wintypes, 'LPDWORD'): # PY2

if not hasattr(wintypes, "LPDWORD"): # PY2
wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)
SetConsoleMode = ctypes.windll.kernel32.SetConsoleMode
GetConsoleMode = ctypes.windll.kernel32.GetConsoleMode
GetStdHandle = ctypes.windll.kernel32.GetStdHandle
mode = wintypes.DWORD()
GetConsoleMode(GetStdHandle(-11), ctypes.byref(mode))
if (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0:
SetConsoleMode(GetStdHandle(-11), mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
SetConsoleMode(
GetStdHandle(-11),
mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING,
)
self._saved_cm = mode
self.output = codecs.getwriter('UTF-8')(Out(sys.stdout.fileno()), 'replace')
self.output = codecs.getwriter("UTF-8")(
Out(sys.stdout.fileno()), "replace"
)
# the change of the code page is not propagated to Python, manually fix it
sys.stderr = codecs.getwriter('UTF-8')(Out(sys.stderr.fileno()), 'replace')
sys.stderr = codecs.getwriter("UTF-8")(
Out(sys.stderr.fileno()), "replace"
)
sys.stdout = self.output
self.output.encoding = 'UTF-8' # needed for input
self.output.encoding = "UTF-8" # needed for input

def __del__(self):
ctypes.windll.kernel32.SetConsoleOutputCP(self._saved_ocp)
ctypes.windll.kernel32.SetConsoleCP(self._saved_icp)
try:
ctypes.windll.kernel32.SetConsoleMode(ctypes.windll.kernel32.GetStdHandle(-11), self._saved_cm)
except AttributeError: # in case no _saved_cm
ctypes.windll.kernel32.SetConsoleMode(
ctypes.windll.kernel32.GetStdHandle(-11), self._saved_cm
)
except AttributeError: # in case no _saved_cm
pass

def getkey(self):
while True:
z = msvcrt.getwch()
if z == unichr(13):
return unichr(10)
elif z is unichr(0) or z is unichr(0xe0):
elif z is unichr(0) or z is unichr(0xE0):
try:
code = msvcrt.getwch()
if z is unichr(0):
Expand All @@ -175,26 +182,26 @@ def cancel(self):
# CancelIo, CancelSynchronousIo do not seem to work when using
# getwch, so instead, send a key to the window with the console
hwnd = ctypes.windll.kernel32.GetConsoleWindow()
ctypes.windll.user32.PostMessageA(hwnd, 0x100, 0x0d, 0)
ctypes.windll.user32.PostMessageA(hwnd, 0x100, 0x0D, 0)

class Term(Miniterm):

def __init__(self, serial_instance, echo=False, eol='crlf', filters=()):
def __init__(self, serial_instance, echo=False, eol="crlf", filters=()):
self.console = Console()
self.serial = serial_instance
self.echo = echo
self.raw = False
self.input_encoding = 'UTF-8'
self.output_encoding = 'UTF-8'
self.input_encoding = "UTF-8"
self.output_encoding = "UTF-8"
self.eol = eol
self.filters = filters
self.update_transformations()
self.exit_character = unichr(0x1d) # GS/CTRL+]
self.exit_character = unichr(0x1D) # GS/CTRL+]
self.menu_character = unichr(0x14) # Menu: CTRL+T
self.alive = None
self._reader_alive = None
self.receiver_thread = None
self.rx_decoder = None
self.tx_decoder = None

else:
Term = Miniterm
2 changes: 1 addition & 1 deletion mp/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
##


FULL = '0.9.1'
FULL = "0.9.1"

MAJOR, MINOR, PATCH = FULL.split(".")

0 comments on commit 429469f

Please sign in to comment.