Skip to content

Commit

Permalink
style.py file format and UMR message colors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dalthviz committed Nov 20, 2024
1 parent 584cc8d commit db71627
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 63 deletions.
11 changes: 3 additions & 8 deletions spyder_kernels/console/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from spyder_kernels.utils.mpl import automatic_backend, MPL_BACKENDS_TO_SPYDER
from spyder_kernels.utils.nsview import (
get_remote_data, make_remote_view, get_size)
from spyder_kernels.utils.style import create_style_class
from spyder_kernels.console.shell import SpyderShell
from spyder_kernels.comms.utils import WriteContext

Expand Down Expand Up @@ -659,17 +660,13 @@ def set_configuration(self, conf):
return ret

def set_color_scheme(self, color_scheme):
if color_scheme == "dark":
# Needed to change the colors of tracebacks
self.shell.run_line_magic("colors", "linux")
elif color_scheme == "light":
self.shell.run_line_magic("colors", "lightbg")
self.shell.set_spyder_theme(color_scheme)
self.set_sympy_forecolor(background_color=color_scheme)
self.set_traceback_highlighting(color_scheme)

def set_traceback_highlighting(self, color_scheme):
"""Set the traceback highlighting color."""
color = 'bg:ansigreen' if color_scheme == 'dark' else 'bg:ansiyellow'
color = 'bg:ansired' if color_scheme == 'dark' else 'bg:ansiyellow'
from IPython.core.ultratb import VerboseTB

if getattr(VerboseTB, 'tb_highlight', None) is not None:
Expand All @@ -682,8 +679,6 @@ def set_traceback_syntax_highlighting(self, syntax_style):
import IPython.core.ultratb
from IPython.core.ultratb import VerboseTB

from spyder_kernels.utils.style import create_style_class

IPython.core.ultratb.get_style_by_name = create_style_class

if getattr(VerboseTB, 'tb_highlight_style', None) is not None:
Expand Down
14 changes: 14 additions & 0 deletions spyder_kernels/console/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(self, *args, **kwargs):
self._allow_kbdint = False
self.register_debugger_sigint()
self.update_gui_frontend = False
self._spyder_theme = 'dark'

# register post_execute
self.events.register('post_execute', self.do_post_execute)
Expand Down Expand Up @@ -84,6 +85,19 @@ def _showtraceback(self, etype, evalue, stb):
stb = ['']
super(SpyderShell, self)._showtraceback(etype, evalue, stb)

def set_spyder_theme(self, theme):
"""Set the theme for the console."""
self._spyder_theme = theme
if theme == "dark":
# Needed to change the colors of tracebacks
self.run_line_magic("colors", "linux")
elif theme == "light":
self.run_line_magic("colors", "lightbg")

def get_spyder_theme(self):
"""Get the theme for the console."""
return self._spyder_theme

def enable_matplotlib(self, gui=None):
"""Enable matplotlib."""
if gui is None or gui.lower() == "auto":
Expand Down
5 changes: 3 additions & 2 deletions spyder_kernels/customize/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ class SpyderCodeRunner(Magics):
def __init__(self, *args, **kwargs):
self.show_global_msg = True
self.show_invalid_syntax_msg = True
super().__init__(*args, **kwargs)
self.umr = UserModuleReloader(
namelist=os.environ.get("SPY_UMR_NAMELIST", None)
namelist=os.environ.get("SPY_UMR_NAMELIST", None),
shell=self.shell,
)
super().__init__(*args, **kwargs)

@runfile_arguments
@needs_local_scope
Expand Down
11 changes: 8 additions & 3 deletions spyder_kernels/customize/umr.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UserModuleReloader:
namelist [list]: blacklist in terms of module name
"""

def __init__(self, namelist=None, pathlist=None):
def __init__(self, namelist=None, pathlist=None, shell=None):
if namelist is None:
namelist = []
else:
Expand All @@ -45,6 +45,7 @@ def __init__(self, namelist=None, pathlist=None):
self.namelist = namelist + spy_modules + mpl_modules + other_modules

self.pathlist = pathlist
self._shell = shell

# List of previously loaded modules
self.previous_modules = list(sys.modules.keys())
Expand Down Expand Up @@ -92,7 +93,11 @@ def run(self):
# Report reloaded modules
if self.verbose and modnames_to_reload:
modnames = modnames_to_reload
print("\x1b[1;4;31m%s\x1b[24m%s\x1b[0m"
% ("Reloaded modules", ": "+", ".join(modnames)))
colors = {"dark": "33", "light": "31"}
color = colors["dark"]
if self._shell:
color = colors[self._shell.get_spyder_theme()]
print("\x1b[4;%sm%s\x1b[24m%s\x1b[0m"
% (color, "Reloaded modules", ": "+", ".join(modnames)))

return modnames_to_reload
120 changes: 70 additions & 50 deletions spyder_kernels/utils/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@

# Third party imports
from pygments.style import Style
from pygments.token import (Name, Keyword, Comment, String, Number,
Punctuation, Operator)
from pygments.token import (
Name,
Keyword,
Comment,
String,
Number,
Punctuation,
Operator,
)


def create_pygments_dict(color_scheme_dict):
Expand All @@ -24,86 +31,99 @@ def create_pygments_dict(color_scheme_dict):

def give_font_weight(is_bold):
if is_bold:
return 'bold'
return "bold"
else:
return ''
return ""

def give_font_style(is_italic):
if is_italic:
return 'italic'
return "italic"
else:
return ''
return ""

color_scheme = color_scheme_dict

fon_c, fon_fw, fon_fs = color_scheme['normal']
font_color = fon_c
fon_c, fon_fw, fon_fs = color_scheme["normal"]
font_color = fon_c
font_font_weight = give_font_weight(fon_fw)
font_font_style = give_font_style(fon_fs)

key_c, key_fw, key_fs = color_scheme['keyword']
keyword_color = key_c
key_c, key_fw, key_fs = color_scheme["keyword"]
keyword_color = key_c
keyword_font_weight = give_font_weight(key_fw)
keyword_font_style = give_font_style(key_fs)

bui_c, bui_fw, bui_fs = color_scheme['builtin']
builtin_color = bui_c
bui_c, bui_fw, bui_fs = color_scheme["builtin"]
builtin_color = bui_c
builtin_font_weight = give_font_weight(bui_fw)
builtin_font_style = give_font_style(bui_fs)

str_c, str_fw, str_fs = color_scheme['string']
string_color = str_c
str_c, str_fw, str_fs = color_scheme["string"]
string_color = str_c
string_font_weight = give_font_weight(str_fw)
string_font_style = give_font_style(str_fs)

num_c, num_fw, num_fs = color_scheme['number']
number_color = num_c
num_c, num_fw, num_fs = color_scheme["number"]
number_color = num_c
number_font_weight = give_font_weight(num_fw)
number_font_style = give_font_style(num_fs)

com_c, com_fw, com_fs = color_scheme['comment']
comment_color = com_c
com_c, com_fw, com_fs = color_scheme["comment"]
comment_color = com_c
comment_font_weight = give_font_weight(com_fw)
comment_font_style = give_font_style(com_fs)

def_c, def_fw, def_fs = color_scheme['definition']
definition_color = def_c
def_c, def_fw, def_fs = color_scheme["definition"]
definition_color = def_c
definition_font_weight = give_font_weight(def_fw)
definition_font_style = give_font_style(def_fs)

ins_c, ins_fw, ins_fs = color_scheme['instance']
instance_color = ins_c
ins_c, ins_fw, ins_fs = color_scheme["instance"]
instance_color = ins_c
instance_font_weight = give_font_weight(ins_fw)
instance_font_style = give_font_style(ins_fs)

font_token = font_font_style + ' ' + font_font_weight + ' ' + font_color
definition_token = (definition_font_style + ' ' + definition_font_weight +
' ' + definition_color)
builtin_token = (builtin_font_style + ' ' + builtin_font_weight + ' ' +
builtin_color)
instance_token = (instance_font_style + ' ' + instance_font_weight + ' ' +
instance_color)
keyword_token = (keyword_font_style + ' ' + keyword_font_weight + ' ' +
keyword_color)
comment_token = (comment_font_style + ' ' + comment_font_weight + ' ' +
comment_color)
string_token = (string_font_style + ' ' + string_font_weight + ' ' +
string_color)
number_token = (number_font_style + ' ' + number_font_weight + ' ' +
number_color)

syntax_style_dic = {Name: font_token.strip(),
Name.Class: definition_token.strip(),
Name.Function: definition_token.strip(),
Name.Builtin: builtin_token.strip(),
Name.Builtin.Pseudo: instance_token.strip(),
Keyword: keyword_token.strip(),
Keyword.Type: builtin_token.strip(),
Comment: comment_token.strip(),
String: string_token.strip(),
Number: number_token.strip(),
Punctuation: font_token.strip(),
Operator.Word: keyword_token.strip()}
font_token = font_font_style + " " + font_font_weight + " " + font_color
definition_token = (
definition_font_style
+ " "
+ definition_font_weight
+ " "
+ definition_color
)
builtin_token = (
builtin_font_style + " " + builtin_font_weight + " " + builtin_color
)
instance_token = (
instance_font_style + " " + instance_font_weight + " " + instance_color
)
keyword_token = (
keyword_font_style + " " + keyword_font_weight + " " + keyword_color
)
comment_token = (
comment_font_style + " " + comment_font_weight + " " + comment_color
)
string_token = (
string_font_style + " " + string_font_weight + " " + string_color
)
number_token = (
number_font_style + " " + number_font_weight + " " + number_color
)

syntax_style_dic = {
Name: font_token.strip(),
Name.Class: definition_token.strip(),
Name.Function: definition_token.strip(),
Name.Builtin: builtin_token.strip(),
Name.Builtin.Pseudo: instance_token.strip(),
Keyword: keyword_token.strip(),
Keyword.Type: builtin_token.strip(),
Comment: comment_token.strip(),
String: string_token.strip(),
Number: number_token.strip(),
Punctuation: font_token.strip(),
Operator.Word: keyword_token.strip(),
}

return syntax_style_dic

Expand Down

0 comments on commit db71627

Please sign in to comment.