upgrade wakatime cli to v4.0.15

This commit is contained in:
Alan Hamlett 2015-06-21 10:35:14 -07:00
parent 9337e3173b
commit abf4a94a59
4 changed files with 164 additions and 35 deletions

View file

@ -1,7 +1,7 @@
__title__ = 'wakatime' __title__ = 'wakatime'
__description__ = 'Common interface to the WakaTime api.' __description__ = 'Common interface to the WakaTime api.'
__url__ = 'https://github.com/wakatime/wakatime' __url__ = 'https://github.com/wakatime/wakatime'
__version_info__ = ('4', '0', '13') __version_info__ = ('4', '0', '15')
__version__ = '.'.join(__version_info__) __version__ = '.'.join(__version_info__)
__author__ = 'Alan Hamlett' __author__ = 'Alan Hamlett'
__author_email__ = 'alan@wakatime.com' __author_email__ = 'alan@wakatime.com'

View file

@ -314,14 +314,15 @@ def send_heartbeat(project=None, branch=None, stats={}, key=None, targetFile=Non
log.debug('Sending heartbeat to api at %s' % api_url) log.debug('Sending heartbeat to api at %s' % api_url)
data = { data = {
'time': timestamp, 'time': timestamp,
'file': targetFile, 'entity': targetFile,
'type': 'file',
} }
if hidefilenames and targetFile is not None and not notfile: if hidefilenames and targetFile is not None and not notfile:
data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1] data['entity'] = data['entity'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1]
if len(data['file'].strip('.').split('.', 1)) > 1: if len(data['entity'].strip('.').split('.', 1)) > 1:
data['file'] = u('HIDDEN.{ext}').format(ext=u(data['file'].strip('.').rsplit('.', 1)[-1])) data['entity'] = u('HIDDEN.{ext}').format(ext=u(data['entity'].strip('.').rsplit('.', 1)[-1]))
else: else:
data['file'] = u('HIDDEN') data['entity'] = u('HIDDEN')
if stats.get('lines'): if stats.get('lines'):
data['lines'] = stats['lines'] data['lines'] = stats['lines']
if stats.get('language'): if stats.get('language'):
@ -455,15 +456,11 @@ def main(argv=None):
project_name = args.alternate_project project_name = args.alternate_project
kwargs = vars(args) kwargs = vars(args)
if 'project' in kwargs: kwargs['project'] = project_name
del kwargs['project'] kwargs['branch'] = branch
kwargs['stats'] = stats
if send_heartbeat( if send_heartbeat(**kwargs):
project=project_name,
branch=branch,
stats=stats,
**kwargs
):
queue = Queue() queue = Queue()
while True: while True:
heartbeat = queue.pop() heartbeat = queue.pop()

View file

@ -37,15 +37,17 @@ class CustomEncoder(json.JSONEncoder):
class JsonFormatter(logging.Formatter): class JsonFormatter(logging.Formatter):
def setup(self, timestamp, isWrite, targetFile, version, plugin, verbose): def setup(self, timestamp, isWrite, targetFile, version, plugin, verbose,
warnings=False):
self.timestamp = timestamp self.timestamp = timestamp
self.isWrite = isWrite self.isWrite = isWrite
self.targetFile = targetFile self.targetFile = targetFile
self.version = version self.version = version
self.plugin = plugin self.plugin = plugin
self.verbose = verbose self.verbose = verbose
self.warnings = warnings
def format(self, record): def format(self, record, *args):
data = OrderedDict([ data = OrderedDict([
('now', self.formatTime(record, self.datefmt)), ('now', self.formatTime(record, self.datefmt)),
]) ])
@ -60,7 +62,7 @@ class JsonFormatter(logging.Formatter):
if not self.isWrite: if not self.isWrite:
del data['isWrite'] del data['isWrite']
data['level'] = record.levelname data['level'] = record.levelname
data['message'] = record.msg data['message'] = record.getMessage() if self.warnings else record.msg
if not self.plugin: if not self.plugin:
del data['plugin'] del data['plugin']
return CustomEncoder().encode(data) return CustomEncoder().encode(data)
@ -77,7 +79,6 @@ def set_log_level(logger, args):
def setup_logging(args, version): def setup_logging(args, version):
logging.captureWarnings(True)
logger = logging.getLogger('WakaTime') logger = logging.getLogger('WakaTime')
set_log_level(logger, args) set_log_level(logger, args)
if len(logger.handlers) > 0: if len(logger.handlers) > 0:
@ -107,5 +108,23 @@ def setup_logging(args, version):
) )
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) logger.addHandler(handler)
logging.getLogger('py.warnings').addHandler(handler)
warnings_formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
warnings_formatter.setup(
timestamp=args.timestamp,
isWrite=args.isWrite,
targetFile=args.targetFile,
version=version,
plugin=args.plugin,
verbose=args.verbose,
warnings=True,
)
warnings_handler = logging.FileHandler(os.path.expanduser(logfile))
warnings_handler.setFormatter(warnings_formatter)
logging.getLogger('py.warnings').addHandler(warnings_handler)
try:
logging.captureWarnings(True)
except AttributeError:
pass # Python >= 2.7 is needed to capture warnings
return logger return logger

View file

@ -20,13 +20,15 @@ if sys.version_info[0] == 2:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2')) sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py2'))
else: else:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3')) sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments_py3'))
from pygments.lexers import guess_lexer_for_filename from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
from pygments.modeline import get_filetype_from_buffer
from pygments.util import ClassNotFound
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
# force file name extensions to be recognized as a certain language # extensions taking priority over lexer
EXTENSIONS = { EXTENSIONS = {
'j2': 'HTML', 'j2': 'HTML',
'markdown': 'Markdown', 'markdown': 'Markdown',
@ -34,6 +36,8 @@ EXTENSIONS = {
'mdown': 'Markdown', 'mdown': 'Markdown',
'twig': 'Twig', 'twig': 'Twig',
} }
# lexers to human readable languages
TRANSLATIONS = { TRANSLATIONS = {
'CSS+Genshi Text': 'CSS', 'CSS+Genshi Text': 'CSS',
'CSS+Lasso': 'CSS', 'CSS+Lasso': 'CSS',
@ -45,31 +49,127 @@ TRANSLATIONS = {
'RHTML': 'HTML', 'RHTML': 'HTML',
} }
# extensions for when no lexer is found
AUXILIARY_EXTENSIONS = {
'vb': 'VB.net',
}
def guess_language(file_name): def guess_language(file_name):
language, lexer = None, None """Guess lexer and language for a file.
try:
with open(file_name, 'r', encoding='utf-8') as fh: Returns (language, lexer) tuple where language is a unicode string.
lexer = guess_lexer_for_filename(file_name, fh.read(512000)) """
except:
pass lexer = smart_guess_lexer(file_name)
language = None
# guess language from file extension
if file_name: if file_name:
language = guess_language_from_extension(file_name.rsplit('.', 1)[-1]) language = get_language_from_extension(file_name, EXTENSIONS)
if lexer and language is None:
language = translate_language(u(lexer.name)) # get language from lexer if we didn't have a hard-coded extension rule
if language is None and lexer:
language = u(lexer.name)
if language is None:
language = get_language_from_extension(file_name, AUXILIARY_EXTENSIONS)
if language is not None:
language = translate_language(language)
return language, lexer return language, lexer
def guess_language_from_extension(extension): 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)
lexer_1, accuracy_1 = guess_lexer_using_filename(file_name, text)
lexer_2, accuracy_2 = guess_lexer_using_modeline(text)
if lexer_1:
lexer = lexer_1
if (lexer_2 and accuracy_2 and
(not accuracy_1 or accuracy_2 > accuracy_1)):
lexer = lexer_2
return lexer
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).
"""
lexer, accuracy = None, None
try:
lexer = guess_lexer_for_filename(file_name, text)
except:
pass
if lexer is not None:
try:
accuracy = lexer.analyse_text(text)
except:
pass
return lexer, accuracy
def guess_lexer_using_modeline(text):
"""Guess lexer for given text using Vim modeline.
Returns a tuple of (lexer, accuracy).
"""
lexer, accuracy = None, None
file_type = get_filetype_from_buffer(text)
if file_type is not None:
try:
lexer = get_lexer_by_name(file_type)
except ClassNotFound:
pass
if lexer is not None:
try:
accuracy = lexer.analyse_text(text)
except:
pass
return lexer, accuracy
def get_language_from_extension(file_name, extension_map):
"""Returns a matching language for the given file_name using extension_map.
"""
extension = file_name.rsplit('.', 1)[-1] if len(file_name.rsplit('.', 1)) > 1 else None
if extension: if extension:
if extension in EXTENSIONS: if extension in extension_map:
return EXTENSIONS[extension] return extension_map[extension]
if extension.lower() in EXTENSIONS: if extension.lower() in extension_map:
return EXTENSIONS[extension.lower()] return extension_map[extension.lower()]
return None return None
def translate_language(language): def translate_language(language):
"""Turns Pygments lexer class name string into human-readable language.
"""
if language in TRANSLATIONS: if language in TRANSLATIONS:
language = TRANSLATIONS[language] language = TRANSLATIONS[language]
return language return language
@ -107,3 +207,16 @@ def get_file_stats(file_name, notfile=False, lineno=None, cursorpos=None):
'cursorpos': cursorpos, 'cursorpos': cursorpos,
} }
return stats return stats
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:
pass
return text