Compare commits

...

9 Commits

9 changed files with 118 additions and 17 deletions

View File

@ -3,6 +3,31 @@ History
------- -------
1.5.1 (2013-12-02)
++++++++++++++++++
- decode file names with filesystem encoding, then encode as utf-8 for logging
1.5.0 (2013-11-28)
++++++++++++++++++
- increase "ping" frequency from every 5 minutes to every 2 minutes
- prevent sending multiple api requests when saving the same file
1.4.12 (2013-11-21)
+++++++++++++++++++
- handle UnicodeDecodeError exceptions when json encoding log messages
1.4.11 (2013-11-13)
+++++++++++++++++++
- placing .wakatime-project file in a folder will read the project's name from that file
1.4.10 (2013-10-31) 1.4.10 (2013-10-31)
++++++++++++++++++ ++++++++++++++++++

View File

@ -5,7 +5,7 @@ Maintainer: WakaTi.me <support@wakatime.com>
Website: https://www.wakati.me/ Website: https://www.wakati.me/
===========================================================""" ==========================================================="""
__version__ = '1.4.10' __version__ = '1.5.1'
import sublime import sublime
import sublime_plugin import sublime_plugin
@ -21,14 +21,17 @@ from os.path import expanduser, dirname, realpath, isfile, join, exists
# globals # globals
ACTION_FREQUENCY = 5 ACTION_FREQUENCY = 2
ST_VERSION = int(sublime.version()) ST_VERSION = int(sublime.version())
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_FILE = 'WakaTime.sublime-settings' SETTINGS_FILE = 'WakaTime.sublime-settings'
SETTINGS = {} SETTINGS = {}
LAST_ACTION = 0 LAST_ACTION = {
LAST_FILE = None 'time': 0,
'file': None,
'is_write': False,
}
HAS_SSL = False HAS_SSL = False
LOCK = threading.RLock() LOCK = threading.RLock()
@ -76,20 +79,24 @@ def python_binary():
return 'python' return 'python'
def enough_time_passed(now): def enough_time_passed(now, last_time):
if now - LAST_ACTION > ACTION_FREQUENCY * 60: if now - last_time > ACTION_FREQUENCY * 60:
return True return True
return False return False
def handle_action(view, is_write=False): def handle_action(view, is_write=False):
global LOCK, LAST_FILE, LAST_ACTION global LOCK, LAST_ACTION
with LOCK: with LOCK:
target_file = view.file_name() target_file = view.file_name()
thread = SendActionThread(target_file, is_write=is_write) if target_file:
thread.start() thread = SendActionThread(target_file, is_write=is_write)
LAST_FILE = target_file thread.start()
LAST_ACTION = time.time() LAST_ACTION = {
'file': target_file,
'time': time.time(),
'is_write': is_write,
}
class SendActionThread(threading.Thread): class SendActionThread(threading.Thread):
@ -102,12 +109,12 @@ class SendActionThread(threading.Thread):
self.debug = SETTINGS.get('debug') self.debug = SETTINGS.get('debug')
self.api_key = SETTINGS.get('api_key', '') self.api_key = SETTINGS.get('api_key', '')
self.ignore = SETTINGS.get('ignore', []) self.ignore = SETTINGS.get('ignore', [])
self.last_file = LAST_FILE self.last_action = LAST_ACTION
def run(self): def run(self):
if self.target_file: if self.target_file:
self.timestamp = time.time() self.timestamp = time.time()
if self.force or self.is_write or self.target_file != self.last_file or enough_time_passed(self.timestamp): if self.force or (self.is_write and not self.last_action['is_write']) or self.target_file != self.last_action['file'] or enough_time_passed(self.timestamp, self.last_action['time']):
self.send() self.send()
def send(self): def send(self):

View File

@ -3,6 +3,12 @@ History
------- -------
0.4.10 (2013-11-13)
+++++++++++++++++++
- Placing .wakatime-project file in a folder will read the project's name from that file
0.4.9 (2013-10-27) 0.4.9 (2013-10-27)
++++++++++++++++++ ++++++++++++++++++

View File

@ -1,4 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
wakatime-cli wakatime-cli

View File

@ -12,7 +12,7 @@
from __future__ import print_function from __future__ import print_function
__title__ = 'wakatime' __title__ = 'wakatime'
__version__ = '0.4.9' __version__ = '0.4.10'
__author__ = 'Alan Hamlett' __author__ = 'Alan Hamlett'
__license__ = 'BSD' __license__ = 'BSD'
__copyright__ = 'Copyright 2013 Alan Hamlett' __copyright__ = 'Copyright 2013 Alan Hamlett'
@ -63,6 +63,9 @@ def parseConfigFile(configFile):
'verbose': False, 'verbose': False,
} }
if not os.path.isfile(configFile):
return configs
try: try:
with open(configFile) as fh: with open(configFile) as fh:
for line in fh: for line in fh:

View File

@ -11,6 +11,7 @@
import logging import logging
import os import os
import sys
from .packages import simplejson as json from .packages import simplejson as json
try: try:
@ -25,7 +26,13 @@ class CustomEncoder(json.JSONEncoder):
if isinstance(obj, bytes): if isinstance(obj, bytes):
obj = bytes.decode(obj) obj = bytes.decode(obj)
return json.dumps(obj) return json.dumps(obj)
return super(CustomEncoder, self).default(obj) try:
encoded = super(CustomEncoder, self).default(obj)
except UnicodeDecodeError:
encoding = sys.getfilesystemencoding()
obj = obj.decode(encoding, 'ignore').encode('utf-8')
encoded = super(CustomEncoder, self).default(obj)
return encoded
class JsonFormatter(logging.Formatter): class JsonFormatter(logging.Formatter):

View File

@ -12,6 +12,7 @@
import logging import logging
import os import os
from .projects.wakatime import WakaTime
from .projects.git import Git from .projects.git import Git
from .projects.mercurial import Mercurial from .projects.mercurial import Mercurial
from .projects.subversion import Subversion from .projects.subversion import Subversion
@ -20,6 +21,7 @@ from .projects.subversion import Subversion
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
PLUGINS = [ PLUGINS = [
WakaTime,
Git, Git,
Mercurial, Mercurial,
Subversion, Subversion,

View File

@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
"""
wakatime.projects.wakatime
~~~~~~~~~~~~~~~~~~~~~~~~~~
Information from a .wakatime-project file about the project for
a given file.
:copyright: (c) 2013 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
import logging
import os
from .base import BaseProject
log = logging.getLogger(__name__)
class WakaTime(BaseProject):
def process(self):
self.config = self._find_config(self.path)
if self.config:
return True
return False
def name(self):
project_name = None
try:
with open(self.config) as fh:
project_name = fh.readline().strip()
except IOError as e:
log.exception("Exception:")
return project_name
def branch(self):
return None
def _find_config(self, path):
path = os.path.realpath(path)
if os.path.isfile(path):
path = os.path.split(path)[0]
if os.path.isfile(os.path.join(path, '.wakatime-project')):
return os.path.join(path, '.wakatime-project')
split_path = os.path.split(path)
if split_path[1] == '':
return None
return self._find_config(split_path[0])

View File

@ -25,8 +25,9 @@ log = logging.getLogger(__name__)
# force file name extensions to be recognized as a certain language # force file name extensions to be recognized as a certain language
EXTENSIONS = { EXTENSIONS = {
'md': 'Markdown',
'j2': 'HTML', 'j2': 'HTML',
'markdown': 'Markdown',
'md': 'Markdown',
} }
TRANSLATIONS = { TRANSLATIONS = {
'CSS+Genshi Text': 'CSS', 'CSS+Genshi Text': 'CSS',