Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color log #587

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 73 additions & 7 deletions saspy/sasbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
except Exception as e:
pass

try:
from pygments.formatters import HtmlFormatter
from pygments import highlight
from saspy.SASLogLexer import SASLogStyle, SASLogLexer
except:
pass

import os
import sys
import datetime
Expand Down Expand Up @@ -193,6 +200,7 @@ def __init__(self, **kwargs):
provider = cfg.get('provider')
self.display = cfg.get('display', '')
self.results = cfg.get('results')
self.colorLOG = cfg.get('colorLOG', False)
self.autoexec = cfg.get('autoexec')

bcv = kwargs.get('SAS_BCV', getattr(SAScfg, "SAS_BCV", SASPy_CUR_VER))
Expand Down Expand Up @@ -277,6 +285,17 @@ def __init__(self, **kwargs):
else:
provider = inprov

incLOG = kwargs.get('colorLOG', None)
if incLOG is not None:
self.colorLOG = bool(incLOG)
if self.colorLOG:
try:
if SASLogLexer:
self.colorLOG = True
except:
logger.warning("Pygments module wasn't found so coloring isn't available.")
self.colorLOG = False

if java is not None:
self.mode = 'IOM'
elif url is not None:
Expand Down Expand Up @@ -746,7 +765,16 @@ def submitLOG(self, code, results: str = '', prompt: dict = None, printto=False,
'''
This method is a convenience wrapper around the submit() method. It executes the submit then prints the LOG that was returned.
'''
print(self.submit(code, results, prompt, printto, **kwargs)['LOG'])
clog = self.sascfg.colorLOG
self.sascfg.colorLOG = False
log = self.submit(code, results, prompt, printto, **kwargs)['LOG']
self.sascfg.colorLOG = clog

if self.sascfg.colorLOG:
clog = highlight(log, SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
self.DISPLAY(self.HTML(clog))
else:
print(log)

def submitLST(self, code, results: str = '', prompt: dict = None, method: str = None, printto=False, **kwargs):
'''
Expand All @@ -772,7 +800,10 @@ def submitLST(self, code, results: str = '', prompt: dict = None, method: str =
else:
results = self.results

clog = self.sascfg.colorLOG
self.sascfg.colorLOG = False
ll = self.submit(code, results, prompt, printto, **kwargs)
self.sascfg.colorLOG = clog

if results.upper() == 'HTML':
if method.lower() == 'listonly':
Expand All @@ -781,23 +812,54 @@ def submitLST(self, code, results: str = '', prompt: dict = None, method: str =
if len(ll['LST']) > 0:
self.DISPLAY(self.HTML(ll['LST']))
else:
self.DISPLAY(self.HTML("<pre>"+ll['LOG']+"</pre>"))
if self.sascfg.colorLOG:
clog = highlight(ll['LOG'], SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
self.DISPLAY(self.HTML(clog))
else:
print(ll['LOG'])
elif method.lower() == 'listandlog':
self.DISPLAY(self.HTML(ll['LST']+"\n<pre>"+ll['LOG']+"</pre>"))
if self.sascfg.colorLOG:
clog = highlight(ll['LOG'], SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
self.DISPLAY(self.HTML(ll['LST']))
self.DISPLAY(self.HTML(clog))
else:
#self.DISPLAY(self.HTML(ll['LST']+"\n<pre>"+ll['LOG']+"</pre>"))
self.DISPLAY(self.HTML(ll['LST']))
print(ll['LOG'])
else:
self.DISPLAY(self.HTML("<pre>"+ll['LOG']+"\n</pre>"+ll['LST']))
if self.sascfg.colorLOG:
clog = highlight(ll['LOG'], SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
self.DISPLAY(self.HTML(clog))
self.DISPLAY(self.HTML(ll['LST']))
else:
print(ll['LOG'])
self.DISPLAY(self.HTML(ll['LST']))
else:
if method.lower() == 'listonly':
print(ll['LST'])
elif method.lower() == 'listorlog':
if len(ll['LST']) > 0:
print(ll['LST'])
else:
print(ll['LOG'])
if self.sascfg.colorLOG:
clog = highlight(ll['LOG'], SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
self.DISPLAY(self.HTML(clog))
else:
print(ll['LOG'])
elif method.lower() == 'listandlog':
print(ll['LST']+"\n"+ll['LOG'])
if self.sascfg.colorLOG:
clog = highlight(ll['LOG'], SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
print(ll['LST'])
self.DISPLAY(self.HTML(clog))
else:
print(ll['LST']+"\n"+ll['LOG'])
else:
print(ll['LOG']+"\n"+ll['LST'])
if self.sascfg.colorLOG:
clog = highlight(ll['LOG'], SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
self.DISPLAY(self.HTML(clog))
print(ll['LST'])
else:
print(ll['LOG']+"\n"+ll['LST'])

def submit(self, code: str, results: str = '', prompt: dict = None, printto=False, **kwargs) -> dict:
'''
Expand Down Expand Up @@ -864,6 +926,10 @@ def submit(self, code: str, results: str = '', prompt: dict = None, printto=Fals

ll = self._io.submit(code, results, prompt, undo=printto, **kwargs)

if self.sascfg.colorLOG:
clog = highlight(ll['LOG'], SASLogLexer(), HtmlFormatter(full=True, style=SASLogStyle, lineseparator="<br>"))
ll['LOG'] = clog

return ll

def saslog(self) -> str:
Expand Down
Loading