[PATCH] pygmentsutils: don't fail if an extension doesn't speficy EXTRA_LEXERS

Mads Kiilerich mads at kiilerich.com
Wed Apr 5 00:16:04 UTC 2017


On 04/04/2017 04:55 PM, Andrew Shadura wrote:
> # HG changeset patch
> # User Andrew Shadura <andrew at shadura.me>
> # Date 1491317675 -7200
> #      Tue Apr 04 16:54:35 2017 +0200
> # Node ID 860e1f4e02146a9f5731ed426f6f36419e2f4110
> # Parent  f58ed40c9a72ce0b72fa7ce4f8b7da843430bc57
> pygmentsutils: don't fail if an extension doesn't speficy EXTRA_LEXERS
>
> diff --git a/kallithea/lib/pygmentsutils.py b/kallithea/lib/pygmentsutils.py
> --- a/kallithea/lib/pygmentsutils.py
> +++ b/kallithea/lib/pygmentsutils.py
> @@ -87,6 +87,6 @@ def get_custom_lexer(extension):
>       """
>       import kallithea
>       #check if we didn't define this extension as other lexer
> -    if kallithea.EXTENSIONS and extension in kallithea.EXTENSIONS.EXTRA_LEXERS:
> +    if kallithea.EXTENSIONS and hasattr(kallithea.EXTENSIONS, 'EXTRA_LEXERS') and extension in kallithea.EXTENSIONS.EXTRA_LEXERS:
>           _lexer_name = kallithea.EXTENSIONS.EXTRA_LEXERS[extension]
>           return lexers.get_lexer_by_name(_lexer_name)

That's a lot of checking before doing. I wonder if it would be cleaner 
to do something like:

_lexer_name = getattr(kallithea.EXTENSIONS, 'EXTRA_LEXERS', 
{}).get(extension)
if _lexer_name is not None:
     ...

If you agree, I can tweak it to that.

/Mads


More information about the kallithea-general mailing list