added WakaTime.sublime-settings file for disabling away prompt. Fixes #10.

This commit is contained in:
Alan Hamlett 2013-08-04 22:11:22 -07:00
parent bc8c1af287
commit fc2776a125
3 changed files with 83 additions and 19 deletions

32
Main.sublime-menu Normal file
View File

@ -0,0 +1,32 @@
[
{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "WakaTime",
"mnemonic": "W",
"id": "wakatime-settings",
"children":
[
{
"command": "open_file", "args":
{
"file": "${packages}/WakaTime/WakaTime.sublime-settings"
},
"caption": "Settings Default"
},
{
"command": "open_file", "args":
{
"file": "${packages}/User/WakaTime/WakaTime.sublime-settings"
},
"caption": "Settings User"
}
]
}
]
}
]

11
WakaTime.sublime-settings Normal file
View File

@ -0,0 +1,11 @@
{
// Prompt after being away.
// If true, will prompt user after 10 minutes of inactivity.
// If false, only logs actual activity inside Sublime.
// Defaults to false.
"prompt_after_away": false,
// Your api key from https://www.wakati.me/#apikey
// Set this in your User specific WakaTime.sublime-settings file.
"api_key": ""
}

View File

@ -11,11 +11,12 @@ import sublime
import sublime_plugin import sublime_plugin
import glob import glob
import os
import platform import platform
import time import time
import uuid import uuid
from os.path import expanduser, dirname, realpath, isfile, join, exists from os.path import expanduser, dirname, realpath, isfile, join, exists
from subprocess import call, Popen, STARTUPINFO, STARTF_USESHOWWINDOW from subprocess import call, Popen
# globals # globals
@ -23,29 +24,46 @@ AWAY_MINUTES = 10
ACTION_FREQUENCY = 5 ACTION_FREQUENCY = 5
PLUGIN_DIR = dirname(realpath(__file__)) PLUGIN_DIR = dirname(realpath(__file__))
API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR
SETTINGS = '%s.sublime-settings' % __name__
LAST_ACTION = 0 LAST_ACTION = 0
LAST_USAGE = 0 LAST_USAGE = 0
LAST_FILE = None LAST_FILE = None
BUSY = False BUSY = False
# To be backwards compatible, rename config file # Convert ~/.wakatime.conf to WakaTime.sublime-settings
if isfile(join(expanduser('~'), '.wakatime')): def convert_config_to_sublime_settings():
call([ # To be backwards compatible, rename config file
'mv', settings = sublime.load_settings(SETTINGS)
join(expanduser('~'), '.wakatime'), api_key = settings.get('api_key', '')
join(expanduser('~'), '.wakatime.conf') try:
]) with open(join(expanduser('~'), '.wakatime.conf')) as old_file:
for line in old_file:
line = line.split('=', 1)
if line[0] == 'api_key':
api_key = line[1].strip()
except IOError:
pass
settings.set('api_key', api_key)
sublime.save_settings(SETTINGS)
try:
os.remove(join(expanduser('~'), '.wakatime.conf'))
except:
pass
convert_config_to_sublime_settings()
# Create config file if it does not already exist # Prompt for api key if not set in WakaTime.sublime-settings
if not isfile(join(expanduser('~'), '.wakatime.conf')): def check_api_key():
def got_key(text): settings = sublime.load_settings(SETTINGS)
if text: api_key = settings.get('api_key', None)
cfg = open(join(expanduser('~'), '.wakatime.conf'), 'w') if not api_key:
cfg.write('api_key=%s' % text) def got_key(text):
cfg.close() if text:
sublime.active_window().show_input_panel('Enter your WakaTi.me api key:', '', got_key, None, None) settings.set('api_key', str(api_key))
sublime.save_settings(SETTINGS)
sublime.active_window().show_input_panel('Enter your WakaTi.me api key:', '', got_key, None, None)
check_api_key()
def python_binary(): def python_binary():
@ -73,15 +91,16 @@ def api(targetFile, timestamp, isWrite=False, endtime=0):
'--plugin', 'sublime-wakatime/%s' % __version__, '--plugin', 'sublime-wakatime/%s' % __version__,
#'--verbose', #'--verbose',
] ]
api_key = sublime.load_settings(SETTINGS).get('api_key', None)
if api_key:
cmd.extend(['--key', str(api_key)])
if isWrite: if isWrite:
cmd.append('--write') cmd.append('--write')
if endtime: if endtime:
cmd.extend(['--endtime', str('%f' % endtime)]) cmd.extend(['--endtime', str('%f' % endtime)])
#print(cmd) #print(cmd)
if platform.system() == 'Windows': if platform.system() == 'Windows':
startupinfo = STARTUPINFO() Popen(cmd, shell=False)
startupinfo.dwFlags |= STARTF_USESHOWWINDOW
Popen(cmd, startupinfo=startupinfo)
else: else:
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr: with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
Popen(cmd, stderr=stderr) Popen(cmd, stderr=stderr)
@ -123,6 +142,8 @@ def enough_time_passed(now):
def should_prompt_user(now): def should_prompt_user(now):
if not sublime.load_settings(SETTINGS).get('prompt_after_away', False):
return False
if not LAST_USAGE: if not LAST_USAGE:
return False return False
duration = now - LAST_USAGE duration = now - LAST_USAGE