# HG changeset patch # User Søren Løvborg # Date 1470151465 -7200 # Node ID 27e6594645f1a449b74e5db3cec05e2d6bb421ec # Parent 7ea933acdb8bb7541df6b3cb7627f1a057906c8b pygmentsutils: separate from util2 __get_lem and __get_index_filenames has nothing in common with the rest of util2, neither with respects to implementation nor usage, and belongs in a dedicated module. Also, they are clearly not actually private, so shouldn't be named with leading underscores. diff -r 7ea933acdb8b -r 27e6594645f1 kallithea/config/conf.py --- a/kallithea/config/conf.py Fri Jun 24 20:12:05 2016 +0200 +++ b/kallithea/config/conf.py Tue Aug 02 17:24:25 2016 +0200 @@ -25,12 +25,12 @@ :license: GPLv3, see LICENSE.md for more details. """ -from kallithea.lib.utils2 import __get_lem, __get_index_filenames +from kallithea.lib import pygmentsutils # language map is also used by whoosh indexer, which for those specified # extensions will index it's content -LANGUAGES_EXTENSIONS_MAP = __get_lem() +LANGUAGES_EXTENSIONS_MAP = pygmentsutils.get_lem() # Whoosh index targets @@ -38,7 +38,7 @@ INDEX_EXTENSIONS = LANGUAGES_EXTENSIONS_MAP.keys() # Filenames we want to index content of using whoosh -INDEX_FILENAMES = __get_index_filenames() +INDEX_FILENAMES = pygmentsutils.get_index_filenames() # list of readme files to search in file tree and display in summary # attached weights defines the search order lower is first diff -r 7ea933acdb8b -r 27e6594645f1 kallithea/lib/pygmentsutils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kallithea/lib/pygmentsutils.py Tue Aug 02 17:24:25 2016 +0200 @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +kallithea.lib.pygmentsutils +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Functions for extracting internal Pygments data. + +This file was forked by the Kallithea project in July 2014. +Original author and date, and relevant copyright and licensing information is below: +:created_on: Jan 5, 2011 +:author: marcink +:copyright: (c) 2013 RhodeCode GmbH, and others. +:license: GPLv3, see LICENSE.md for more details. +""" + +from collections import defaultdict +from itertools import ifilter +from string import lower + +from pygments import lexers + + +def get_lem(): + """ + Get language extension map based on what's inside pygments lexers + """ + d = defaultdict(lambda: []) + + def __clean(s): + s = s.lstrip('*') + s = s.lstrip('.') + + if s.find('[') != -1: + exts = [] + start, stop = s.find('['), s.find(']') + + for suffix in s[start + 1:stop]: + exts.append(s[:s.find('[')] + suffix) + return map(lower, exts) + else: + return map(lower, [s]) + + for lx, t in sorted(lexers.LEXERS.items()): + m = map(__clean, t[-2]) + if m: + m = reduce(lambda x, y: x + y, m) + for ext in m: + desc = lx.replace('Lexer', '') + d[ext].append(desc) + + return dict(d) + + +def get_index_filenames(): + """ + Get list of known indexable filenames from pygment lexer internals + """ + + filenames = [] + + def likely_filename(s): + return s.find('*') == -1 and s.find('[') == -1 + + for lx, t in sorted(lexers.LEXERS.items()): + for f in ifilter(likely_filename, t[-2]): + filenames.append(f) + + return filenames diff -r 7ea933acdb8b -r 27e6594645f1 kallithea/lib/utils2.py --- a/kallithea/lib/utils2.py Fri Jun 24 20:12:05 2016 +0200 +++ b/kallithea/lib/utils2.py Tue Aug 02 17:24:25 2016 +0200 @@ -43,60 +43,6 @@ from kallithea.lib.compat import json -def __get_lem(): - """ - Get language extension map based on what's inside pygments lexers - """ - from pygments import lexers - from string import lower - from collections import defaultdict - - d = defaultdict(lambda: []) - - def __clean(s): - s = s.lstrip('*') - s = s.lstrip('.') - - if s.find('[') != -1: - exts = [] - start, stop = s.find('['), s.find(']') - - for suffix in s[start + 1:stop]: - exts.append(s[:s.find('[')] + suffix) - return map(lower, exts) - else: - return map(lower, [s]) - - for lx, t in sorted(lexers.LEXERS.items()): - m = map(__clean, t[-2]) - if m: - m = reduce(lambda x, y: x + y, m) - for ext in m: - desc = lx.replace('Lexer', '') - d[ext].append(desc) - - return dict(d) - - -def __get_index_filenames(): - """ - Get list of known indexable filenames from pygment lexer internals - """ - from pygments import lexers - from itertools import ifilter - - filenames = [] - - def likely_filename(s): - return s.find('*') == -1 and s.find('[') == -1 - - for lx, t in sorted(lexers.LEXERS.items()): - for f in ifilter(likely_filename, t[-2]): - filenames.append(f) - - return filenames - - def str2bool(_str): """ returns True/False value from given string, it tries to translate the