summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pygments/formatters/__init__.py17
-rw-r--r--pygments/lexers/__init__.py21
2 files changed, 30 insertions, 8 deletions
diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py
index 58de5fe3..887015c8 100644
--- a/pygments/formatters/__init__.py
+++ b/pygments/formatters/__init__.py
@@ -8,9 +8,10 @@
:license: BSD, see LICENSE for details.
"""
+import re
import sys
import types
-from fnmatch import fnmatch
+import fnmatch
from os.path import basename
from pygments.formatters._mapping import FORMATTERS
@@ -21,6 +22,16 @@ __all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
'get_all_formatters', 'load_formatter_from_file'] + list(FORMATTERS)
_formatter_cache = {} # classes by name
+_pattern_cache = {}
+
+
+def _fn_matches(fn, glob):
+ """Return whether the supplied file name fn matches pattern filename."""
+ if glob not in _pattern_cache:
+ pattern = _pattern_cache[glob] = re.compile(fnmatch.translate(glob))
+ return pattern.match(fn)
+ return _pattern_cache[glob].match(fn)
+
def _load_formatters(module_name):
"""Load a formatter (and all others in the module too)."""
@@ -111,13 +122,13 @@ def get_formatter_for_filename(fn, **options):
fn = basename(fn)
for modname, name, _, filenames, _ in FORMATTERS.values():
for filename in filenames:
- if fnmatch(fn, filename):
+ if _fn_matches(fn, filename):
if name not in _formatter_cache:
_load_formatters(modname)
return _formatter_cache[name](**options)
for cls in find_plugin_formatters():
for filename in cls.filenames:
- if fnmatch(fn, filename):
+ if _fn_matches(fn, filename):
return cls(**options)
raise ClassNotFound("no formatter found for file name %r" % fn)
diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py
index 83be0e48..79c4e9bf 100644
--- a/pygments/lexers/__init__.py
+++ b/pygments/lexers/__init__.py
@@ -8,9 +8,10 @@
:license: BSD, see LICENSE for details.
"""
+import re
import sys
import types
-from fnmatch import fnmatch
+import fnmatch
from os.path import basename
from pygments.lexers._mapping import LEXERS
@@ -27,6 +28,16 @@ __all__ = ['get_lexer_by_name', 'get_lexer_for_filename', 'find_lexer_class',
'guess_lexer', 'load_lexer_from_file'] + list(LEXERS) + list(COMPAT)
_lexer_cache = {}
+_pattern_cache = {}
+
+
+def _fn_matches(fn, glob):
+ """Return whether the supplied file name fn matches pattern filename."""
+ if glob not in _pattern_cache:
+ pattern = _pattern_cache[glob] = re.compile(fnmatch.translate(glob))
+ return pattern.match(fn)
+ return _pattern_cache[glob].match(fn)
+
def _load_lexers(module_name):
"""Load a lexer (and all others in the module too)."""
@@ -158,13 +169,13 @@ def find_lexer_class_for_filename(_fn, code=None):
fn = basename(_fn)
for modname, name, _, filenames, _ in LEXERS.values():
for filename in filenames:
- if fnmatch(fn, filename):
+ if _fn_matches(fn, filename):
if name not in _lexer_cache:
_load_lexers(modname)
matches.append((_lexer_cache[name], filename))
for cls in find_plugin_lexers():
for filename in cls.filenames:
- if fnmatch(fn, filename):
+ if _fn_matches(fn, filename):
matches.append((cls, filename))
if isinstance(code, bytes):
@@ -251,11 +262,11 @@ def guess_lexer_for_filename(_fn, _text, **options):
matching_lexers = set()
for lexer in _iter_lexerclasses():
for filename in lexer.filenames:
- if fnmatch(fn, filename):
+ if _fn_matches(fn, filename):
matching_lexers.add(lexer)
primary[lexer] = True
for filename in lexer.alias_filenames:
- if fnmatch(fn, filename):
+ if _fn_matches(fn, filename):
matching_lexers.add(lexer)
primary[lexer] = False
if not matching_lexers: