Skip to content

Commit

Permalink
Added -F FILESTDOUT option server-side and client-side
Browse files Browse the repository at this point in the history
generalized pretty print errors function
modified shell message printer and logger creator
shifted lcid checker
created logfile checker
colored logging on stdout
  • Loading branch information
SystemRage committed Oct 16, 2019
1 parent f397481 commit dddc025
Show file tree
Hide file tree
Showing 4 changed files with 627 additions and 526 deletions.
11 changes: 7 additions & 4 deletions py-kms/pykms_Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pykms_RequestV6 import kmsRequestV6
from pykms_RpcBase import rpcBase
from pykms_DB2Dict import kmsDB2Dict
from pykms_Misc import logger_create
from pykms_Misc import logger_create, check_logfile
from pykms_Format import justify, byterize, enco, deco, ShellMessage

clt_description = 'KMS Client Emulator written in Python'
Expand All @@ -46,7 +46,8 @@
will be generated.', 'def' : None, 'des' : "machineName"},
'llevel' : {'help' : 'Use this option to set a log level. The default is \"ERROR\".', 'def' : "ERROR", 'des' : "loglevel",
'choi' : ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "MINI"]},
'lfile' : {'help' : 'Use this option to set an output log file. The default is \"pykms_logclient.log\" or type \"STDOUT\" to view log info on stdout.',
'lfile' : {'help' : 'Use this option to set an output log file. The default is \"pykms_logclient.log\". Type \"STDOUT\" to view \
log info on stdout. Type \"FILESTDOUT\" to combine previous actions.',
'def' : os.path.dirname(os.path.abspath( __file__ )) + "/pykms_logclient.log", 'des' : "logfile"},
'lsize' : {'help' : 'Use this flag to set a maximum size (in MB) to the output log file. Desactivated by default.', 'def' : 0, 'des': "logsize"},
}
Expand All @@ -61,17 +62,19 @@ def client_options():
parser.add_argument("-n", "--name", dest = clt_options['name']['des'] , default = clt_options['name']['def'], help = clt_options['name']['help'], type = str)
parser.add_argument("-V", "--loglevel", dest = clt_options['llevel']['des'], action = "store", choices = clt_options['llevel']['choi'],
default = clt_options['llevel']['def'], help = clt_options['llevel']['help'], type = str)
parser.add_argument("-F", "--logfile", dest = clt_options['lfile']['des'], action = "store", default = clt_options['lfile']['def'],
parser.add_argument("-F", "--logfile", nargs = "+", dest = clt_options['lfile']['des'], default = clt_options['lfile']['def'],
help = clt_options['lfile']['help'], type = str)
parser.add_argument("-S", "--logsize", dest = clt_options['lsize']['des'], action = "store", default = clt_options['lsize']['def'],
help = clt_options['lsize']['help'], type = float)

clt_config.update(vars(parser.parse_args()))
# Check logfile.
clt_config['logfile'] = check_logfile(clt_config['logfile'], clt_options['lfile']['def'], loggerclt)


def client_check():
# Setup hidden or not messages.
ShellMessage.view = ( False if clt_config['logfile'] == 'STDOUT' else True )
ShellMessage.view = ( False if any(i in ['STDOUT', 'FILESTDOUT'] for i in clt_config['logfile']) else True )
# Create log.
logger_create(loggerclt, clt_config, mode = 'a')

Expand Down
53 changes: 33 additions & 20 deletions py-kms/pykms_Format.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def justify(astring, indent = 35, break_every = 100):
return justy

##----------------------------------------------------------------------------------------------------------------------------------------------------
ColorMap = {'red' : '\x1b[91m',
ColorMap = {'gray' : '\x1b[90m',
'red' : '\x1b[91m',
'green' : '\x1b[92m',
'yellow' : '\x1b[93m',
'blue' : '\x1b[94m',
Expand Down Expand Up @@ -121,13 +122,13 @@ def justify(astring, indent = 35, break_every = 100):
-3 : {'text' : "{white}\t\t\t\t\t\t\t\tClient receiving{end}", 'where' : "srv"},
-4 : {'text' : "{white}\n\nServer sending{end}", 'where' : "clt"},

30 : {'text' : "{red}{bold}Server connection timed out. Exiting...{end}", 'where' : "srv"},
31 : {'text' : "{red}{bold}HWID '{0}' is invalid. Digit {1} non hexadecimal. Exiting...{end}", 'where' : "srv"},
32 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is odd length. Exiting...{end}", 'where' : "srv"},
33 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too short. Exiting...{end}", 'where' : "srv"},
34 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too long. Exiting...{end}", 'where' : "srv"},
35 : {'text' : "{red}{bold}Port number '{0}' is invalid. Enter between 1 - 65535. Exiting...{end}", 'where' : "srv"},
36 : {'text' : "{red}{bold}{0}. Exiting...{end}", 'where' : "srv"},
40 : {'text' : "{red}{bold}Server connection timed out. Exiting...{end}", 'where' : "srv"},
41 : {'text' : "{red}{bold}HWID '{0}' is invalid. Digit {1} non hexadecimal. Exiting...{end}", 'where' : "srv"},
42 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is odd length. Exiting...{end}", 'where' : "srv"},
43 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too short. Exiting...{end}", 'where' : "srv"},
44 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too long. Exiting...{end}", 'where' : "srv"},
45 : {'text' : "{red}{bold}Port number '{0}' is invalid. Enter between 1 - 65535. Exiting...{end}", 'where' : "srv"},
46 : {'text' : "{red}{bold}{0}. Exiting...{end}", 'where' : "srv"},
}

def pick_MsgMap(messagelist):
Expand Down Expand Up @@ -181,13 +182,32 @@ def __init__(self, nshell, get_text = False, put_text = None):
self.nshell = nshell
self.print_queue = Queue.Queue()
self.get_text = get_text
self.put_text = put_text
self.plaintext = []

if not isinstance(nshell, list):
self.nshell = [nshell]
if not isinstance(put_text, list):
self.put_text = [put_text]


def formatter(self, num):
if self.put_text is None:
self.msg = MsgMap[num]['text'].format(**ColorExtraMap)
else:
self.msg = MsgMap[num]['text'].format(*self.put_text, **ColorExtraMap)

if self.get_text:
self.plaintext.append(unshell_message(self.msg, m = 0)[0]["tag00"]['text'])

def run(self):
if not ShellMessage.view:
return
if self.get_text:
for num in self.nshell:
self.formatter(num)
return self.plaintext
else:
return

# Start thread process.
print_thread = threading.Thread(target = self.spawn(), args=(self.print_queue,))
print_thread.setDaemon(True)
Expand All @@ -214,16 +234,9 @@ def spawn(self):

try:
# Print something.
if not isinstance(self.nshell, list):
self.nshell = [self.nshell]
for n in self.nshell:
if self.put_text is None:
msg = MsgMap[n]['text'].format(**ColorExtraMap)
else:
msg = MsgMap[n]['text'].format(*self.put_text, **ColorExtraMap)
print(msg, flush = True)
if self.get_text:
self.plaintext.append(unshell_message(msg, m = 0)[0]["tag00"]['text'])
for num in self.nshell:
self.formatter(num)
print(self.msg, flush = True)
finally:
# Restore stdout and send content.
sys.stdout = sys.__stdout__
Expand Down
Loading

0 comments on commit dddc025

Please sign in to comment.