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
评论列表
文章目录