From 9247705bd8cb8da3381c3e30045cb5d739ddce33 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 27 Jan 2022 10:38:55 +0000 Subject: [PATCH 1/3] Allow override of lexer in Syntax.from_path --- rich/syntax.py | 8 ++++++-- tests/test_syntax.py | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/rich/syntax.py b/rich/syntax.py index e912daaa1..d3b14bca3 100644 --- a/rich/syntax.py +++ b/rich/syntax.py @@ -267,6 +267,7 @@ def from_path( cls, path: str, encoding: str = "utf-8", + lexer: Optional[Union[Lexer, str]] = None, theme: Union[str, SyntaxTheme] = DEFAULT_THEME, dedent: bool = False, line_numbers: bool = False, @@ -283,6 +284,7 @@ def from_path( Args: path (str): Path to file to highlight. + lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content. encoding (str): Encoding of file. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs". dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True. @@ -302,11 +304,12 @@ def from_path( with open(path, "rt", encoding=encoding) as code_file: code = code_file.read() - lexer_name = cls.guess_lexer(path, code=code) + if not lexer: + lexer = cls.guess_lexer(path, code=code) return cls( code, - lexer_name, + lexer, theme=theme, dedent=dedent, line_numbers=line_numbers, @@ -757,6 +760,7 @@ def __rich_console__( else: syntax = Syntax.from_path( args.path, + lexer=args.lexer_name, line_numbers=args.line_numbers, word_wrap=args.word_wrap, theme=args.theme, diff --git a/tests/test_syntax.py b/tests/test_syntax.py index a8ac05b6b..46d0126e1 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -241,8 +241,13 @@ def test_ansi_theme(): assert theme.get_background_style() == Style() -@pytest.mark.skipif(sys.platform == "win32", reason="permissions error on Windows") -def test_from_file(): +skip_windows_permission_error = pytest.mark.skipif( + sys.platform == "win32", reason="permissions error on Windows" +) + + +@skip_windows_permission_error +def test_from_path(): fh, path = tempfile.mkstemp("example.py") try: os.write(fh, b"import this\n") @@ -254,8 +259,8 @@ def test_from_file(): os.remove(path) -@pytest.mark.skipif(sys.platform == "win32", reason="permissions error on Windows") -def test_from_file_unknown_lexer(): +@skip_windows_permission_error +def test_from_path_unknown_lexer(): fh, path = tempfile.mkstemp("example.nosuchtype") try: os.write(fh, b"import this\n") @@ -266,6 +271,30 @@ def test_from_file_unknown_lexer(): os.remove(path) +@skip_windows_permission_error +def test_from_path_lexer_override(): + fh, path = tempfile.mkstemp("example.nosuchtype") + try: + os.write(fh, b"import this\n") + syntax = Syntax.from_path(path, lexer="rust") + assert syntax.lexer.name is "Rust" + assert syntax.code == "import this\n" + finally: + os.remove(path) + + +@skip_windows_permission_error +def test_from_path_lexer_override_invalid_lexer(): + fh, path = tempfile.mkstemp("example.nosuchtype") + try: + os.write(fh, b"import this\n") + syntax = Syntax.from_path(path, lexer="blah") + assert syntax.lexer is None + assert syntax.code == "import this\n" + finally: + os.remove(path) + + def test_syntax_guess_lexer(): assert Syntax.guess_lexer("banana.py") == "python" assert Syntax.guess_lexer("banana.py", "import this") == "python" From 8c41358fb13bf135067d331d231fd244b7b787d9 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 27 Jan 2022 10:40:12 +0000 Subject: [PATCH 2/3] Add Pull Request to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3e81f724..26469ec5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Workaround for edge case of object from Faiss with no `__class__` https://github.com/Textualize/rich/issues/1838 - Add `Syntax.guess_lexer`, add support for more lexers (e.g. Django templates etc.) https://github.com/Textualize/rich/pull/1869 +- Add `lexer` parameter to `Syntax.from_path` to allow for overrides https://github.com/Textualize/rich/pull/1873 ### Added From 308f433f4e04ac1835d7c720c4cb1d749b2adc8b Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 27 Jan 2022 10:42:54 +0000 Subject: [PATCH 3/3] Fix ordering in docstring --- rich/syntax.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rich/syntax.py b/rich/syntax.py index d3b14bca3..1be36fd87 100644 --- a/rich/syntax.py +++ b/rich/syntax.py @@ -284,8 +284,8 @@ def from_path( Args: path (str): Path to file to highlight. - lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content. encoding (str): Encoding of file. + lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs". dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.