Skip to content

Commit

Permalink
[mod] makes pygments optional in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Psycojoker committed Mar 17, 2016
1 parent 1055678 commit 6db0114
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ will be for other releases, the workload is already big enough.
- new helper methods: .next_recursive and .previous_recursive https://redbaron.readthedocs.org/en/latest/other.html
- fix: doc is tested in CI now, it shouldn't break anymore
- more rendering test for python3, it shouldn't break anymore
- pygments is now an optional dependancy, "pip install redbaron" won't install it, "pip install redbaron[pygments"] will
- new node.next_intuitive and node.previous_intuitive methods for situations where .next/previous doesn't behave the way the user expect it http://redbaron.readthedocs.org/en/latest/other.html#next-intuitive-previous-intuitive

0.5.1 (2015-03-11)
Expand Down
24 changes: 15 additions & 9 deletions redbaron/base_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

from fnmatch import fnmatch

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter, HtmlFormatter

import baron
import baron.path
from baron.utils import python_version, string_instance
Expand All @@ -21,8 +17,12 @@
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
from redbaron.private_config import runned_from_ipython, HAS_PYGMENTS

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

This comment has been minimized.

Copy link
@ibizaman

ibizaman Mar 18, 2016

Collaborator

and what about defining something like

def highlight(string):
    return string

Instead of adding a if everywhere?

This comment has been minimized.

Copy link
@Psycojoker

Psycojoker Mar 18, 2016

Author Member

oh, nice idea :)


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

def help(self, deep=2, with_formatting=False):
if runned_from_ipython():
sys.stdout.write(highlight(self.__help__(deep=deep, with_formatting=with_formatting) + "\n", HelpLexer(), Terminal256Formatter(style='monokai')))
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")
else:
sys.stdout.write(self.__help__(deep=deep, with_formatting=with_formatting) + "\n")

Expand Down Expand Up @@ -972,9 +975,12 @@ def __repr__(self):

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

Expand Down
10 changes: 6 additions & 4 deletions redbaron/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import re

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

import baron
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


class ArgumentGeneratorComprehensionNode(Node):
Expand Down
11 changes: 11 additions & 0 deletions redbaron/private_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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
if force_ipython_behavior:
return True

if not ipython_behavior:
return False
try:
Expand Down
43 changes: 24 additions & 19 deletions redbaron/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import re
import sys

from pygments.token import Comment, Text, String, Keyword, Name, Operator
from pygments.lexer import RegexLexer, bygroups

from baron.utils import python_version

from redbaron.private_config import DEBUG
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

This comment has been minimized.

Copy link
@ibizaman

ibizaman Mar 18, 2016

Collaborator

Now that all the imports are in the if statement, maybe we could move them close the the HelpLexer class?

This comment has been minimized.

Copy link
@Psycojoker

Psycojoker Mar 18, 2016

Author Member

Imports should be at the beginning of the code, but I think I'll switch to move all of that in another file in fact.


if python_version == 3:
from io import StringIO
Expand Down Expand Up @@ -65,18 +66,22 @@ def truncate(text, n):
return "".join(truncated)


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),
]
}
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 6db0114

Please sign in to comment.