def add_filter(self, filter_, **options): """ Add a new stream filter to this lexer. """ if not isinstance(filter_, Filter): filter_ = get_filter_by_name(filter_, **options) self.filters.append(filter_)
def add_filter(self, filter_, **options): """ Add a new stream filter to this lexer. """ if not isinstance(filter_, Filter): filter_ = get_filter_by_name(filter_, **options) self.filters.append(filter_)
def _discover_filters(): import inspect from pygments.filters import get_all_filters, get_filter_by_name # maps filter 'name' (not the class name) to (module, classname) tuples default_names = {} names = {} filters = {"names": names} if DEBUG: from collections import defaultdict duplicates = defaultdict(set) for name in get_all_filters(): filter = get_filter_by_name(name) cls = type(filter) mod = inspect.getmodule(cls) val = (mod.__name__, cls.__name__) if DEBUG and name in names and names[ name] != val and name not in default_names: duplicates[name].add(val) duplicates[name].add(names[name]) names[name] = val # remove some ambiquity names.update(default_names) # print dumplicate message if DEBUG: _print_duplicate_message(duplicates) return filters
def get_filter_by_name(filtername, **options): """Gets a filter instance from its name. This mimics the behavior of ``pygments.filters.get_filtere_by_name()``. """ if CACHE is None: load_or_build() names = CACHE["filters"]["names"] if filtername in names: modname, clsname = names[filtername] mod = importlib.import_module(modname) cls = getattr(mod, clsname) filter = cls(**options) else: # couldn't find style in cache, fallback to the hard way import inspect from pygments.filters import get_filter_by_name filter = get_filter_by_name(filtername, **options) # add this filter to the cache for future use cls = type(filter) mod = inspect.getmodule(cls) names[filtername] = (mod.__name__, cls.__name__) write_cache(cache_filename()) return filter
def _discover_filters(): import inspect from pygments.filters import get_all_filters, get_filter_by_name # maps filter 'name' (not the class name) to (module, classname) tuples default_names = {} names = {} filters = {"names": names} if DEBUG: from collections import defaultdict duplicates = defaultdict(set) for name in get_all_filters(): filter = get_filter_by_name(name) cls = type(filter) mod = inspect.getmodule(cls) val = (mod.__name__, cls.__name__) if DEBUG and name in names and names[name] != val and name not in default_names: duplicates[name].add(val) duplicates[name].add(names[name]) names[name] = val # remove some ambiquity names.update(default_names) # print dumplicate message if DEBUG: _print_duplicate_message(duplicates) return filters
def get_filter_by_name(filtername, **options): """Gets a filter instance from its name. This mimics the behavior of ``pygments.filters.get_filtere_by_name()``. """ if CACHE is None: load_or_build() names = CACHE["filters"]["names"] if filtername in names: modname, clsname = names[filtername] mod = importlib.import_module(modname) cls = getattr(mod, clsname) filter = cls(**options) else: # couldn't find style in cache, fallback to the hard way import inspect from pygments.filters import get_filter_by_name filter = get_filter_by_name(filtername, **options) # add this filter to the cache for future use cls = type(filter) mod = inspect.getmodule(cls) names[filtername] = (mod.__name__, cls.__name__) write_cache(cache_filename()) return filter