Merge pull request #2741 from xonsh/pygments_all_styles

Pygments get_all_styles
This commit is contained in:
Anthony Scopatz 2018-07-22 13:58:31 -04:00 committed by GitHub
commit dd42b8e10b
Failed to generate hash of commit
5 changed files with 38 additions and 16 deletions

14
news/pygments_cache.rst Normal file
View file

@ -0,0 +1,14 @@
**Added:** None
**Changed:**
* Use the ``pygments_cache.get_all_styles()`` function instead of
interacting directly with pygments.
**Deprecated:** None
**Removed:** None
**Fixed:** None
**Security:** None

View file

@ -1,11 +1,10 @@
[tool:pytest]
flake8-max-line-length = 180
flake8-exclude =
xonsh/pygments_cache.py
flake8-ignore =
*.py E122
*.py E402
tests/tools.py E128
xonsh/pygments_cache.py ALL
# flake8 gives incorrect unused import errors, F401
xonsh/ast.py F401
xonsh/platform.py F401

View file

@ -20,6 +20,7 @@ from xonsh.shell import transform_command
from xonsh.platform import HAS_PYGMENTS, ON_WINDOWS
from xonsh.style_tools import partial_color_tokenize, _TokenType, DEFAULT_STYLE_DICT
from xonsh.lazyimps import pygments, pyghooks, winutils
from xonsh.pygments_cache import get_all_styles
Token = _TokenType()
@ -279,7 +280,6 @@ class PromptToolkitShell(BaseShell):
"""Returns an iterable of all available style names."""
if not HAS_PYGMENTS:
return ['For other xonsh styles, please install pygments']
from pygments.styles import get_all_styles
return get_all_styles()
def color_style(self):

View file

@ -10,6 +10,7 @@ from xonsh.tools import print_exception, carriage_return
from xonsh.platform import HAS_PYGMENTS, ON_WINDOWS
from xonsh.style_tools import partial_color_tokenize, _TokenType, DEFAULT_STYLE_DICT
from xonsh.lazyimps import pygments, pyghooks, winutils
from xonsh.pygments_cache import get_all_styles
from xonsh.ptk2.history import PromptToolkitHistory
from xonsh.ptk2.completer import PromptToolkitCompleter
from xonsh.ptk2.key_bindings import load_xonsh_bindings
@ -282,7 +283,6 @@ class PromptToolkit2Shell(BaseShell):
"""Returns an iterable of all available style names."""
if not HAS_PYGMENTS:
return ['For other xonsh styles, please install pygments']
from pygments.styles import get_all_styles
return get_all_styles()
def color_style(self):

View file

@ -4,7 +4,7 @@ The following pygments API functions are currently supplied here::
from pygments_cache import get_lexer_for_filename, guess_lexer_for_filename
from pygments_cache import get_formatter_for_filename, get_formatter_by_name
from pygments_cache import get_style_by_name
from pygments_cache import get_style_by_name, get_all_styles
from pygments_cache import get_filter_by_name
The cache itself is stored at the location given by the ``$PYGMENTS_CACHE_FILE``
@ -94,7 +94,7 @@ def _discover_lexers():
'.sql': ('pygments.lexers.sql', 'SqlLexer'),
'.txt': ('pygments.lexers.special', 'TextLexer'),
'.html': ('pygments.lexers.html', 'HtmlLexer'),
}
}
exts = {}
lexers = {'exts': exts}
if DEBUG:
@ -109,8 +109,8 @@ def _discover_lexers():
filename = filename[1:]
if '*' in filename:
continue
if (DEBUG and filename in exts and exts[filename] != val and
filename not in default_exts):
if (DEBUG and filename in exts and exts[filename] != val
and filename not in default_exts):
duplicates[filename].add(val)
duplicates[filename].add(exts[filename])
exts[filename] = val
@ -144,16 +144,16 @@ def _discover_formatters():
filename = filename[1:]
if '*' in filename:
continue
if (DEBUG and filename in exts and exts[filename] != val and
filename not in default_exts):
if (DEBUG and filename in exts and exts[filename] != val
and filename not in default_exts):
duplicates[filename].add(val)
duplicates[filename].add(exts[filename])
exts[filename] = val
# add names and aliases
names[cls.name] = val
for alias in cls.aliases:
if (DEBUG and alias in names and names[alias] != val and
alias not in default_names):
if (DEBUG and alias in names and names[alias] != val
and alias not in default_names):
duplicates[alias].add(val)
duplicates[alias].add(names[alias])
names[alias] = val
@ -180,8 +180,8 @@ def _discover_styles():
cls = get_style_by_name(name)
mod = inspect.getmodule(cls)
val = (mod.__name__, cls.__name__)
if (DEBUG and name in names and names[name] != val and
name not in default_names):
if (DEBUG and name in names and names[name] != val
and name not in default_names):
duplicates[name].add(val)
duplicates[name].add(names[name])
names[name] = val
@ -208,8 +208,8 @@ def _discover_filters():
cls = type(filter)
mod = inspect.getmodule(cls)
val = (mod.__name__, cls.__name__)
if (DEBUG and name in names and names[name] != val and
name not in default_names):
if (DEBUG and name in names and names[name] != val
and name not in default_names):
duplicates[name].add(val)
duplicates[name].add(names[name])
names[name] = val
@ -392,6 +392,15 @@ def get_style_by_name(name):
return style
def get_all_styles():
"""Iterable through all known style names.
This mimics the behavior of ``pygments.styles.get_all_styles``.
"""
if CACHE is None:
load_or_build()
yield from CACHE['styles']['names']
def get_filter_by_name(filtername, **options):
"""Gets a filter instance from its name. This mimics the behavior of
``pygments.filters.get_filtere_by_name()``.