diff options
author | Georg Brandl <georg@python.org> | 2017-01-22 19:57:19 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2017-01-22 19:57:19 +0100 |
commit | 1f75c51afdbe281e0044ca5c5369e14fc5e0e8e1 (patch) | |
tree | c790ff3b447d5c41e2e50cdd5d18b0071772ee9b | |
parent | 041b80fab1922800ae0618fd4d3d8d78bf7063b4 (diff) | |
download | pygments-1f75c51afdbe281e0044ca5c5369e14fc5e0e8e1.tar.gz |
-x functionality updates, Python 3 compatibility fix
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | pygments/cmdline.py | 2 | ||||
-rw-r--r-- | pygments/formatters/__init__.py | 10 | ||||
-rw-r--r-- | pygments/lexers/__init__.py | 10 | ||||
-rw-r--r-- | tests/support/empty.py | 1 | ||||
-rw-r--r-- | tests/support/html_formatter.py | 5 | ||||
-rw-r--r-- | tests/support/python_lexer.py | 11 |
8 files changed, 26 insertions, 18 deletions
@@ -155,6 +155,7 @@ Other contributors, listed alphabetically, are: * Dominik Picheta -- Nimrod lexer * Andrew Pinkham -- RTF Formatter Refactoring * Clément Prévost -- UrbiScript lexer +* Tanner Prynn -- cmdline -x option and loading lexers from files * Oleh Prypin -- Crystal lexer (based on Ruby lexer) * Elias Rabel -- Fortran fixed form lexer * raichoo -- Idris lexer @@ -29,6 +29,10 @@ Version 2.2 * Crystal (PR#576) * Snowball (PR#589) +- Added the ability to load lexer and formatter classes directly from files + with the `-x` command line option and the `lexers.load_lexer_from_file()` + and `formatters.load_formatter_from_file()` functions. (PR#559) + - Added `lexers.find_lexer_class_by_name()`. (#1203) - Added new token types and lexing for magic methods and variables in Python diff --git a/pygments/cmdline.py b/pygments/cmdline.py index 98c3ec37..5e1f39e2 100644 --- a/pygments/cmdline.py +++ b/pygments/cmdline.py @@ -433,7 +433,7 @@ def main_inner(popts, args, usage): if ':' in fmter: file, fmtername = fmter.rsplit(':', 1) fmter = load_formatter_from_file(file, fmtername, - **parsed_opts) + **parsed_opts) else: fmter = load_formatter_from_file(fmter, **parsed_opts) except ClassNotFound as err: diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py index 4ea79c5a..965c5f1a 100644 --- a/pygments/formatters/__init__.py +++ b/pygments/formatters/__init__.py @@ -91,14 +91,16 @@ def load_formatter_from_file(filename, formattername="CustomFormatter", Users should be very careful with the input, because this method is equivalent to running eval on the input file. - Raises ClassNotFound if there are any problems importing the Formatter + Raises ClassNotFound if there are any problems importing the Formatter. + + .. versionadded:: 2.2 """ try: # This empty dict will contain the namespace for the exec'd file custom_namespace = {} - exec(open(filename, 'r'), custom_namespace) + exec(open(filename, 'rb').read(), custom_namespace) # Retrieve the class `formattername` from that namespace - if not formattername in custom_namespace: + if formattername not in custom_namespace: raise ClassNotFound('no valid %s class found in %s' % (formattername, filename)) formatter_class = custom_namespace[formattername] @@ -107,7 +109,7 @@ def load_formatter_from_file(filename, formattername="CustomFormatter", except IOError as err: raise ClassNotFound('cannot read %s' % filename) except ClassNotFound as err: - raise err + raise except Exception as err: raise ClassNotFound('error when loading custom formatter: %s' % err) diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py index d5e02e49..328e072c 100644 --- a/pygments/lexers/__init__.py +++ b/pygments/lexers/__init__.py @@ -126,14 +126,16 @@ def load_lexer_from_file(filename, lexername="CustomLexer", **options): Users should be very careful with the input, because this method is equivalent to running eval on the input file. - Raises ClassNotFound if there are any problems importing the Lexer + Raises ClassNotFound if there are any problems importing the Lexer. + + .. versionadded:: 2.2 """ try: # This empty dict will contain the namespace for the exec'd file custom_namespace = {} - exec(open(filename, 'r'), custom_namespace) + exec(open(filename, 'rb').read(), custom_namespace) # Retrieve the class `lexername` from that namespace - if not lexername in custom_namespace: + if lexername not in custom_namespace: raise ClassNotFound('no valid %s class found in %s' % (lexername, filename)) lexer_class = custom_namespace[lexername] @@ -142,7 +144,7 @@ def load_lexer_from_file(filename, lexername="CustomLexer", **options): except IOError as err: raise ClassNotFound('cannot read %s' % filename) except ClassNotFound as err: - raise err + raise except Exception as err: raise ClassNotFound('error when loading custom lexer: %s' % err) diff --git a/tests/support/empty.py b/tests/support/empty.py index e69de29b..40a96afc 100644 --- a/tests/support/empty.py +++ b/tests/support/empty.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/support/html_formatter.py b/tests/support/html_formatter.py index 7f5581ed..169cd4af 100644 --- a/tests/support/html_formatter.py +++ b/tests/support/html_formatter.py @@ -1,5 +1,6 @@ - +# -*- coding: utf-8 -*- from pygments.formatters import HtmlFormatter + class HtmlFormatterWrapper(HtmlFormatter): - name = 'HtmlWrapper' + name = 'HtmlWrapper' diff --git a/tests/support/python_lexer.py b/tests/support/python_lexer.py index ad34d31b..565ee674 100644 --- a/tests/support/python_lexer.py +++ b/tests/support/python_lexer.py @@ -1,15 +1,12 @@ # -*- coding: utf-8 -*- -""" - pygments.lexers.python (as CustomLexer) - ~~~~~~~~~~~~~~~~~~~~~~ - - For test_cmdline.py -""" +# pygments.lexers.python (as CustomLexer) for test_cmdline.py from pygments.lexers import PythonLexer + class CustomLexer(PythonLexer): name = 'PythonLexerWrapper' + class LexerWrapper(CustomLexer): - name="PythonLexerWrapperWrapper" + name = 'PythonLexerWrapperWrapper' |