Compare commits

...

8 Commits
1.0.2 ... 1.3.0

7 changed files with 70 additions and 85 deletions

View File

@ -5,7 +5,7 @@ Maintainer: WakaTi.me <support@wakatime.com>
Website: https://www.wakati.me/
==========================================================="""
__version__ = '1.0.2'
__version__ = '1.3.0'
import sublime
import sublime_plugin
@ -29,21 +29,21 @@ SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {}
LAST_ACTION = 0
LAST_FILE = None
BUSY = False
HAS_SSL = False
LOCK = threading.RLock()
# check if we have SSL support
try:
import ssl
HAS_SSL = True
except ImportError:
from subprocess import Popen
# import wakatime package
if HAS_SSL:
# import wakatime package
sys.path.insert(0, join(PLUGIN_DIR, 'packages', 'wakatime'))
import wakatime
except ImportError:
from subprocess import Popen
def setup_settings_file():
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings
@ -70,24 +70,20 @@ def setup_settings_file():
sublime.save_settings(SETTINGS_FILE)
def get_api_key():
"""If api key not set, prompt user to enter one then save
to WakaTime.sublime-settings.
"""
def prompt_api_key():
global SETTINGS
api_key = SETTINGS.get('api_key', '')
if not api_key:
if not SETTINGS.get('api_key'):
def got_key(text):
if text:
api_key = str(text)
SETTINGS.set('api_key', api_key)
SETTINGS.set('api_key', str(text))
sublime.save_settings(SETTINGS_FILE)
window = sublime.active_window()
if window:
window.show_input_panel('Enter your WakaTi.me api key:', '', got_key, None, None)
window.show_input_panel('Enter your WakaTime api key:', '', got_key, None, None)
return True
else:
print('Error: Could not prompt for api key because no window found.')
return api_key
return False
def python_binary():
@ -111,13 +107,23 @@ def enough_time_passed(now):
def handle_write_action(view):
thread = SendActionThread(view.file_name(), isWrite=True)
thread.start()
global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name()
thread = SendActionThread(targetFile, isWrite=True)
thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
def handle_normal_action(view):
thread = SendActionThread(view.file_name())
thread.start()
global LOCK, LAST_FILE, LAST_ACTION
with LOCK:
targetFile = view.file_name()
thread = SendActionThread(targetFile)
thread.start()
LAST_FILE = targetFile
LAST_ACTION = time.time()
class SendActionThread(threading.Thread):
@ -127,33 +133,29 @@ class SendActionThread(threading.Thread):
self.targetFile = targetFile
self.isWrite = isWrite
self.force = force
self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '')
def run(self):
sublime.set_timeout(self.check, 0)
def check(self):
global LAST_ACTION, LAST_FILE
if self.targetFile:
self.timestamp = time.time()
if self.force or self.isWrite or self.targetFile != LAST_FILE or enough_time_passed(self.timestamp):
LAST_FILE = self.targetFile
LAST_ACTION = self.timestamp
self.send()
def send(self):
api_key = get_api_key()
if not api_key:
if not self.api_key:
print('missing api key')
return
cmd = [
API_CLIENT,
'--file', self.targetFile,
'--time', str('%f' % self.timestamp),
'--plugin', 'sublime-wakatime/%s' % __version__,
'--key', str(bytes.decode(api_key.encode('utf8'))),
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
]
if self.isWrite:
cmd.append('--write')
if SETTINGS.get('debug'):
if self.debug:
cmd.append('--verbose')
print(cmd)
if HAS_SSL:
@ -169,6 +171,12 @@ class SendActionThread(threading.Thread):
def plugin_loaded():
setup_settings_file()
after_loaded()
def after_loaded():
if not prompt_api_key():
sublime.set_timeout(after_loaded, 500)
# need to call plugin_loaded because only ST3 will auto-call it
@ -179,22 +187,10 @@ if ST_VERSION < 3000:
class WakatimeListener(sublime_plugin.EventListener):
def on_post_save(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_write_action(view)
BUSY = False
handle_write_action(view)
def on_activated(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_normal_action(view)
BUSY = False
handle_normal_action(view)
def on_modified(self, view):
global BUSY
if not BUSY:
BUSY = True
handle_normal_action(view)
BUSY = False
handle_normal_action(view)

View File

@ -2,6 +2,12 @@
History
-------
0.4.0 (2013-08-15)
++++++++++++++++++
- Sending single branch instead of multiple tags
0.3.1 (2013-08-08)
++++++++++++++++++

View File

@ -12,7 +12,7 @@
from __future__ import print_function
__title__ = 'wakatime'
__version__ = '0.3.1'
__version__ = '0.4.0'
__author__ = 'Alan Hamlett'
__license__ = 'BSD'
__copyright__ = 'Copyright 2013 Alan Hamlett'
@ -120,7 +120,7 @@ def get_user_agent(plugin):
return user_agent
def send_action(project=None, tags=None, key=None, targetFile=None,
def send_action(project=None, branch=None, key=None, targetFile=None,
timestamp=None, endtime=None, isWrite=None, plugin=None, **kwargs):
url = 'https://www.wakati.me/api/v1/actions'
log.debug('Sending action to api at %s' % url)
@ -134,8 +134,8 @@ def send_action(project=None, tags=None, key=None, targetFile=None,
data['is_write'] = isWrite
if project:
data['project'] = project
if tags:
data['tags'] = list(set(tags))
if branch:
data['branch'] = branch
log.debug(data)
# setup api request headers
@ -175,13 +175,13 @@ def main(argv=None):
args = parseArguments(argv)
setup_logging(args, __version__)
if os.path.isfile(args.targetFile):
tags = []
branch = None
name = None
project = find_project(args.targetFile)
if project:
tags = project.tags()
branch = project.branch()
name = project.name()
if send_action(project=name, tags=tags, **vars(args)):
if send_action(project=name, branch=branch, **vars(args)):
return 0
return 102
else:

View File

@ -47,8 +47,7 @@ class BaseProject(object):
"""
return None
def tags(self):
""" Returns an array of tag strings for the
path and/or project.
def branch(self):
""" Returns the current branch.
"""
return []
return None

View File

@ -37,29 +37,7 @@ class Git(BaseProject):
return os.path.basename(base)
return None
def tags(self):
tags = []
if self.config:
proj_name = self.name()
if proj_name:
tags.append(proj_name)
sections = self._parse_config()
for section in sections:
if section.split(' ', 1)[0] == 'remote' and 'url' in sections[section]:
remote = sections[section]['url'].rsplit(':', 1)[-1].rsplit('/', 1)[-1].split('.git', 1)[0]
if remote:
tags.append(remote)
branch = self._current_branch()
if branch is not None:
tags.append(branch)
return tags
def _project_base(self):
if self.config:
return os.path.dirname(os.path.dirname(self.config))
return None
def _current_branch(self):
def branch(self):
stdout = None
try:
stdout, stderr = Popen([
@ -77,6 +55,12 @@ class Git(BaseProject):
return line[1]
return None
def _project_base(self):
if self.config:
return os.path.dirname(os.path.dirname(self.config))
return None
def _find_config(self, path):
path = os.path.realpath(path)
if os.path.isfile(path):

View File

@ -26,5 +26,5 @@ class Mercurial(BaseProject):
def name(self):
return None
def tags(self):
return []
def branch(self):
return None

View File

@ -31,11 +31,11 @@ class Subversion(BaseProject):
def name(self):
return self.info['Repository Root'].split('/')[-1]
def tags(self):
tags = []
def branch(self):
branch = None
if self.base:
tags.append(os.path.basename(self.base))
return tags
branch = os.path.basename(self.base)
return branch
def _get_info(self, path):
info = OrderedDict()