2013-09-22 23:22:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
wakatime.stats
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Stats about files
|
|
|
|
|
|
|
|
:copyright: (c) 2013 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2014-09-30 16:23:17 +00:00
|
|
|
from .compat import u, open
|
2014-12-23 11:22:49 +00:00
|
|
|
from .languages import DependencyParser
|
2014-09-30 16:23:17 +00:00
|
|
|
|
2013-09-22 23:22:11 +00:00
|
|
|
if sys.version_info[0] == 2:
|
2014-12-01 06:19:45 +00:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
|
2013-09-22 23:22:11 +00:00
|
|
|
else:
|
2014-12-01 06:19:45 +00:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
|
2015-06-16 01:27:58 +00:00
|
|
|
from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
|
|
|
|
from pygments.modeline import get_filetype_from_buffer
|
2015-06-16 01:56:44 +00:00
|
|
|
from pygments.util import ClassNotFound
|
2013-09-22 23:22:11 +00:00
|
|
|
|
|
|
|
|
2014-07-25 09:45:35 +00:00
|
|
|
log = logging.getLogger('WakaTime')
|
|
|
|
|
2013-09-22 23:22:11 +00:00
|
|
|
|
|
|
|
def guess_language(file_name):
|
2015-06-11 21:14:30 +00:00
|
|
|
"""Guess lexer and language for a file.
|
|
|
|
|
|
|
|
Returns (language, lexer) tuple where language is a unicode string.
|
|
|
|
"""
|
|
|
|
|
2015-08-11 01:34:04 +00:00
|
|
|
language = get_language_from_extension(file_name)
|
|
|
|
if language:
|
|
|
|
return language, None
|
2015-06-16 01:27:58 +00:00
|
|
|
|
2015-08-11 01:34:04 +00:00
|
|
|
lexer = smart_guess_lexer(file_name)
|
2015-08-11 03:10:39 +00:00
|
|
|
if lexer:
|
|
|
|
language = u(lexer.name)
|
2015-06-11 21:14:30 +00:00
|
|
|
|
2014-12-23 11:22:49 +00:00
|
|
|
return language, lexer
|
2013-09-22 23:22:11 +00:00
|
|
|
|
|
|
|
|
2015-06-11 21:14:30 +00:00
|
|
|
def smart_guess_lexer(file_name):
|
|
|
|
"""Guess Pygments lexer for a file.
|
|
|
|
|
|
|
|
Looks for a vim modeline in file contents, then compares the accuracy
|
|
|
|
of that lexer with a second guess. The second guess looks up all lexers
|
|
|
|
matching the file name, then runs a text analysis for the best choice.
|
|
|
|
"""
|
|
|
|
lexer = None
|
|
|
|
|
|
|
|
text = get_file_contents(file_name)
|
|
|
|
|
2015-08-11 01:34:04 +00:00
|
|
|
lexer1, accuracy1 = guess_lexer_using_filename(file_name, text)
|
|
|
|
lexer2, accuracy2 = guess_lexer_using_modeline(text)
|
2015-06-11 21:14:30 +00:00
|
|
|
|
2015-08-11 01:34:04 +00:00
|
|
|
if lexer1:
|
|
|
|
lexer = lexer1
|
|
|
|
if (lexer2 and accuracy2 and
|
|
|
|
(not accuracy1 or accuracy2 > accuracy1)):
|
|
|
|
lexer = lexer2
|
2015-06-11 21:14:30 +00:00
|
|
|
|
|
|
|
return lexer
|
|
|
|
|
|
|
|
|
2015-06-16 01:56:44 +00:00
|
|
|
def guess_lexer_using_filename(file_name, text):
|
|
|
|
"""Guess lexer for given text, limited to lexers for this file's extension.
|
|
|
|
|
|
|
|
Returns a tuple of (lexer, accuracy).
|
2015-06-11 21:14:30 +00:00
|
|
|
"""
|
|
|
|
|
2015-06-16 01:56:44 +00:00
|
|
|
lexer, accuracy = None, None
|
2015-06-16 01:27:58 +00:00
|
|
|
|
2015-06-16 01:56:44 +00:00
|
|
|
try:
|
|
|
|
lexer = guess_lexer_for_filename(file_name, text)
|
|
|
|
except:
|
|
|
|
pass
|
2015-06-16 01:27:58 +00:00
|
|
|
|
2015-06-16 01:56:44 +00:00
|
|
|
if lexer is not None:
|
|
|
|
try:
|
|
|
|
accuracy = lexer.analyse_text(text)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return lexer, accuracy
|
2015-06-16 01:27:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def guess_lexer_using_modeline(text):
|
|
|
|
"""Guess lexer for given text using Vim modeline.
|
2015-06-16 01:56:44 +00:00
|
|
|
|
|
|
|
Returns a tuple of (lexer, accuracy).
|
2015-06-16 01:27:58 +00:00
|
|
|
"""
|
|
|
|
|
2015-06-16 01:56:44 +00:00
|
|
|
lexer, accuracy = None, None
|
2015-06-16 01:27:58 +00:00
|
|
|
|
2015-07-03 21:25:22 +00:00
|
|
|
file_type = None
|
|
|
|
try:
|
|
|
|
file_type = get_filetype_from_buffer(text)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2015-06-16 01:27:58 +00:00
|
|
|
if file_type is not None:
|
|
|
|
try:
|
2015-06-16 01:56:44 +00:00
|
|
|
lexer = get_lexer_by_name(file_type)
|
|
|
|
except ClassNotFound:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if lexer is not None:
|
|
|
|
try:
|
|
|
|
accuracy = lexer.analyse_text(text)
|
2015-06-16 01:27:58 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2015-06-16 01:56:44 +00:00
|
|
|
return lexer, accuracy
|
|
|
|
|
|
|
|
|
2015-08-11 01:34:04 +00:00
|
|
|
def get_language_from_extension(file_name):
|
|
|
|
"""Returns a matching language for the given file extension.
|
2015-06-16 01:56:44 +00:00
|
|
|
"""
|
|
|
|
|
2015-08-11 01:34:04 +00:00
|
|
|
extension = os.path.splitext(file_name)[1].lower()
|
|
|
|
if extension == '.h':
|
|
|
|
directory = os.path.dirname(file_name)
|
|
|
|
available_files = os.listdir(directory)
|
|
|
|
available_extensions = zip(*map(os.path.splitext, available_files))[1]
|
|
|
|
available_extensions = [ext.lower() for ext in available_extensions]
|
|
|
|
if '.cpp' in available_extensions:
|
|
|
|
return 'C++'
|
|
|
|
if '.c' in available_extensions:
|
|
|
|
return 'C'
|
2015-06-16 01:56:44 +00:00
|
|
|
|
2013-10-26 04:32:30 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-22 23:22:11 +00:00
|
|
|
def number_lines_in_file(file_name):
|
|
|
|
lines = 0
|
|
|
|
try:
|
2014-09-30 16:23:17 +00:00
|
|
|
with open(file_name, 'r', encoding='utf-8') as fh:
|
|
|
|
for line in fh:
|
2013-09-22 23:22:11 +00:00
|
|
|
lines += 1
|
2014-11-04 06:51:05 +00:00
|
|
|
except:
|
2015-08-25 07:51:01 +00:00
|
|
|
try:
|
|
|
|
with open(file_name, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
|
|
|
for line in fh:
|
|
|
|
lines += 1
|
|
|
|
except:
|
|
|
|
return None
|
2013-09-22 23:22:11 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2015-05-12 22:02:09 +00:00
|
|
|
def get_file_stats(file_name, notfile=False, lineno=None, cursorpos=None):
|
2015-02-13 03:06:40 +00:00
|
|
|
if notfile:
|
|
|
|
stats = {
|
|
|
|
'language': None,
|
|
|
|
'dependencies': [],
|
|
|
|
'lines': None,
|
2015-05-12 22:02:09 +00:00
|
|
|
'lineno': lineno,
|
|
|
|
'cursorpos': cursorpos,
|
2015-02-13 03:06:40 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
language, lexer = guess_language(file_name)
|
|
|
|
parser = DependencyParser(file_name, lexer)
|
|
|
|
dependencies = parser.parse()
|
|
|
|
stats = {
|
|
|
|
'language': language,
|
|
|
|
'dependencies': dependencies,
|
|
|
|
'lines': number_lines_in_file(file_name),
|
2015-05-12 22:02:09 +00:00
|
|
|
'lineno': lineno,
|
|
|
|
'cursorpos': cursorpos,
|
2015-02-13 03:06:40 +00:00
|
|
|
}
|
2013-09-22 23:22:11 +00:00
|
|
|
return stats
|
2015-06-11 21:14:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_file_contents(file_name):
|
|
|
|
"""Returns the first 512000 bytes of the file's contents.
|
|
|
|
"""
|
|
|
|
|
|
|
|
text = None
|
|
|
|
try:
|
|
|
|
with open(file_name, 'r', encoding='utf-8') as fh:
|
|
|
|
text = fh.read(512000)
|
|
|
|
except:
|
2015-08-25 07:51:01 +00:00
|
|
|
try:
|
|
|
|
with open(file_name, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
|
|
|
text = fh.read(512000)
|
|
|
|
except:
|
|
|
|
log.exception("Exception:")
|
2015-06-11 21:14:30 +00:00
|
|
|
return text
|