44import re , html
55from datetime import datetime
66from threading import Thread , Lock
7- from PyQt5 .QtWidgets import *
8- from PyQt5 .QtGui import *
9- from PyQt5 .QtCore import *
107
11- from grpcClient import *
12-
13- from TerminalPanel import *
14- from ScriptPanel import *
15- from AssistantPanel import *
16-
17- sys .path .insert (1 , './Credentials' )
18- import credentials
8+ from PyQt5 .QtCore import QObject , Qt , QEvent , QThread , QTimer , pyqtSignal , pyqtSlot
9+ from PyQt5 .QtGui import QFont , QStandardItem , QStandardItemModel , QTextCursor
10+ from PyQt5 .QtWidgets import (
11+ QWidget ,
12+ QTabWidget ,
13+ QVBoxLayout ,
14+ QHBoxLayout ,
15+ QTextEdit ,
16+ QLineEdit ,
17+ QShortcut ,
18+ QCompleter ,
19+ QTableWidget ,
20+ QTableWidgetItem ,
21+ )
22+
23+ from .grpcClient import GrpcClient , TeamServerApi_pb2
24+ from .TerminalPanel import Terminal
25+ from .ScriptPanel import Script
26+ from .AssistantPanel import Assistant
27+ from .TerminalModules .Credentials import credentials
1928
2029
2130#
@@ -570,15 +579,13 @@ def runCommand(self):
570579 self .printInTerminal ("" , "" , "" )
571580
572581 else :
573- cmdHistoryFile = open (CmdHistoryFileName , 'a' )
574- cmdHistoryFile .write (commandLine )
575- cmdHistoryFile .write ('\n ' )
576- cmdHistoryFile .close ()
582+ with open (CmdHistoryFileName , 'a' ) as cmdHistoryFile :
583+ cmdHistoryFile .write (commandLine )
584+ cmdHistoryFile .write ('\n ' )
577585
578- logFile = open (logsDir + "/" + self .logFileName , 'a' )
579- logFile .write ('[+] send: \" ' + commandLine + '\" ' )
580- logFile .write ('\n ' )
581- logFile .close ()
586+ with open (os .path .join (logsDir , self .logFileName ), 'a' ) as logFile :
587+ logFile .write ('[+] send: \" ' + commandLine + '\" ' )
588+ logFile .write ('\n ' )
582589
583590 self .commandEditor .setCmdHistory ()
584591 instructions = commandLine .split ()
@@ -612,11 +619,10 @@ def displayResponse(self):
612619 self .printInTerminal ("" , response .instruction + " " + response .cmd , response .response .decode ('utf-8' , 'replace' ))
613620 self .setCursorEditorAtEnd ()
614621
615- logFile = open (logsDir + "/" + self .logFileName , 'a' )
616- logFile .write ('[+] result: \" ' + response .instruction + " " + response .cmd + '\" ' )
617- logFile .write ('\n ' + response .response .decode ('utf-8' , 'replace' ) + '\n ' )
618- logFile .write ('\n ' )
619- logFile .close ()
622+ with open (os .path .join (logsDir , self .logFileName ), 'a' ) as logFile :
623+ logFile .write ('[+] result: \" ' + response .instruction + " " + response .cmd + '\" ' )
624+ logFile .write ('\n ' + response .response .decode ('utf-8' , 'replace' ) + '\n ' )
625+ logFile .write ('\n ' )
620626
621627 def setCursorEditorAtEnd (self ):
622628 cursor = self .editorOutput .textCursor ()
@@ -625,32 +631,36 @@ def setCursorEditorAtEnd(self):
625631
626632
627633class GetSessionResponse (QObject ):
634+ """Background worker querying session responses."""
635+
628636 checkin = pyqtSignal ()
629637
630- exit = False
638+ def __init__ (self ) -> None :
639+ super ().__init__ ()
640+ self .exit = False
631641
632- def run (self ):
633- while self .exit == False :
642+ def run (self ) -> None :
643+ while not self .exit :
634644 self .checkin .emit ()
635645 time .sleep (1 )
636646
637- def quit (self ):
638- self .exit = True
647+ def quit (self ) -> None :
648+ self .exit = True
639649
640650
641651class CommandEditor (QLineEdit ):
642652 tabPressed = pyqtSignal ()
643- cmdHistory = []
644- idx = 0
645653
646- def __init__ (self , parent = None ):
654+ def __init__ (self , parent : QWidget | None = None ) -> None :
647655 super ().__init__ (parent )
648656
649- if (os .path .isfile (CmdHistoryFileName )):
650- cmdHistoryFile = open (CmdHistoryFileName )
651- self .cmdHistory = cmdHistoryFile .readlines ()
652- self .idx = len (self .cmdHistory )- 1
653- cmdHistoryFile .close ()
657+ self .cmdHistory : list [str ] = []
658+ self .idx : int = 0
659+
660+ if os .path .isfile (CmdHistoryFileName ):
661+ with open (CmdHistoryFileName ) as cmdHistoryFile :
662+ self .cmdHistory = cmdHistoryFile .readlines ()
663+ self .idx = len (self .cmdHistory ) - 1
654664
655665 QShortcut (Qt .Key_Up , self , self .historyUp )
656666 QShortcut (Qt .Key_Down , self , self .historyDown )
@@ -686,11 +696,10 @@ def historyDown(self):
686696 cmd = self .cmdHistory [self .idx % len (self .cmdHistory )]
687697 self .setText (cmd .strip ())
688698
689- def setCmdHistory (self ):
690- cmdHistoryFile = open (CmdHistoryFileName )
691- self .cmdHistory = cmdHistoryFile .readlines ()
692- self .idx = len (self .cmdHistory )- 1
693- cmdHistoryFile .close ()
699+ def setCmdHistory (self ) -> None :
700+ with open (CmdHistoryFileName ) as cmdHistoryFile :
701+ self .cmdHistory = cmdHistoryFile .readlines ()
702+ self .idx = len (self .cmdHistory ) - 1
694703
695704 def clearLine (self ):
696705 self .clear ()
0 commit comments