mirror of
https://github.com/wakatime/sublime-wakatime.git
synced 2023-08-10 21:13:02 +03:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
d34432217f | |||
f2e8f85198 | |||
05b08b6ab2 | |||
685d242c60 |
@ -3,6 +3,12 @@ History
|
|||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
||||||
|
1.4.8 (2013-10-27)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- new setting to ignore files that match a regular expression pattern
|
||||||
|
|
||||||
|
|
||||||
1.4.7 (2013-10-26)
|
1.4.7 (2013-10-26)
|
||||||
++++++++++++++++++
|
++++++++++++++++++
|
||||||
|
|
||||||
|
71
WakaTime.py
71
WakaTime.py
@ -5,7 +5,7 @@ Maintainer: WakaTi.me <support@wakatime.com>
|
|||||||
Website: https://www.wakati.me/
|
Website: https://www.wakati.me/
|
||||||
==========================================================="""
|
==========================================================="""
|
||||||
|
|
||||||
__version__ = '1.4.7'
|
__version__ = '1.4.8'
|
||||||
|
|
||||||
import sublime
|
import sublime
|
||||||
import sublime_plugin
|
import sublime_plugin
|
||||||
@ -47,31 +47,6 @@ if HAS_SSL:
|
|||||||
import wakatime
|
import wakatime
|
||||||
|
|
||||||
|
|
||||||
def setup_settings_file():
|
|
||||||
""" Convert ~/.wakatime.conf to WakaTime.sublime-settings
|
|
||||||
"""
|
|
||||||
global SETTINGS
|
|
||||||
# To be backwards compatible, rename config file
|
|
||||||
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
|
||||||
api_key = SETTINGS.get('api_key', '')
|
|
||||||
if not api_key:
|
|
||||||
api_key = ''
|
|
||||||
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 = str(line[1].strip())
|
|
||||||
try:
|
|
||||||
os.remove(join(expanduser('~'), '.wakatime.conf'))
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
except IOError:
|
|
||||||
pass
|
|
||||||
SETTINGS.set('api_key', api_key)
|
|
||||||
sublime.save_settings(SETTINGS_FILE)
|
|
||||||
|
|
||||||
|
|
||||||
def prompt_api_key():
|
def prompt_api_key():
|
||||||
global SETTINGS
|
global SETTINGS
|
||||||
if not SETTINGS.get('api_key'):
|
if not SETTINGS.get('api_key'):
|
||||||
@ -107,41 +82,32 @@ def enough_time_passed(now):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def handle_write_action(view):
|
def handle_action(view, is_write=False):
|
||||||
global LOCK, LAST_FILE, LAST_ACTION
|
global LOCK, LAST_FILE, LAST_ACTION
|
||||||
with LOCK:
|
with LOCK:
|
||||||
targetFile = view.file_name()
|
target_file = view.file_name()
|
||||||
thread = SendActionThread(targetFile, isWrite=True)
|
thread = SendActionThread(target_file, is_write=is_write)
|
||||||
thread.start()
|
thread.start()
|
||||||
LAST_FILE = targetFile
|
LAST_FILE = target_file
|
||||||
LAST_ACTION = time.time()
|
|
||||||
|
|
||||||
|
|
||||||
def handle_normal_action(view):
|
|
||||||
global LOCK, LAST_FILE, LAST_ACTION
|
|
||||||
with LOCK:
|
|
||||||
targetFile = view.file_name()
|
|
||||||
thread = SendActionThread(targetFile)
|
|
||||||
thread.start()
|
|
||||||
LAST_FILE = targetFile
|
|
||||||
LAST_ACTION = time.time()
|
LAST_ACTION = time.time()
|
||||||
|
|
||||||
|
|
||||||
class SendActionThread(threading.Thread):
|
class SendActionThread(threading.Thread):
|
||||||
|
|
||||||
def __init__(self, targetFile, isWrite=False, force=False):
|
def __init__(self, target_file, is_write=False, force=False):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.targetFile = targetFile
|
self.target_file = target_file
|
||||||
self.isWrite = isWrite
|
self.is_write = is_write
|
||||||
self.force = force
|
self.force = force
|
||||||
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.last_file = LAST_FILE
|
self.last_file = LAST_FILE
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.targetFile:
|
if self.target_file:
|
||||||
self.timestamp = time.time()
|
self.timestamp = time.time()
|
||||||
if self.force or self.isWrite or self.targetFile != self.last_file or enough_time_passed(self.timestamp):
|
if self.force or self.is_write or self.target_file != self.last_file or enough_time_passed(self.timestamp):
|
||||||
self.send()
|
self.send()
|
||||||
|
|
||||||
def send(self):
|
def send(self):
|
||||||
@ -151,13 +117,15 @@ class SendActionThread(threading.Thread):
|
|||||||
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
||||||
cmd = [
|
cmd = [
|
||||||
API_CLIENT,
|
API_CLIENT,
|
||||||
'--file', self.targetFile,
|
'--file', self.target_file,
|
||||||
'--time', str('%f' % self.timestamp),
|
'--time', str('%f' % self.timestamp),
|
||||||
'--plugin', ua,
|
'--plugin', ua,
|
||||||
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
|
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
|
||||||
]
|
]
|
||||||
if self.isWrite:
|
if self.is_write:
|
||||||
cmd.append('--write')
|
cmd.append('--write')
|
||||||
|
for pattern in self.ignore:
|
||||||
|
cmd.extend(['--ignore', pattern])
|
||||||
if self.debug:
|
if self.debug:
|
||||||
cmd.append('--verbose')
|
cmd.append('--verbose')
|
||||||
if HAS_SSL:
|
if HAS_SSL:
|
||||||
@ -182,7 +150,8 @@ class SendActionThread(threading.Thread):
|
|||||||
|
|
||||||
|
|
||||||
def plugin_loaded():
|
def plugin_loaded():
|
||||||
setup_settings_file()
|
global SETTINGS
|
||||||
|
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
||||||
after_loaded()
|
after_loaded()
|
||||||
|
|
||||||
|
|
||||||
@ -199,10 +168,10 @@ if ST_VERSION < 3000:
|
|||||||
class WakatimeListener(sublime_plugin.EventListener):
|
class WakatimeListener(sublime_plugin.EventListener):
|
||||||
|
|
||||||
def on_post_save(self, view):
|
def on_post_save(self, view):
|
||||||
handle_write_action(view)
|
handle_action(view, is_write=True)
|
||||||
|
|
||||||
def on_activated(self, view):
|
def on_activated(self, view):
|
||||||
handle_normal_action(view)
|
handle_action(view)
|
||||||
|
|
||||||
def on_modified(self, view):
|
def on_modified(self, view):
|
||||||
handle_normal_action(view)
|
handle_action(view)
|
||||||
|
@ -7,6 +7,10 @@
|
|||||||
// Set this in your User specific WakaTime.sublime-settings file.
|
// Set this in your User specific WakaTime.sublime-settings file.
|
||||||
"api_key": "",
|
"api_key": "",
|
||||||
|
|
||||||
|
// Ignore files; Files (including absolute paths) that match one of these
|
||||||
|
// POSIX regular expressions will not be logged.
|
||||||
|
"ignore": ["^/tmp/", "^/etc/", "^/var/"],
|
||||||
|
|
||||||
// Debug mode. Set to true for verbose logging. Defaults to false.
|
// Debug mode. Set to true for verbose logging. Defaults to false.
|
||||||
"debug": false
|
"debug": false
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,13 @@ History
|
|||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
||||||
|
0.4.9 (2013-10-27)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- New config for ignoring files from regular expressions
|
||||||
|
- Parse more options from config file (verbose, logfile, ignore)
|
||||||
|
|
||||||
|
|
||||||
0.4.8 (2013-10-13)
|
0.4.8 (2013-10-13)
|
||||||
++++++++++++++++++
|
++++++++++++++++++
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
__title__ = 'wakatime'
|
__title__ = 'wakatime'
|
||||||
__version__ = '0.4.8'
|
__version__ = '0.4.9'
|
||||||
__author__ = 'Alan Hamlett'
|
__author__ = 'Alan Hamlett'
|
||||||
__license__ = 'BSD'
|
__license__ = 'BSD'
|
||||||
__copyright__ = 'Copyright 2013 Alan Hamlett'
|
__copyright__ = 'Copyright 2013 Alan Hamlett'
|
||||||
@ -52,6 +52,38 @@ class FileAction(argparse.Action):
|
|||||||
setattr(namespace, self.dest, values)
|
setattr(namespace, self.dest, values)
|
||||||
|
|
||||||
|
|
||||||
|
def parseConfigFile(configFile):
|
||||||
|
if not configFile:
|
||||||
|
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
||||||
|
|
||||||
|
# define default config values
|
||||||
|
configs = {
|
||||||
|
'api_key': None,
|
||||||
|
'ignore': [],
|
||||||
|
'verbose': False,
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(configFile) as fh:
|
||||||
|
for line in fh:
|
||||||
|
line = line.split('=', 1)
|
||||||
|
if len(line) == 2 and line[0].strip() and line[1].strip():
|
||||||
|
line[0] = line[0].strip()
|
||||||
|
line[1] = line[1].strip()
|
||||||
|
if line[0] in configs:
|
||||||
|
if isinstance(configs[line[0]], list):
|
||||||
|
configs[line[0]].append(line[1])
|
||||||
|
elif isinstance(configs[line[0]], bool):
|
||||||
|
configs[line[0]] = True if line[1].lower() == 'true' else False
|
||||||
|
else:
|
||||||
|
configs[line[0]] = line[1]
|
||||||
|
else:
|
||||||
|
configs[line[0]] = line[1]
|
||||||
|
except IOError:
|
||||||
|
print('Error: Could not read from config file ~/.wakatime.conf')
|
||||||
|
return configs
|
||||||
|
|
||||||
|
|
||||||
def parseArguments(argv):
|
def parseArguments(argv):
|
||||||
try:
|
try:
|
||||||
sys.argv
|
sys.argv
|
||||||
@ -75,6 +107,8 @@ def parseArguments(argv):
|
|||||||
parser.add_argument('--key', dest='key',
|
parser.add_argument('--key', dest='key',
|
||||||
help='your wakati.me api key; uses api_key from '+
|
help='your wakati.me api key; uses api_key from '+
|
||||||
'~/.wakatime.conf by default')
|
'~/.wakatime.conf by default')
|
||||||
|
parser.add_argument('--ignore', dest='ignore', action='append',
|
||||||
|
help='filename patterns to ignore; POSIX regex syntax; can be used more than once')
|
||||||
parser.add_argument('--logfile', dest='logfile',
|
parser.add_argument('--logfile', dest='logfile',
|
||||||
help='defaults to ~/.wakatime.log')
|
help='defaults to ~/.wakatime.log')
|
||||||
parser.add_argument('--config', dest='config',
|
parser.add_argument('--config', dest='config',
|
||||||
@ -85,29 +119,35 @@ def parseArguments(argv):
|
|||||||
args = parser.parse_args(args=argv[1:])
|
args = parser.parse_args(args=argv[1:])
|
||||||
if not args.timestamp:
|
if not args.timestamp:
|
||||||
args.timestamp = time.time()
|
args.timestamp = time.time()
|
||||||
|
|
||||||
|
# set arguments from config file
|
||||||
|
configs = parseConfigFile(args.config)
|
||||||
if not args.key:
|
if not args.key:
|
||||||
default_key = get_api_key(args.config)
|
default_key = configs.get('api_key')
|
||||||
if default_key:
|
if default_key:
|
||||||
args.key = default_key
|
args.key = default_key
|
||||||
else:
|
else:
|
||||||
parser.error('Missing api key')
|
parser.error('Missing api key')
|
||||||
|
for pattern in configs.get('ignore', []):
|
||||||
|
if not args.ignore:
|
||||||
|
args.ignore = []
|
||||||
|
args.ignore.append(pattern)
|
||||||
|
if not args.verbose and 'verbose' in configs:
|
||||||
|
args.verbose = configs['verbose']
|
||||||
|
if not args.logfile and 'logfile' in configs:
|
||||||
|
args.logfile = configs['logfile']
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def get_api_key(configFile):
|
def should_ignore(fileName, patterns):
|
||||||
if not configFile:
|
for pattern in patterns:
|
||||||
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
try:
|
||||||
api_key = None
|
compiled = re.compile(pattern, re.IGNORECASE)
|
||||||
try:
|
if compiled.search(fileName):
|
||||||
cf = open(configFile)
|
return pattern
|
||||||
for line in cf:
|
except re.error as ex:
|
||||||
line = line.split('=', 1)
|
log.warning('Regex error (%s) for ignore pattern: %s' % (str(ex), pattern))
|
||||||
if line[0] == 'api_key':
|
return False
|
||||||
api_key = line[1].strip()
|
|
||||||
cf.close()
|
|
||||||
except IOError:
|
|
||||||
print('Error: Could not read from config file.')
|
|
||||||
return api_key
|
|
||||||
|
|
||||||
|
|
||||||
def get_user_agent(plugin):
|
def get_user_agent(plugin):
|
||||||
@ -189,6 +229,10 @@ def main(argv=None):
|
|||||||
argv = sys.argv
|
argv = sys.argv
|
||||||
args = parseArguments(argv)
|
args = parseArguments(argv)
|
||||||
setup_logging(args, __version__)
|
setup_logging(args, __version__)
|
||||||
|
ignore = should_ignore(args.targetFile, args.ignore)
|
||||||
|
if ignore is not False:
|
||||||
|
log.debug('File ignored because matches pattern: %s' % ignore)
|
||||||
|
return 0
|
||||||
if os.path.isfile(args.targetFile):
|
if os.path.isfile(args.targetFile):
|
||||||
branch = None
|
branch = None
|
||||||
name = None
|
name = None
|
||||||
@ -208,4 +252,3 @@ def main(argv=None):
|
|||||||
else:
|
else:
|
||||||
log.debug('File does not exist; ignoring this action.')
|
log.debug('File does not exist; ignoring this action.')
|
||||||
return 101
|
return 101
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user