Skip to content

Commit 98ef53a

Browse files
committed
style.py file format and UMR message colors handling
1 parent 584cc8d commit 98ef53a

File tree

5 files changed

+97
-62
lines changed

5 files changed

+97
-62
lines changed

spyder_kernels/console/kernel.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from spyder_kernels.utils.mpl import automatic_backend, MPL_BACKENDS_TO_SPYDER
4848
from spyder_kernels.utils.nsview import (
4949
get_remote_data, make_remote_view, get_size)
50+
from spyder_kernels.utils.style import create_style_class
5051
from spyder_kernels.console.shell import SpyderShell
5152
from spyder_kernels.comms.utils import WriteContext
5253

@@ -659,17 +660,13 @@ def set_configuration(self, conf):
659660
return ret
660661

661662
def set_color_scheme(self, color_scheme):
662-
if color_scheme == "dark":
663-
# Needed to change the colors of tracebacks
664-
self.shell.run_line_magic("colors", "linux")
665-
elif color_scheme == "light":
666-
self.shell.run_line_magic("colors", "lightbg")
663+
self.shell.set_spyder_theme(color_scheme)
667664
self.set_sympy_forecolor(background_color=color_scheme)
668665
self.set_traceback_highlighting(color_scheme)
669666

670667
def set_traceback_highlighting(self, color_scheme):
671668
"""Set the traceback highlighting color."""
672-
color = 'bg:ansigreen' if color_scheme == 'dark' else 'bg:ansiyellow'
669+
color = 'bg:ansired' if color_scheme == 'dark' else 'bg:ansiyellow'
673670
from IPython.core.ultratb import VerboseTB
674671

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

685-
from spyder_kernels.utils.style import create_style_class
686-
687682
IPython.core.ultratb.get_style_by_name = create_style_class
688683

689684
if getattr(VerboseTB, 'tb_highlight_style', None) is not None:

spyder_kernels/console/shell.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __init__(self, *args, **kwargs):
5656
self._allow_kbdint = False
5757
self.register_debugger_sigint()
5858
self.update_gui_frontend = False
59+
self._spyder_theme = 'dark'
5960

6061
# register post_execute
6162
self.events.register('post_execute', self.do_post_execute)
@@ -84,6 +85,19 @@ def _showtraceback(self, etype, evalue, stb):
8485
stb = ['']
8586
super(SpyderShell, self)._showtraceback(etype, evalue, stb)
8687

88+
def set_spyder_theme(self, theme):
89+
"""Set the theme for the console."""
90+
self._spyder_theme = theme
91+
if theme == "dark":
92+
# Needed to change the colors of tracebacks
93+
self.shell.run_line_magic("colors", "linux")
94+
elif theme == "light":
95+
self.shell.run_line_magic("colors", "lightbg")
96+
97+
def get_spyder_theme(self):
98+
"""Get the theme for the console."""
99+
return self._spyder_theme
100+
87101
def enable_matplotlib(self, gui=None):
88102
"""Enable matplotlib."""
89103
if gui is None or gui.lower() == "auto":

spyder_kernels/customize/code_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ def __init__(self, *args, **kwargs):
146146
self.show_global_msg = True
147147
self.show_invalid_syntax_msg = True
148148
self.umr = UserModuleReloader(
149-
namelist=os.environ.get("SPY_UMR_NAMELIST", None)
149+
namelist=os.environ.get("SPY_UMR_NAMELIST", None),
150+
shell=self.shell,
150151
)
151152
super().__init__(*args, **kwargs)
152153

spyder_kernels/customize/umr.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class UserModuleReloader:
2020
namelist [list]: blacklist in terms of module name
2121
"""
2222

23-
def __init__(self, namelist=None, pathlist=None):
23+
def __init__(self, namelist=None, pathlist=None, shell=None):
2424
if namelist is None:
2525
namelist = []
2626
else:
@@ -45,6 +45,7 @@ def __init__(self, namelist=None, pathlist=None):
4545
self.namelist = namelist + spy_modules + mpl_modules + other_modules
4646

4747
self.pathlist = pathlist
48+
self._shell = shell
4849

4950
# List of previously loaded modules
5051
self.previous_modules = list(sys.modules.keys())
@@ -92,7 +93,11 @@ def run(self):
9293
# Report reloaded modules
9394
if self.verbose and modnames_to_reload:
9495
modnames = modnames_to_reload
95-
print("\x1b[1;4;31m%s\x1b[24m%s\x1b[0m"
96-
% ("Reloaded modules", ": "+", ".join(modnames)))
96+
colors = {"dark": "31", "light": "33"}
97+
color = colors["dark"]
98+
if self._shell:
99+
color = colors[self._shell.get_spyder_theme()]
100+
print("\x1b[1;4;%sm%s\x1b[24m%s\x1b[0m"
101+
% (color, "Reloaded modules", ": "+", ".join(modnames)))
97102

98103
return modnames_to_reload

spyder_kernels/utils/style.py

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212

1313
# Third party imports
1414
from pygments.style import Style
15-
from pygments.token import (Name, Keyword, Comment, String, Number,
16-
Punctuation, Operator)
15+
from pygments.token import (
16+
Name,
17+
Keyword,
18+
Comment,
19+
String,
20+
Number,
21+
Punctuation,
22+
Operator,
23+
)
1724

1825

1926
def create_pygments_dict(color_scheme_dict):
@@ -24,86 +31,99 @@ def create_pygments_dict(color_scheme_dict):
2431

2532
def give_font_weight(is_bold):
2633
if is_bold:
27-
return 'bold'
34+
return "bold"
2835
else:
29-
return ''
36+
return ""
3037

3138
def give_font_style(is_italic):
3239
if is_italic:
33-
return 'italic'
40+
return "italic"
3441
else:
35-
return ''
42+
return ""
3643

3744
color_scheme = color_scheme_dict
3845

39-
fon_c, fon_fw, fon_fs = color_scheme['normal']
40-
font_color = fon_c
46+
fon_c, fon_fw, fon_fs = color_scheme["normal"]
47+
font_color = fon_c
4148
font_font_weight = give_font_weight(fon_fw)
4249
font_font_style = give_font_style(fon_fs)
4350

44-
key_c, key_fw, key_fs = color_scheme['keyword']
45-
keyword_color = key_c
51+
key_c, key_fw, key_fs = color_scheme["keyword"]
52+
keyword_color = key_c
4653
keyword_font_weight = give_font_weight(key_fw)
4754
keyword_font_style = give_font_style(key_fs)
4855

49-
bui_c, bui_fw, bui_fs = color_scheme['builtin']
50-
builtin_color = bui_c
56+
bui_c, bui_fw, bui_fs = color_scheme["builtin"]
57+
builtin_color = bui_c
5158
builtin_font_weight = give_font_weight(bui_fw)
5259
builtin_font_style = give_font_style(bui_fs)
5360

54-
str_c, str_fw, str_fs = color_scheme['string']
55-
string_color = str_c
61+
str_c, str_fw, str_fs = color_scheme["string"]
62+
string_color = str_c
5663
string_font_weight = give_font_weight(str_fw)
5764
string_font_style = give_font_style(str_fs)
5865

59-
num_c, num_fw, num_fs = color_scheme['number']
60-
number_color = num_c
66+
num_c, num_fw, num_fs = color_scheme["number"]
67+
number_color = num_c
6168
number_font_weight = give_font_weight(num_fw)
6269
number_font_style = give_font_style(num_fs)
6370

64-
com_c, com_fw, com_fs = color_scheme['comment']
65-
comment_color = com_c
71+
com_c, com_fw, com_fs = color_scheme["comment"]
72+
comment_color = com_c
6673
comment_font_weight = give_font_weight(com_fw)
6774
comment_font_style = give_font_style(com_fs)
6875

69-
def_c, def_fw, def_fs = color_scheme['definition']
70-
definition_color = def_c
76+
def_c, def_fw, def_fs = color_scheme["definition"]
77+
definition_color = def_c
7178
definition_font_weight = give_font_weight(def_fw)
7279
definition_font_style = give_font_style(def_fs)
7380

74-
ins_c, ins_fw, ins_fs = color_scheme['instance']
75-
instance_color = ins_c
81+
ins_c, ins_fw, ins_fs = color_scheme["instance"]
82+
instance_color = ins_c
7683
instance_font_weight = give_font_weight(ins_fw)
7784
instance_font_style = give_font_style(ins_fs)
7885

79-
font_token = font_font_style + ' ' + font_font_weight + ' ' + font_color
80-
definition_token = (definition_font_style + ' ' + definition_font_weight +
81-
' ' + definition_color)
82-
builtin_token = (builtin_font_style + ' ' + builtin_font_weight + ' ' +
83-
builtin_color)
84-
instance_token = (instance_font_style + ' ' + instance_font_weight + ' ' +
85-
instance_color)
86-
keyword_token = (keyword_font_style + ' ' + keyword_font_weight + ' ' +
87-
keyword_color)
88-
comment_token = (comment_font_style + ' ' + comment_font_weight + ' ' +
89-
comment_color)
90-
string_token = (string_font_style + ' ' + string_font_weight + ' ' +
91-
string_color)
92-
number_token = (number_font_style + ' ' + number_font_weight + ' ' +
93-
number_color)
94-
95-
syntax_style_dic = {Name: font_token.strip(),
96-
Name.Class: definition_token.strip(),
97-
Name.Function: definition_token.strip(),
98-
Name.Builtin: builtin_token.strip(),
99-
Name.Builtin.Pseudo: instance_token.strip(),
100-
Keyword: keyword_token.strip(),
101-
Keyword.Type: builtin_token.strip(),
102-
Comment: comment_token.strip(),
103-
String: string_token.strip(),
104-
Number: number_token.strip(),
105-
Punctuation: font_token.strip(),
106-
Operator.Word: keyword_token.strip()}
86+
font_token = font_font_style + " " + font_font_weight + " " + font_color
87+
definition_token = (
88+
definition_font_style
89+
+ " "
90+
+ definition_font_weight
91+
+ " "
92+
+ definition_color
93+
)
94+
builtin_token = (
95+
builtin_font_style + " " + builtin_font_weight + " " + builtin_color
96+
)
97+
instance_token = (
98+
instance_font_style + " " + instance_font_weight + " " + instance_color
99+
)
100+
keyword_token = (
101+
keyword_font_style + " " + keyword_font_weight + " " + keyword_color
102+
)
103+
comment_token = (
104+
comment_font_style + " " + comment_font_weight + " " + comment_color
105+
)
106+
string_token = (
107+
string_font_style + " " + string_font_weight + " " + string_color
108+
)
109+
number_token = (
110+
number_font_style + " " + number_font_weight + " " + number_color
111+
)
112+
113+
syntax_style_dic = {
114+
Name: font_token.strip(),
115+
Name.Class: definition_token.strip(),
116+
Name.Function: definition_token.strip(),
117+
Name.Builtin: builtin_token.strip(),
118+
Name.Builtin.Pseudo: instance_token.strip(),
119+
Keyword: keyword_token.strip(),
120+
Keyword.Type: builtin_token.strip(),
121+
Comment: comment_token.strip(),
122+
String: string_token.strip(),
123+
Number: number_token.strip(),
124+
Punctuation: font_token.strip(),
125+
Operator.Word: keyword_token.strip(),
126+
}
107127

108128
return syntax_style_dic
109129

0 commit comments

Comments
 (0)