Skip to content

Commit

Permalink
[mod] refactor, encapsulate pygments optional dependancy in a module
Browse files Browse the repository at this point in the history
  • Loading branch information
Psycojoker committed Mar 18, 2016
1 parent 4be526f commit feb52fa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 58 deletions.
24 changes: 6 additions & 18 deletions redbaron/base_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

import redbaron

from redbaron.utils import redbaron_classname_to_baron_type, baron_type_to_redbaron_classname, log, in_a_shell, indent, truncate, HelpLexer
from redbaron.private_config import runned_from_ipython, HAS_PYGMENTS
from redbaron.utils import redbaron_classname_to_baron_type, baron_type_to_redbaron_classname, log, in_a_shell, indent, truncate
from redbaron.private_config import runned_from_ipython
from redbaron.syntax_highlight import help_highlight, python_highlight, python_html_highlight

if HAS_PYGMENTS:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter, HtmlFormatter

if python_version == 3:
from collections import UserList
Expand Down Expand Up @@ -920,10 +917,7 @@ def dumps(self):

def help(self, deep=2, with_formatting=False):
if runned_from_ipython():
if HAS_PYGMENTS:
sys.stdout.write(highlight(self.__help__(deep=deep, with_formatting=with_formatting) + "\n", HelpLexer(), Terminal256Formatter(style='monokai')))
else:
sys.stdout.write(self.__help__(deep=deep, with_formatting=with_formatting) + "\n")
sys.stdout.write(help_highlight(self.__help__(deep=deep, with_formatting=with_formatting) + "\n"))
else:
sys.stdout.write(self.__help__(deep=deep, with_formatting=with_formatting) + "\n")

Expand Down Expand Up @@ -975,18 +969,12 @@ def __repr__(self):

def __str__(self):
if runned_from_ipython():
if HAS_PYGMENTS:
return highlight(self.dumps(), PythonLexer(encoding="Utf-8"),
Terminal256Formatter(style='monokai',
encoding="Utf-8"))
else:
return self.dumps()
return python_highlight(self.dumps())
else:
return self.dumps()

def _bytes_repr_html_(self):
return highlight(self.dumps(), PythonLexer(encode="Utf-8"),
HtmlFormatter(noclasses=True, encoding="UTf-8"))
return python_html_highlight(self.dumps())

def _repr_html_(self):
return self._bytes_repr_html_().decode("Utf-8")
Expand Down
10 changes: 2 additions & 8 deletions redbaron/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
from baron.utils import string_instance

from redbaron.base_nodes import Node, NodeList, LiteralyEvaluable, CodeBlockNode, DotProxyList, CommaProxyList, LineProxyList, IfElseBlockSiblingNode, ElseAttributeNode
from redbaron.private_config import HAS_PYGMENTS

if HAS_PYGMENTS:
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from redbaron.syntax_highlight import python_html_highlight


class ArgumentGeneratorComprehensionNode(Node):
Expand Down Expand Up @@ -434,8 +429,7 @@ def __repr__(self):
return repr(baron.dumps([self.fst()]))

def _bytes_repr_html_(self):
return highlight(self.__repr__(), PythonLexer(encode="Utf-8"),
HtmlFormatter(noclasses=True, encoding="UTf-8"))
return python_html_highlight(self.__repr__())


class ExceptNode(CodeBlockNode):
Expand Down
6 changes: 0 additions & 6 deletions redbaron/private_config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
ipython_behavior = True
force_ipython_behavior = False
DEBUG = False
HAS_PYGMENTS = True

try:
import pygments
except ImportError:
HAS_PYGMENTS = False

def runned_from_ipython():
# for testing
Expand Down
50 changes: 50 additions & 0 deletions redbaron/syntax_highlight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
HAS_PYGMENTS = True

try:
import pygments
except ImportError:
HAS_PYGMENTS = False


if HAS_PYGMENTS:
from pygments.token import Comment, Text, String, Keyword, Name, Operator
from pygments.lexer import RegexLexer, bygroups
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter, HtmlFormatter


class HelpLexer(RegexLexer):
name = 'Lexer for RedBaron .help() method output'

tokens = {
'root': [
(r"#.*$", Comment),
(r"('([^\\']|\\.)*'|\"([^\\\"]|\\.)*\")", String),
(r"(None|False|True)", String),
(r'(\*)( \w+Node)', bygroups(Operator, Keyword)),
(r'\w+Node', Name.Function),
(r'(\*|=|->|\(|\)|\.\.\.)', Operator),
(r'\w+', Text),
(r'\s+', Text),
]
}

def help_highlight(string):
return highlight(string, HelpLexer(), Terminal256Formatter(style='monokai'))

def python_highlight(string):
return highlight(string, PythonLexer(encoding="Utf-8"),
Terminal256Formatter(style='monokai',
encoding="Utf-8"))

def python_html_highlight(string):
return highlight(string, PythonLexer(encode="Utf-8"),
HtmlFormatter(noclasses=True, encoding="UTf-8"))

else:
def help_highlight(string):
return string

def python_highlight(string):
return string
27 changes: 1 addition & 26 deletions redbaron/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@

from baron.utils import python_version

from redbaron.private_config import DEBUG, HAS_PYGMENTS

if HAS_PYGMENTS:
from pygments.token import Comment, Text, String, Keyword, Name, Operator
from pygments.lexer import RegexLexer, bygroups
from redbaron.private_config import DEBUG

if python_version == 3:
from io import StringIO
Expand Down Expand Up @@ -64,24 +60,3 @@ def truncate(text, n):
truncated[-3:-1] = ['.', '.', '.']
del truncated[n-4 : -4]
return "".join(truncated)


if HAS_PYGMENTS:
class HelpLexer(RegexLexer):
name = 'Lexer for RedBaron .help() method output'

tokens = {
'root': [
(r"#.*$", Comment),
(r"('([^\\']|\\.)*'|\"([^\\\"]|\\.)*\")", String),
(r"(None|False|True)", String),
(r'(\*)( \w+Node)', bygroups(Operator, Keyword)),
(r'\w+Node', Name.Function),
(r'(\*|=|->|\(|\)|\.\.\.)', Operator),
(r'\w+', Text),
(r'\s+', Text),
]
}
else:
class HelpLexer(object):
pass

0 comments on commit feb52fa

Please sign in to comment.