From 903e4ccc05b71e725f0bd505eb5491ca70f48e40 Mon Sep 17 00:00:00 2001 From: Benjamin Attal Date: Mon, 14 Apr 2014 12:51:42 -0400 Subject: [PATCH] Add constants in bitshift/config.py for languages instead of just strings. --- bitshift/config.py | 10 ++++++++++ bitshift/parser/__init__.py | 9 +++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bitshift/config.py b/bitshift/config.py index 6c7be42..e69e367 100644 --- a/bitshift/config.py +++ b/bitshift/config.py @@ -4,3 +4,13 @@ Module to contain definitions of all Flask variables required by the app module. DEBUG = True SECRET_KEY = "\x89\x87\x9a9\xab{\xda\xfe.28\xb4\x18\x01\x95]]\xd2\xeaen\xe0Ot" + +LANG_PYTHON = 0 +LANG_C = 1 +LANG_JAVA = 2 + +PYG_IDS = { + "Python": LANG_PYTHON, + "C": LANG_C, + "JAVA": LANG_JAVA +} diff --git a/bitshift/parser/__init__.py b/bitshift/parser/__init__.py index 03d812c..3220a73 100644 --- a/bitshift/parser/__init__.py +++ b/bitshift/parser/__init__.py @@ -1,6 +1,7 @@ from .python import parse_py from .c import parse_c from .java import parse_java +from bitshift.config import LANG_PYTHON, LANG_C, LANG_JAVA, PYG_IDS import pygments.lexers as pgl _all__ = ["parse"] @@ -17,7 +18,7 @@ def _lang(codelet): if codelet.filename is not None: return pgl.guess_lexer_for_filename(codelet.filename).name - return pgl.guess_lexer(codelet.code) + return PYG_IDS[pgl.guess_lexer(codelet.code)] def parser(codelet): """ @@ -29,10 +30,10 @@ def parser(codelet): """ lang = _lang(codelet) - if lang == "Python": + if lang == LANG_PYTHON: parse_py(codelet) - elif lang == "C": + elif lang == LANG_C: parse_c(codelet) - elif lang == "Java": + elif lang == LANG_JAVA: parse_java(codelet)