2013-09-22 20:39:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
wakatime.stats
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
2013-09-22 20:41:13 +00:00
|
|
|
Stats about files
|
2013-09-22 20:39:16 +00:00
|
|
|
|
|
|
|
:copyright: (c) 2013 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2014-09-30 16:09:30 +00:00
|
|
|
from .compat import u, open
|
2015-09-27 02:26:15 +00:00
|
|
|
from .dependencies import DependencyParser
|
2014-09-30 16:09:30 +00:00
|
|
|
|
2017-02-20 21:22:14 +00:00
|
|
|
from .packages.pygments.lexers import (
|
|
|
|
ClassNotFound,
|
2017-02-20 22:23:02 +00:00
|
|
|
find_lexer_class,
|
2016-04-18 21:27:41 +00:00
|
|
|
get_lexer_by_name,
|
|
|
|
guess_lexer_for_filename,
|
|
|
|
)
|
2017-02-20 21:22:14 +00:00
|
|
|
from .packages.pygments.modeline import get_filetype_from_buffer
|
2013-09-22 20:39:16 +00:00
|
|
|
|
2016-04-19 10:30:44 +00:00
|
|
|
try:
|
|
|
|
from .packages import simplejson as json # pragma: nocover
|
|
|
|
except (ImportError, SyntaxError): # pragma: nocover
|
|
|
|
import json
|
|
|
|
|
2013-09-22 20:39:16 +00:00
|
|
|
|
2014-07-25 07:59:25 +00:00
|
|
|
log = logging.getLogger('WakaTime')
|
2013-09-22 20:39:16 +00:00
|
|
|
|
|
|
|
|
2017-02-20 21:22:14 +00:00
|
|
|
def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None,
|
2017-02-20 23:29:44 +00:00
|
|
|
plugin=None, language=None):
|
2017-02-20 21:22:14 +00:00
|
|
|
if entity_type != 'file':
|
|
|
|
stats = {
|
|
|
|
'language': None,
|
|
|
|
'dependencies': [],
|
|
|
|
'lines': None,
|
|
|
|
'lineno': lineno,
|
|
|
|
'cursorpos': cursorpos,
|
|
|
|
}
|
|
|
|
else:
|
2017-02-20 23:29:44 +00:00
|
|
|
language = standardize_language(language, plugin)
|
2017-02-20 22:23:02 +00:00
|
|
|
lexer = get_lexer(language)
|
|
|
|
|
|
|
|
if not language:
|
|
|
|
language, lexer = guess_language(file_name)
|
|
|
|
|
2017-02-20 21:22:14 +00:00
|
|
|
parser = DependencyParser(file_name, lexer)
|
|
|
|
dependencies = parser.parse()
|
2017-02-20 22:23:02 +00:00
|
|
|
|
2017-02-20 21:22:14 +00:00
|
|
|
stats = {
|
|
|
|
'language': language,
|
|
|
|
'dependencies': dependencies,
|
|
|
|
'lines': number_lines_in_file(file_name),
|
|
|
|
'lineno': lineno,
|
|
|
|
'cursorpos': cursorpos,
|
|
|
|
}
|
|
|
|
return stats
|
|
|
|
|
|
|
|
|
2017-02-20 22:23:02 +00:00
|
|
|
def get_lexer(language):
|
|
|
|
"""Return a Pygments Lexer object for the given language string."""
|
|
|
|
|
|
|
|
if not language:
|
|
|
|
return None
|
|
|
|
|
|
|
|
lexer_cls = find_lexer_class(language)
|
|
|
|
if lexer_cls:
|
|
|
|
return lexer_cls()
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-22 20:39:16 +00:00
|
|
|
def guess_language(file_name):
|
2015-06-11 21:11:10 +00:00
|
|
|
"""Guess lexer and language for a file.
|
|
|
|
|
|
|
|
Returns (language, lexer) tuple where language is a unicode string.
|
|
|
|
"""
|
|
|
|
|
2015-08-10 18:33:39 +00:00
|
|
|
language = get_language_from_extension(file_name)
|
|
|
|
lexer = smart_guess_lexer(file_name)
|
2015-09-29 09:18:54 +00:00
|
|
|
if language is None and lexer is not None:
|
2015-08-11 03:10:09 +00:00
|
|
|
language = u(lexer.name)
|
2015-06-11 21:11:10 +00:00
|
|
|
|
2014-12-23 05:12:57 +00:00
|
|
|
return language, lexer
|
2013-09-22 20:39:16 +00:00
|
|
|
|
|
|
|
|
2015-06-11 21:11:10 +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
|
|
|
|
|
2016-04-19 10:30:44 +00:00
|
|
|
text = get_file_head(file_name)
|
2015-06-11 21:11:10 +00:00
|
|
|
|
2015-08-10 18:33:39 +00:00
|
|
|
lexer1, accuracy1 = guess_lexer_using_filename(file_name, text)
|
|
|
|
lexer2, accuracy2 = guess_lexer_using_modeline(text)
|
2015-06-11 21:11:10 +00:00
|
|
|
|
2015-08-10 18:33:39 +00:00
|
|
|
if lexer1:
|
|
|
|
lexer = lexer1
|
|
|
|
if (lexer2 and accuracy2 and
|
|
|
|
(not accuracy1 or accuracy2 > accuracy1)):
|
2015-09-07 03:46:57 +00:00
|
|
|
lexer = lexer2 # pragma: nocover
|
2015-06-11 21:11:10 +00:00
|
|
|
|
|
|
|
return lexer
|
|
|
|
|
|
|
|
|
2015-06-16 01:55:47 +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:11:10 +00:00
|
|
|
"""
|
|
|
|
|
2015-06-16 01:55:47 +00:00
|
|
|
lexer, accuracy = None, None
|
2015-06-13 00:25:04 +00:00
|
|
|
|
2015-06-16 01:55:47 +00:00
|
|
|
try:
|
|
|
|
lexer = guess_lexer_for_filename(file_name, text)
|
2017-02-20 21:22:14 +00:00
|
|
|
except:
|
2015-06-16 01:55:47 +00:00
|
|
|
pass
|
2015-06-13 00:25:04 +00:00
|
|
|
|
2015-06-16 01:55:47 +00:00
|
|
|
if lexer is not None:
|
|
|
|
try:
|
|
|
|
accuracy = lexer.analyse_text(text)
|
2017-02-20 21:22:14 +00:00
|
|
|
except:
|
2015-06-16 01:55:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
return lexer, accuracy
|
2013-10-26 04:28:59 +00:00
|
|
|
|
|
|
|
|
2015-06-16 01:26:10 +00:00
|
|
|
def guess_lexer_using_modeline(text):
|
|
|
|
"""Guess lexer for given text using Vim modeline.
|
2015-06-16 01:55:47 +00:00
|
|
|
|
|
|
|
Returns a tuple of (lexer, accuracy).
|
2015-06-16 01:26:10 +00:00
|
|
|
"""
|
|
|
|
|
2015-06-16 01:55:47 +00:00
|
|
|
lexer, accuracy = None, None
|
2015-06-16 01:26:10 +00:00
|
|
|
|
2015-07-02 19:36:45 +00:00
|
|
|
file_type = None
|
|
|
|
try:
|
|
|
|
file_type = get_filetype_from_buffer(text)
|
2015-09-07 03:46:57 +00:00
|
|
|
except: # pragma: nocover
|
2015-07-02 19:36:45 +00:00
|
|
|
pass
|
|
|
|
|
2015-06-16 01:26:10 +00:00
|
|
|
if file_type is not None:
|
|
|
|
try:
|
2015-06-16 01:55:47 +00:00
|
|
|
lexer = get_lexer_by_name(file_type)
|
2016-06-15 19:09:40 +00:00
|
|
|
except ClassNotFound:
|
2015-06-16 01:55:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
if lexer is not None:
|
|
|
|
try:
|
|
|
|
accuracy = lexer.analyse_text(text)
|
2015-09-07 03:46:57 +00:00
|
|
|
except: # pragma: nocover
|
2015-06-16 01:26:10 +00:00
|
|
|
pass
|
|
|
|
|
2015-06-16 01:55:47 +00:00
|
|
|
return lexer, accuracy
|
|
|
|
|
|
|
|
|
2015-08-10 18:33:39 +00:00
|
|
|
def get_language_from_extension(file_name):
|
|
|
|
"""Returns a matching language for the given file extension.
|
2015-06-16 01:55:47 +00:00
|
|
|
"""
|
|
|
|
|
2015-09-07 03:06:36 +00:00
|
|
|
filepart, extension = os.path.splitext(file_name)
|
|
|
|
|
|
|
|
if os.path.exists(u('{0}{1}').format(u(filepart), u('.c'))) or os.path.exists(u('{0}{1}').format(u(filepart), u('.C'))):
|
|
|
|
return 'C'
|
|
|
|
|
|
|
|
extension = extension.lower()
|
2015-08-11 01:17:50 +00:00
|
|
|
if extension == '.h':
|
|
|
|
directory = os.path.dirname(file_name)
|
|
|
|
available_files = os.listdir(directory)
|
2015-09-07 03:19:22 +00:00
|
|
|
available_extensions = list(zip(*map(os.path.splitext, available_files)))[1]
|
2015-08-11 01:17:50 +00:00
|
|
|
available_extensions = [ext.lower() for ext in available_extensions]
|
|
|
|
if '.cpp' in available_extensions:
|
2015-08-10 18:33:39 +00:00
|
|
|
return 'C++'
|
2015-08-11 01:17:50 +00:00
|
|
|
if '.c' in available_extensions:
|
2015-08-10 18:33:39 +00:00
|
|
|
return 'C'
|
2015-06-16 01:55:47 +00:00
|
|
|
|
2015-06-16 01:26:10 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-22 20:39:16 +00:00
|
|
|
def number_lines_in_file(file_name):
|
|
|
|
lines = 0
|
|
|
|
try:
|
2014-09-30 16:09:30 +00:00
|
|
|
with open(file_name, 'r', encoding='utf-8') as fh:
|
|
|
|
for line in fh:
|
2013-09-22 20:39:16 +00:00
|
|
|
lines += 1
|
2015-09-08 21:07:14 +00:00
|
|
|
except: # pragma: nocover
|
2015-08-24 01:49:34 +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 20:39:16 +00:00
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2016-04-19 10:30:44 +00:00
|
|
|
def standardize_language(language, plugin):
|
|
|
|
"""Maps a string to the equivalent Pygments language."""
|
|
|
|
|
2017-02-20 22:23:02 +00:00
|
|
|
if not language:
|
|
|
|
return None
|
|
|
|
|
2016-04-19 10:30:44 +00:00
|
|
|
# standardize language for this plugin
|
|
|
|
if plugin:
|
|
|
|
plugin = plugin.split(' ')[-1].split('/')[0].split('-')[0]
|
|
|
|
standardized = get_language_from_json(language, plugin)
|
|
|
|
if standardized is not None:
|
|
|
|
return standardized
|
|
|
|
|
|
|
|
# standardize language against default languages
|
|
|
|
standardized = get_language_from_json(language, 'default')
|
2017-02-20 20:40:24 +00:00
|
|
|
return standardized
|
2016-04-19 10:30:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_language_from_json(language, key):
|
|
|
|
"""Finds the given language in a json file."""
|
|
|
|
|
|
|
|
file_name = os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
'languages',
|
|
|
|
'{0}.json').format(key.lower())
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(file_name, 'r', encoding='utf-8') as fh:
|
|
|
|
languages = json.loads(fh.read())
|
2017-02-20 20:40:24 +00:00
|
|
|
if languages.get(language.lower()):
|
|
|
|
return languages[language.lower()]
|
2016-04-19 10:30:44 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_file_head(file_name):
|
|
|
|
"""Returns the first 512000 bytes of the file's contents."""
|
2015-06-11 21:11:10 +00:00
|
|
|
|
|
|
|
text = None
|
|
|
|
try:
|
|
|
|
with open(file_name, 'r', encoding='utf-8') as fh:
|
|
|
|
text = fh.read(512000)
|
2016-06-16 06:02:56 +00:00
|
|
|
except:
|
2015-08-24 01:49:34 +00:00
|
|
|
try:
|
|
|
|
with open(file_name, 'r', encoding=sys.getfilesystemencoding()) as fh:
|
2016-08-31 15:13:25 +00:00
|
|
|
text = fh.read(512000) # pragma: nocover
|
2015-08-24 01:49:34 +00:00
|
|
|
except:
|
2016-09-01 09:49:12 +00:00
|
|
|
log.traceback(logging.DEBUG)
|
2015-06-11 21:11:10 +00:00
|
|
|
return text
|