Most of the pep8 changes finished. Be careful, things may be horribly broken.

This commit is contained in:
Storm Dragon
2025-07-03 13:22:00 -04:00
parent 7408951152
commit 21bb9c6083
344 changed files with 6374 additions and 6083 deletions

View File

@ -18,22 +18,22 @@ import fcntl
import getpass
from select import select
from fenrirscreenreader.core import debug
from fenrirscreenreader.core.eventData import fenrirEventType
from fenrirscreenreader.core.screenDriver import screenDriver
from fenrirscreenreader.core.eventData import FenrirEventType
from fenrirscreenreader.core.screenDriver import ScreenDriver as screenDriver
from fenrirscreenreader.utils import screen_utils
class fenrirScreen(pyte.Screen):
class FenrirScreen(pyte.Screen):
def set_margins(self, *args, **kwargs):
kwargs.pop("private", None)
super(fenrirScreen, self).set_margins(*args, **kwargs)
super(FenrirScreen, self).set_margins(*args, **kwargs)
class Terminal:
def __init__(self, columns, lines, p_in):
self.text = ''
self.attributes = None
self.screen = fenrirScreen(columns, lines)
self.screen = FenrirScreen(columns, lines)
self.screen.write_process_input = \
lambda data: p_in.write(data.encode())
self.stream = pyte.ByteStream()
@ -42,7 +42,7 @@ class Terminal:
def feed(self, data):
self.stream.feed(data)
def updateAttributes(self, initialize=False):
def update_attributes(self, initialize=False):
buffer = self.screen.buffer
lines = None
if not initialize:
@ -58,7 +58,7 @@ class Terminal:
# Terminal class doesn't have access to env, use fallback
# logging
print(
f'ptyDriver Terminal updateAttributes: Error accessing attributes: {e}')
f'ptyDriver Terminal update_attributes: Error accessing attributes: {e}')
self.attributes.append([])
self.attributes[y] = [list(
@ -78,25 +78,25 @@ class Terminal:
def resize(self, lines, columns):
self.screen.resize(lines, columns)
self.setCursor()
self.updateAttributes(True)
self.set_cursor()
self.update_attributes(True)
def setCursor(self, x=-1, y=-1):
xPos = x
yPos = y
if xPos == -1:
xPos = self.screen.cursor.x
if yPos == -1:
yPos = self.screen.cursor.y
def set_cursor(self, x=-1, y=-1):
x_pos = x
y_pos = y
if x_pos == -1:
x_pos = self.screen.cursor.x
if y_pos == -1:
y_pos = self.screen.cursor.y
self.screen.cursor.x = min(
self.screen.cursor.x,
self.screen.columns - 1)
self.screen.cursor.y = min(self.screen.cursor.y, self.screen.lines - 1)
def GetScreenContent(self):
def get_screen_content(self):
cursor = self.screen.cursor
self.text = '\n'.join(self.screen.display)
self.updateAttributes(self.attributes is None)
self.update_attributes(self.attributes is None)
self.screen.dirty.clear()
return {"cursor": (cursor.x, cursor.y),
'lines': self.screen.lines,
@ -115,49 +115,49 @@ class driver(screenDriver):
self.p_out = None
self.terminal = None
self.p_pid = -1
signal.signal(signal.SIGWINCH, self.handleSigwinch)
signal.signal(signal.SIGWINCH, self.handle_sigwinch)
def initialize(self, environment):
self.env = environment
self.command = self.env['runtime']['settingsManager'].getSetting(
self.command = self.env['runtime']['SettingsManager'].get_setting(
'general', 'shell')
self.shortcutType = self.env['runtime']['inputManager'].getShortcutType(
self.shortcutType = self.env['runtime']['InputManager'].get_shortcut_type(
)
self.env['runtime']['processManager'].addCustomEventThread(
self.terminalEmulation)
self.env['runtime']['ProcessManager'].add_custom_event_thread(
self.terminal_emulation)
def getCurrScreen(self):
def get_curr_screen(self):
self.env['screen']['oldTTY'] = 'pty'
self.env['screen']['newTTY'] = 'pty'
def injectTextToScreen(self, msgBytes, screen=None):
def inject_text_to_screen(self, msg_bytes, screen=None):
if not screen:
screen = self.p_out.fileno()
if isinstance(msgBytes, str):
msgBytes = bytes(msgBytes, 'UTF-8')
os.write(screen, msgBytes)
if isinstance(msg_bytes, str):
msg_bytes = bytes(msg_bytes, 'UTF-8')
os.write(screen, msg_bytes)
def getSessionInformation(self):
def get_session_information(self):
self.env['screen']['autoIgnoreScreens'] = []
self.env['general']['prevUser'] = getpass.getuser()
self.env['general']['currUser'] = getpass.getuser()
self.env['general']['prev_user'] = getpass.getuser()
self.env['general']['curr_user'] = getpass.getuser()
def readAll(self, fd, timeout=0.3, interruptFd=None, len=65536):
msgBytes = b''
fdList = []
fdList += [fd]
def read_all(self, fd, timeout=0.3, interruptFd=None, len=65536):
msg_bytes = b''
fd_list = []
fd_list += [fd]
if interruptFd:
fdList += [interruptFd]
fd_list += [interruptFd]
starttime = time.time()
while True:
r = screen_utils.hasMoreWhat(fdList, 0.0001)
r = screen_utils.has_more_what(fd_list, 0.0001)
# nothing more to read
if fd not in r:
break
data = os.read(fd, len)
if data == b'':
raise EOFError
msgBytes += data
msg_bytes += data
# exit on interrupt available
if interruptFd in r:
break
@ -165,9 +165,9 @@ class driver(screenDriver):
# more is here
if (time.time() - starttime) >= timeout:
break
return msgBytes
return msg_bytes
def openTerminal(self, columns, lines, command):
def open_terminal(self, columns, lines, command):
p_pid, master_fd = pty.fork()
if p_pid == 0: # Child.
argv = shlex.split(command)
@ -187,96 +187,96 @@ class driver(screenDriver):
p_out = os.fdopen(master_fd, "w+b", 0)
return Terminal(columns, lines, p_out), p_pid, p_out
def resizeTerminal(self, fd):
def resize_terminal(self, fd):
s = struct.pack('HHHH', 0, 0, 0, 0)
s = fcntl.ioctl(0, termios.TIOCGWINSZ, s)
fcntl.ioctl(fd, termios.TIOCSWINSZ, s)
lines, columns, _, _ = struct.unpack('hhhh', s)
return lines, columns
def getTerminalSize(self, fd):
def get_terminal_size(self, fd):
s = struct.pack('HHHH', 0, 0, 0, 0)
lines, columns, _, _ = struct.unpack(
'HHHH', fcntl.ioctl(fd, termios.TIOCGWINSZ, s))
return lines, columns
def handleSigwinch(self, *args):
def handle_sigwinch(self, *args):
os.write(self.signalPipe[1], b'w')
def terminalEmulation(self, active, eventQueue):
def terminal_emulation(self, active, event_queue):
try:
old_attr = termios.tcgetattr(sys.stdin)
tty.setraw(0)
lines, columns = self.getTerminalSize(0)
lines, columns = self.get_terminal_size(0)
if self.command == '':
self.command = screen_utils.getShell()
self.terminal, self.p_pid, self.p_out = self.openTerminal(
self.command = screen_utils.get_shell()
self.terminal, self.p_pid, self.p_out = self.open_terminal(
columns, lines, self.command)
lines, columns = self.resizeTerminal(self.p_out)
lines, columns = self.resize_terminal(self.p_out)
self.terminal.resize(lines, columns)
fdList = [sys.stdin, self.p_out, self.signalPipe[0]]
fd_list = [sys.stdin, self.p_out, self.signalPipe[0]]
while active.value:
r, _, _ = select(fdList, [], [], 1)
r, _, _ = select(fd_list, [], [], 1)
# none
if r == []:
continue
# signals
if self.signalPipe[0] in r:
os.read(self.signalPipe[0], 1)
lines, columns = self.resizeTerminal(self.p_out)
lines, columns = self.resize_terminal(self.p_out)
self.terminal.resize(lines, columns)
# input
if sys.stdin in r:
try:
msgBytes = self.readAll(sys.stdin.fileno(), len=4096)
msg_bytes = self.read_all(sys.stdin.fileno(), len=4096)
except (EOFError, OSError):
eventQueue.put(
{"Type": fenrirEventType.StopMainLoop, "Data": None})
event_queue.put(
{"Type": FenrirEventType.stop_main_loop, "data": None})
break
if self.shortcutType == 'KEY':
try:
self.injectTextToScreen(msgBytes)
self.inject_text_to_screen(msg_bytes)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut(
self.env['runtime']['DebugManager'].write_debug_out(
'ptyDriver getInputData: Error injecting text to screen: ' + str(e),
debug.debugLevel.ERROR)
eventQueue.put(
{"Type": fenrirEventType.StopMainLoop, "Data": None})
debug.DebugLevel.ERROR)
event_queue.put(
{"Type": FenrirEventType.stop_main_loop, "data": None})
break
else:
eventQueue.put({"Type": fenrirEventType.ByteInput,
"Data": msgBytes})
event_queue.put({"Type": FenrirEventType.byte_input,
"data": msg_bytes})
# output
if self.p_out in r:
try:
msgBytes = self.readAll(
msg_bytes = self.read_all(
self.p_out.fileno(), interruptFd=sys.stdin.fileno())
except (EOFError, OSError):
eventQueue.put(
{"Type": fenrirEventType.StopMainLoop, "Data": None})
event_queue.put(
{"Type": FenrirEventType.stop_main_loop, "data": None})
break
# feed and send event bevore write, the pyte already has the right state
# so fenrir already can progress bevore os.write what
# should give some better reaction time
self.terminal.feed(msgBytes)
eventQueue.put(
self.terminal.feed(msg_bytes)
event_queue.put(
{
"Type": fenrirEventType.ScreenUpdate,
"Data": screen_utils.createScreenEventData(
self.terminal.GetScreenContent())})
self.injectTextToScreen(
msgBytes, screen=sys.stdout.fileno())
"Type": FenrirEventType.screen_update,
"data": screen_utils.create_screen_event_data(
self.terminal.get_screen_content())})
self.inject_text_to_screen(
msg_bytes, screen=sys.stdout.fileno())
except Exception as e: # Process died?
print(e)
eventQueue.put(
{"Type": fenrirEventType.StopMainLoop, "Data": None})
event_queue.put(
{"Type": FenrirEventType.stop_main_loop, "data": None})
finally:
os.kill(self.p_pid, signal.SIGTERM)
self.p_out.close()
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
eventQueue.put(
{"Type": fenrirEventType.StopMainLoop, "Data": None})
event_queue.put(
{"Type": FenrirEventType.stop_main_loop, "data": None})
sys.exit(0)
def getCurrApplication(self):
def get_curr_application(self):
pass