我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用pygments.lexers.ClassNotFound()。
def _get_lexer(self, lang): if lang.lower() == 'sql': return get_lexer_by_name('postgresql', **self.options) tries = [lang] if lang.startswith('pl'): tries.append(lang[2:]) if lang.endswith('u'): tries.append(lang[:-1]) if lang.startswith('pl') and lang.endswith('u'): tries.append(lang[2:-1]) for l in tries: try: return get_lexer_by_name(l, **self.options) except ClassNotFound: pass else: # TODO: better logging # print >>sys.stderr, "language not found:", lang return None
def _get_lexer(self, lang): if lang.lower() == 'sql': return get_lexer_by_name('postgresql', **self.options) tries = [ lang ] if lang.startswith('pl'): tries.append(lang[2:]) if lang.endswith('u'): tries.append(lang[:-1]) if lang.startswith('pl') and lang.endswith('u'): tries.append(lang[2:-1]) for l in tries: try: return get_lexer_by_name(l, **self.options) except ClassNotFound: pass else: # TODO: better logging # print >>sys.stderr, "language not found:", lang return None
def guess_code_present(filepath): """Guess whether a file contains "code" or not. Structured text (as listed in NOT_CODE) does not count as "code", but anything else that the Pygments lexer-guesser finds a probable lexer for counts as "code" for these purposes. Parameters ---------- filepath : string Path to the file that may contain code. Returns ------- boolean True if the file contains "code" (as a best guess), False otherwise """ text = get_file_text(filepath) filename = os.path.split(filepath)[1] try: lexer = lexers.guess_lexer_for_filename(filename, text) if lexer.name not in NOT_CODE: return True else: return False except lexers.ClassNotFound: return False