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
|
2014-12-23 05:12:57 +00:00
|
|
|
from .languages import DependencyParser
|
2014-09-30 16:09:30 +00:00
|
|
|
|
2013-09-22 22:24:03 +00:00
|
|
|
if sys.version_info[0] == 2:
|
2014-12-01 06:10:30 +00:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
|
2013-09-22 22:24:03 +00:00
|
|
|
else:
|
2014-12-01 06:10:30 +00:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
|
2013-09-22 23:28:09 +00:00
|
|
|
from pygments.lexers import guess_lexer_for_filename
|
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
|
|
|
|
|
|
|
|
2013-10-26 04:28:59 +00:00
|
|
|
# force file name extensions to be recognized as a certain language
|
|
|
|
EXTENSIONS = {
|
2013-11-01 00:16:38 +00:00
|
|
|
'j2': 'HTML',
|
2013-11-03 19:34:54 +00:00
|
|
|
'markdown': 'Markdown',
|
|
|
|
'md': 'Markdown',
|
2014-11-11 07:52:48 +00:00
|
|
|
'mdown': 'Markdown',
|
2013-12-03 19:20:57 +00:00
|
|
|
'twig': 'Twig',
|
2013-10-26 21:06:54 +00:00
|
|
|
}
|
|
|
|
TRANSLATIONS = {
|
|
|
|
'CSS+Genshi Text': 'CSS',
|
2013-10-27 00:58:15 +00:00
|
|
|
'CSS+Lasso': 'CSS',
|
|
|
|
'HTML+Django/Jinja': 'HTML',
|
|
|
|
'HTML+Lasso': 'HTML',
|
|
|
|
'JavaScript+Genshi Text': 'JavaScript',
|
|
|
|
'JavaScript+Lasso': 'JavaScript',
|
2013-10-26 21:14:13 +00:00
|
|
|
'Perl6': 'Perl',
|
2013-10-29 07:25:12 +00:00
|
|
|
'RHTML': 'HTML',
|
2013-10-26 04:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-22 20:39:16 +00:00
|
|
|
def guess_language(file_name):
|
2014-12-23 05:12:57 +00:00
|
|
|
language, lexer = None, None
|
2013-09-22 20:39:16 +00:00
|
|
|
try:
|
2014-09-30 16:09:30 +00:00
|
|
|
with open(file_name, 'r', encoding='utf-8') as fh:
|
|
|
|
lexer = guess_lexer_for_filename(file_name, fh.read(512000))
|
2013-10-14 21:12:40 +00:00
|
|
|
except:
|
2013-09-22 20:39:16 +00:00
|
|
|
pass
|
2014-12-23 05:12:57 +00:00
|
|
|
if file_name:
|
|
|
|
language = guess_language_from_extension(file_name.rsplit('.', 1)[-1])
|
|
|
|
if lexer and language is None:
|
|
|
|
language = translate_language(u(lexer.name))
|
|
|
|
return language, lexer
|
2013-09-22 20:39:16 +00:00
|
|
|
|
|
|
|
|
2013-10-26 04:28:59 +00:00
|
|
|
def guess_language_from_extension(extension):
|
|
|
|
if extension:
|
|
|
|
if extension in EXTENSIONS:
|
|
|
|
return EXTENSIONS[extension]
|
|
|
|
if extension.lower() in EXTENSIONS:
|
2015-02-10 20:09:32 +00:00
|
|
|
return EXTENSIONS[extension.lower()]
|
2013-10-26 04:28:59 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-10-26 21:06:54 +00:00
|
|
|
def translate_language(language):
|
|
|
|
if language in TRANSLATIONS:
|
|
|
|
language = TRANSLATIONS[language]
|
|
|
|
return language
|
|
|
|
|
|
|
|
|
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
|
2014-10-07 11:43:00 +00:00
|
|
|
except:
|
2013-09-22 20:39:16 +00:00
|
|
|
return None
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
2015-05-06 23:24:25 +00:00
|
|
|
def get_file_stats(file_name, notfile=False, lineno=None, cursorpos=None):
|
2015-02-13 01:27:21 +00:00
|
|
|
if notfile:
|
|
|
|
stats = {
|
|
|
|
'language': None,
|
|
|
|
'dependencies': [],
|
|
|
|
'lines': None,
|
2015-05-06 22:19:48 +00:00
|
|
|
'lineno': lineno,
|
2015-05-06 23:24:25 +00:00
|
|
|
'cursorpos': cursorpos,
|
2015-02-13 01:27:21 +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-06 22:19:48 +00:00
|
|
|
'lineno': lineno,
|
2015-05-06 23:24:25 +00:00
|
|
|
'cursorpos': cursorpos,
|
2015-02-13 01:27:21 +00:00
|
|
|
}
|
2013-09-22 20:39:16 +00:00
|
|
|
return stats
|