new version 0.1.3. Cleaned up plugin. Automatically creating ~/.wakatime.log file.

This commit is contained in:
Alan Hamlett 2013-07-02 02:24:04 -07:00
parent 3457b0b534
commit 61370df1ba
3 changed files with 161 additions and 177 deletions

View file

@ -1,7 +1,7 @@
vim-wakatime 0.1.2 vim-wakatime 0.1.2
=========== ===========
Automatic time tracking. Automatic time tracking for Vim.
Installation Installation
------------ ------------
@ -12,7 +12,7 @@ https://wakati.me
2) Run this shell command replacing KEY with your api key: 2) Run this shell command replacing KEY with your api key:
echo "api_key=KEY" > ~/.wakatime echo "api_key=KEY" >> ~/.wakatime
3) Using [Vundle](https://github.com/gmarik/vundle), the Vim plugin manager: 3) Using [Vundle](https://github.com/gmarik/vundle), the Vim plugin manager:

View file

@ -17,7 +17,7 @@ import logging as log
# Config # Config
version = '0.1.2' version = '0.1.3'
user_agent = 'vim-wakatime/%s (%s)' % (version, platform.platform()) user_agent = 'vim-wakatime/%s (%s)' % (version, platform.platform())
@ -118,14 +118,12 @@ def send_action(key, instance, action, task, timestamp, project, tags):
url = 'https://www.wakati.me/api/v1/actions' url = 'https://www.wakati.me/api/v1/actions'
data = { data = {
'type': action, 'type': action,
'task': task, 'task': os.path.realpath(task),
'time': time.time(), 'time': timestamp,
'instance_id': instance, 'instance_id': instance,
'project': project, 'project': project,
'tags': tags, 'tags': tags,
} }
if timestamp:
data['time'] = timestamp
request = urllib2.Request(url=url, data=json.dumps(data)) request = urllib2.Request(url=url, data=json.dumps(data))
request.add_header('User-Agent', user_agent) request.add_header('User-Agent', user_agent)
request.add_header('Content-Type', 'application/json') request.add_header('Content-Type', 'application/json')
@ -173,7 +171,10 @@ def main(argv):
if args.verbose: if args.verbose:
level = log.DEBUG level = log.DEBUG
del args.verbose del args.verbose
if not args.timestamp:
args.timestamp = time.time()
log.basicConfig(filename=os.path.expanduser('~/.wakatime.log'), format='%(asctime)s vim-wakatime/'+version+' %(levelname)s %(message)s', datefmt='%Y-%m-%dT%H:%M:%SZ', level=level) log.basicConfig(filename=os.path.expanduser('~/.wakatime.log'), format='%(asctime)s vim-wakatime/'+version+' %(levelname)s %(message)s', datefmt='%Y-%m-%dT%H:%M:%SZ', level=level)
if os.path.isfile(os.path.realpath(args.task)):
tags = tags_from_path(args.task) tags = tags_from_path(args.task)
project = project_from_path(args.task) project = project_from_path(args.task)
send_action(project=project, tags=tags, **vars(args)) send_action(project=project, tags=tags, **vars(args))

View file

@ -1,27 +1,27 @@
" ============================================================================ " ============================================================================
" File: wakatime.vim " File: wakatime.vim
" Description: invisible time tracker using Wakati.Me " Description: Automatic time tracking for Vim.
" Maintainer: Wakati.Me <support@wakatime.com> " Maintainer: Wakati.Me <support@wakatime.com>
" Version: 0.1.2 " Version: 0.1.3
" ============================================================================ " ============================================================================
" Init {{{ " Init {{{
" Check Vim version " Check Vim version
if v:version < 700 if v:version < 700
echoerr "This plugin requires vim >= 7." echoerr "This plugin requires vim >= 7."
finish finish
endif endif
" Check for Python support " Check for Python support
if !has('python') if !has('python')
echoerr "This plugin requires Vim to be compiled with Python support." echoerr "This plugin requires Vim to be compiled with Python support."
finish finish
endif endif
" Check for required user-defined settings " Check for required user-defined settings
if !exists("g:wakatime_api_key") if !exists("g:wakatime_api_key")
if filereadable(expand("$HOME/.wakatime")) if filereadable(expand("$HOME/.wakatime"))
for s:line in readfile(expand("$HOME/.wakatime")) for s:line in readfile(expand("$HOME/.wakatime"))
let s:setting = split(s:line, "=") let s:setting = split(s:line, "=")
@ -33,90 +33,78 @@ if !exists("g:wakatime_api_key")
if !exists("g:wakatime_api_key") if !exists("g:wakatime_api_key")
finish finish
endif endif
endif endif
" Only load plugin once " Only load plugin once
if exists("g:loaded_wakatime") if exists("g:loaded_wakatime")
finish finish
endif endif
let g:loaded_wakatime = 1 let g:loaded_wakatime = 1
" Backup & Override cpoptions " Backup & Override cpoptions
let s:old_cpo = &cpo let s:old_cpo = &cpo
set cpo&vim set cpo&vim
let s:plugin_directory = expand("<sfile>:p:h") let s:plugin_directory = expand("<sfile>:p:h")
" Set default updatetime " Set default updatetime
if !exists("g:wakatime_updatetime") if !exists("g:wakatime_updatetime")
let g:wakatime_updatetime = 15 " 15 minutes let g:wakatime_updatetime = 15 " 15 minutes
endif endif
" We are not away until getting a CursorHold event " We are not away until getting a CursorHold event
let s:away_start = 0 let s:away_start = 0
" Create logfile if does not exist " Create logfile if does not exist
exec "silent !touch ~/.wakatime.log" exec "silent !touch ~/.wakatime.log"
python << ENDPYTHON python import time
import vim python import time
import uuid python import uuid
import time python import vim
python instance_id = str(uuid.uuid4())
instance_id = str(uuid.uuid4()) python vim.command('let s:instance_id = "%s"' % instance_id)
vim.command('let s:instance_id = "%s"' % instance_id)
ENDPYTHON
" }}} " }}}
" Function Definitions {{{ " Function Definitions {{{
function! s:initVariable(var, value) function! s:setUpdateTime()
if !exists(a:var)
exec 'let ' . a:var . ' = ' . "'" . substitute(a:value, "'", "''", "g") . "'"
return 1
endif
return 0
endfunction
function! s:setUpdateTime()
if &updatetime < 60 * 1000 * 2 if &updatetime < 60 * 1000 * 2
let &updatetime = g:wakatime_updatetime * 60 * 1000 let &updatetime = g:wakatime_updatetime * 60 * 1000
endif endif
endfunction endfunction
call s:setUpdateTime()
call s:setUpdateTime() function! s:GetCurrentFile()
function! s:GetCurrentFile()
return expand("%:p") return expand("%:p")
endfunction endfunction
function! s:api(type, task) function! s:GetCurrentTime()
python vim.command('let current_time="%f"' % time.time())
return current_time
endfunction
function! s:api(type, task, time)
if a:task != '' if a:task != ''
exec "silent !python " . s:plugin_directory . "/wakatime.py --key" g:wakatime_api_key "--instance" s:instance_id "--action" a:type "--task" shellescape(a:task) . " &" exec "silent !python " . s:plugin_directory . "/wakatime.py --key" g:wakatime_api_key "--instance" s:instance_id "--action" a:type "--task" shellescape(a:task) "--time" a:time . " &"
endif endif
endfunction endfunction
function! s:api_with_time(type, task, time) function! s:getchar()
if a:task != ''
exec "silent !python " . s:plugin_directory . "/wakatime.py --key" g:wakatime_api_key "--instance" s:instance_id "--action" a:type "--task" shellescape(a:task) "--time" printf("%f", a:time) . " &"
endif
endfunction
function! s:getchar()
let c = getchar() let c = getchar()
if c =~ '^\d\+$' if c =~ '^\d\+$'
let c = nr2char(c) let c = nr2char(c)
endif endif
return c return c
endfunction endfunction
function! Wakatime_isAway() function! Wakatime_isAway()
return s:away_start return s:away_start
endfunction endfunction
function! s:allServersAway() function! s:allServersAway()
if has('clientserver') if has('clientserver')
let servers = split(serverlist()) let servers = split(serverlist())
for server in servers for server in servers
@ -128,51 +116,46 @@ function! s:allServersAway()
endfor endfor
endif endif
return 1 return 1
endfunction endfunction
" }}} " }}}
" Event Handlers {{{ " Event Handlers {{{
function! s:bufenter() function! s:bufenter()
let task = s:GetCurrentFile() call s:api("open_file", s:GetCurrentFile(), s:GetCurrentTime())
call s:api("open_file", task) endfunction
endfunction
function! s:bufleave() function! s:bufleave()
let task = s:GetCurrentFile() call s:api("close_file", s:GetCurrentFile(), s:GetCurrentTime())
call s:api("close_file", task) endfunction
endfunction
function! s:vimenter() function! s:vimenter()
let task = s:GetCurrentFile() call s:api("open_editor", s:GetCurrentFile(), s:GetCurrentTime())
call s:api("open_editor", task) endfunction
endfunction
function! s:vimleave() function! s:vimleave()
let task = s:GetCurrentFile() call s:api("quit_editor", s:GetCurrentFile(), s:GetCurrentTime())
call s:api("quit_editor", task) endfunction
endfunction
function! s:bufwrite() function! s:bufwrite()
let task = s:GetCurrentFile() call s:api("write_file", s:GetCurrentFile(), s:GetCurrentTime())
call s:api("write_file", task) endfunction
endfunction
function! s:cursorhold() function! s:cursorhold()
let s:away_task = s:GetCurrentFile() let s:away_task = s:GetCurrentFile()
python vim.command("let s:away_start=%f" % (time.time() - (float(vim.eval("&updatetime")) / 1000.0))) python vim.command("let s:away_start=%f" % (time.time() - (float(vim.eval("&updatetime")) / 1000.0)))
autocmd Wakatime CursorMoved,CursorMovedI * call s:cursormoved() autocmd Wakatime CursorMoved,CursorMovedI * call s:cursormoved()
endfunction endfunction
function! s:cursormoved() function! s:cursormoved()
autocmd! Wakatime CursorMoved,CursorMovedI * autocmd! Wakatime CursorMoved,CursorMovedI *
" Don't do anything unless all other Vim instances are also away " Don't do anything unless all other Vim instances are also away
if !s:allServersAway() if !s:allServersAway()
let s:away_start = 0 let s:away_start = 0
call s:api("ping", s:away_task) call s:api("ping", s:away_task, s:GetCurrentTime())
return return
endif endif
@ -193,21 +176,21 @@ function! s:cursormoved()
endif endif
let answer = input(printf("You were away %.f %s. Add time to current file? (y/n)", away_duration, away_unit)) let answer = input(printf("You were away %.f %s. Add time to current file? (y/n)", away_duration, away_unit))
if answer != "y" if answer != "y"
call s:api_with_time("minimize_editor", s:away_task, s:away_start) call s:api("minimize_editor", s:away_task, printf("%f", s:away_start))
call s:api_with_time("maximize_editor", s:away_task, away_end) call s:api("maximize_editor", s:away_task, printf("%f", away_end))
else else
call s:api("ping", s:away_task) call s:api("ping", s:away_task, s:GetCurrentTime())
endif endif
let s:away_start = 0 let s:away_start = 0
"redraw! "redraw!
endfunction endfunction
" }}} " }}}
" Autocommand Events {{{ " Autocommand Events {{{
augroup Wakatime augroup Wakatime
autocmd! autocmd!
autocmd BufEnter * call s:bufenter() autocmd BufEnter * call s:bufenter()
autocmd BufLeave * call s:bufleave() autocmd BufLeave * call s:bufleave()
@ -215,7 +198,7 @@ augroup Wakatime
autocmd VimLeave * call s:vimleave() autocmd VimLeave * call s:vimleave()
autocmd BufWritePost * call s:bufwrite() autocmd BufWritePost * call s:bufwrite()
autocmd CursorHold,CursorHoldI * call s:cursorhold() autocmd CursorHold,CursorHoldI * call s:cursorhold()
augroup END augroup END
" }}} " }}}