mirror of
https://github.com/wakatime/sublime-wakatime.git
synced 2023-08-10 21:13:02 +03:00
Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
9f9b97c69f | |||
908ff98613 | |||
37f74b4b56 | |||
a1c0d7e489 | |||
3127f264b4 | |||
049bc57019 | |||
03ec38bb67 | |||
4fc1a55ff7 | |||
f60815b813 | |||
ca47c2308d | |||
146a959747 | |||
906184cd88 | |||
a13e11d24d | |||
d34432217f | |||
f2e8f85198 | |||
05b08b6ab2 | |||
685d242c60 | |||
023c1dfbe3 | |||
9255fd2c34 | |||
784ad38c38 | |||
36def5c8b8 | |||
2c8dd6c9e7 | |||
e8151535c1 | |||
744116079a | |||
791a969a10 | |||
46c5171d6a | |||
fe641d01d4 | |||
4f03423333 | |||
e812e9fe15 | |||
a92ebad2f2 | |||
78a7e5cbcb | |||
5616206b48 |
243
HISTORY.rst
Normal file
243
HISTORY.rst
Normal file
@ -0,0 +1,243 @@
|
||||
|
||||
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)
|
||||
++++++++++++++++++
|
||||
|
||||
- recognize jinja2 file extensions as HTML
|
||||
|
||||
|
||||
1.4.9 (2013-10-28)
|
||||
++++++++++++++++++
|
||||
|
||||
- handle case where ignore patterns not defined
|
||||
|
||||
|
||||
1.4.8 (2013-10-27)
|
||||
++++++++++++++++++
|
||||
|
||||
- new setting to ignore files that match a regular expression pattern
|
||||
|
||||
|
||||
1.4.7 (2013-10-26)
|
||||
++++++++++++++++++
|
||||
|
||||
- simplify some language lexer names into more common versions
|
||||
|
||||
|
||||
1.4.6 (2013-10-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- force some file extensions to be recognized as certain language
|
||||
|
||||
|
||||
1.4.5 (2013-10-14)
|
||||
++++++++++++++++++
|
||||
|
||||
- remove support for subversion projects on Windows to prevent cmd window popups
|
||||
- ignore all errors from pygments library
|
||||
|
||||
|
||||
1.4.4 (2013-10-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- read git branch from .git/HEAD without running command line git client
|
||||
|
||||
|
||||
1.4.3 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- send olson timezone string to api for displaying logged time in user's zone
|
||||
|
||||
|
||||
1.4.2 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- print error code in Sublime's console if api request fails
|
||||
|
||||
|
||||
1.4.1 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix SSL support problem for Linux users
|
||||
|
||||
|
||||
1.4.0 (2013-09-22)
|
||||
++++++++++++++++++
|
||||
|
||||
- log source code language type of files
|
||||
- log total number of lines in files
|
||||
- better python3 support
|
||||
|
||||
|
||||
1.3.7 (2013-09-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix relative import bug
|
||||
|
||||
|
||||
1.3.6 (2013-09-06)
|
||||
++++++++++++++++++
|
||||
|
||||
- switch back to urllib2 instead of requests library in wakatime package
|
||||
|
||||
|
||||
1.3.5 (2013-09-05)
|
||||
++++++++++++++++++
|
||||
|
||||
- send Sublime version with api requests for easier debugging
|
||||
|
||||
|
||||
1.3.4 (2013-09-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgraded wakatime package
|
||||
|
||||
|
||||
1.3.3 (2013-09-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- using requests package in wakatime package
|
||||
|
||||
|
||||
1.3.2 (2013-08-25)
|
||||
++++++++++++++++++
|
||||
|
||||
- fix bug causing wrong file name detected
|
||||
- misc bug fixes
|
||||
|
||||
|
||||
1.3.0 (2013-08-15)
|
||||
++++++++++++++++++
|
||||
|
||||
- detect git branches
|
||||
|
||||
|
||||
1.2.0 (2013-08-12)
|
||||
++++++++++++++++++
|
||||
|
||||
- run wakatime package in new process when no SSL support in Sublime
|
||||
|
||||
|
||||
1.1.0 (2013-08-12)
|
||||
++++++++++++++++++
|
||||
|
||||
- run wakatime package in main Sublime process
|
||||
|
||||
|
||||
1.0.1 (2013-08-09)
|
||||
++++++++++++++++++
|
||||
|
||||
- no longer beta for Package Control versioning requirement
|
||||
|
||||
|
||||
0.4.2 (2013-08-08)
|
||||
++++++++++++++++++
|
||||
|
||||
- remove away prompt popup
|
||||
|
||||
|
||||
0.4.0 (2013-08-08)
|
||||
++++++++++++++++++
|
||||
|
||||
- run wakatime package in background
|
||||
|
||||
|
||||
0.3.3 (2013-08-06)
|
||||
++++++++++++++++++
|
||||
|
||||
- support installing via Sublime Package Control
|
||||
|
||||
|
||||
0.3.2 (2013-08-06)
|
||||
++++++++++++++++++
|
||||
|
||||
- fixes for user sublime-settings file
|
||||
|
||||
|
||||
0.3.1 (2013-08-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- renamed plugin folder
|
||||
|
||||
|
||||
0.3.0 (2013-08-04)
|
||||
++++++++++++++++++
|
||||
|
||||
- use WakaTime.sublime-settings file for configuration settings
|
||||
|
||||
|
||||
0.2.10 (2013-07-29)
|
||||
+++++++++++++++++++
|
||||
|
||||
- Python3 support
|
||||
- better Windows support by detecting pythonw.exe location
|
||||
|
||||
|
||||
0.2.9 (2013-07-22)
|
||||
++++++++++++++++++
|
||||
|
||||
- upgraded wakatime package
|
||||
- bug fix when detecting git repos
|
||||
|
||||
|
||||
0.2.8 (2013-07-21)
|
||||
++++++++++++++++++
|
||||
|
||||
- Windows bug fixes
|
||||
|
||||
|
||||
0.2.7 (2013-07-20)
|
||||
++++++++++++++++++
|
||||
|
||||
- prevent cmd window opening in background (Windows users only)
|
||||
|
||||
|
||||
0.2.6 (2013-07-17)
|
||||
++++++++++++++++++
|
||||
|
||||
- log errors from wakatime package to ~/.wakatime.log
|
||||
|
||||
|
||||
0.2.5 (2013-07-17)
|
||||
++++++++++++++++++
|
||||
|
||||
- distinguish between write events and normal events
|
||||
- prompt user for api key if one does not already exist
|
||||
- rename ~/.wakatime to ~/.wakatime.conf
|
||||
- set away prompt to 5 minutes
|
||||
- fix bug in custom logger
|
||||
|
||||
|
||||
0.2.1 (2013-07-07)
|
||||
++++++++++++++++++
|
||||
|
||||
- Birth
|
||||
|
@ -1,7 +1,7 @@
|
||||
sublime-wakatime
|
||||
================
|
||||
|
||||
Automatic time tracking for Sublime Text 2 & 3.
|
||||
Fully automatic time tracking for Sublime Text 2 & 3.
|
||||
|
||||
Installation
|
||||
------------
|
||||
@ -29,9 +29,5 @@ Heads Up! For Sublime Text 2 on Windows & Linux, WakaTime depends on [Python](ht
|
||||
Screen Shots
|
||||
------------
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||

|
||||
|
||||
|
125
WakaTime.py
125
WakaTime.py
@ -5,7 +5,7 @@ Maintainer: WakaTi.me <support@wakatime.com>
|
||||
Website: https://www.wakati.me/
|
||||
==========================================================="""
|
||||
|
||||
__version__ = '1.4.3'
|
||||
__version__ = '1.5.1'
|
||||
|
||||
import sublime
|
||||
import sublime_plugin
|
||||
@ -21,14 +21,17 @@ from os.path import expanduser, dirname, realpath, isfile, join, exists
|
||||
|
||||
|
||||
# globals
|
||||
ACTION_FREQUENCY = 5
|
||||
ACTION_FREQUENCY = 2
|
||||
ST_VERSION = int(sublime.version())
|
||||
PLUGIN_DIR = dirname(realpath(__file__))
|
||||
API_CLIENT = '%s/packages/wakatime/wakatime-cli.py' % PLUGIN_DIR
|
||||
SETTINGS_FILE = 'WakaTime.sublime-settings'
|
||||
SETTINGS = {}
|
||||
LAST_ACTION = 0
|
||||
LAST_FILE = None
|
||||
LAST_ACTION = {
|
||||
'time': 0,
|
||||
'file': None,
|
||||
'is_write': False,
|
||||
}
|
||||
HAS_SSL = False
|
||||
LOCK = threading.RLock()
|
||||
|
||||
@ -47,31 +50,6 @@ if HAS_SSL:
|
||||
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():
|
||||
global SETTINGS
|
||||
if not SETTINGS.get('api_key'):
|
||||
@ -89,60 +67,54 @@ def prompt_api_key():
|
||||
|
||||
|
||||
def python_binary():
|
||||
python = 'python'
|
||||
if platform.system() == 'Windows':
|
||||
python = 'pythonw'
|
||||
try:
|
||||
Popen([python, '--version'])
|
||||
Popen(['pythonw', '--version'])
|
||||
return 'pythonw'
|
||||
except:
|
||||
for path in glob.iglob('/python*'):
|
||||
if exists(realpath(join(path, 'pythonw.exe'))):
|
||||
python = realpath(join(path, 'pythonw'))
|
||||
break
|
||||
return python
|
||||
return realpath(join(path, 'pythonw'))
|
||||
return None
|
||||
return 'python'
|
||||
|
||||
|
||||
def enough_time_passed(now):
|
||||
if now - LAST_ACTION > ACTION_FREQUENCY * 60:
|
||||
def enough_time_passed(now, last_time):
|
||||
if now - last_time > ACTION_FREQUENCY * 60:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def handle_write_action(view):
|
||||
global LOCK, LAST_FILE, LAST_ACTION
|
||||
def handle_action(view, is_write=False):
|
||||
global LOCK, 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):
|
||||
global LOCK, LAST_FILE, LAST_ACTION
|
||||
with LOCK:
|
||||
targetFile = view.file_name()
|
||||
thread = SendActionThread(targetFile)
|
||||
thread.start()
|
||||
LAST_FILE = targetFile
|
||||
LAST_ACTION = time.time()
|
||||
target_file = view.file_name()
|
||||
if target_file:
|
||||
thread = SendActionThread(target_file, is_write=is_write)
|
||||
thread.start()
|
||||
LAST_ACTION = {
|
||||
'file': target_file,
|
||||
'time': time.time(),
|
||||
'is_write': is_write,
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
self.targetFile = targetFile
|
||||
self.isWrite = isWrite
|
||||
self.target_file = target_file
|
||||
self.is_write = is_write
|
||||
self.force = force
|
||||
self.debug = SETTINGS.get('debug')
|
||||
self.api_key = SETTINGS.get('api_key', '')
|
||||
self.last_file = LAST_FILE
|
||||
self.ignore = SETTINGS.get('ignore', [])
|
||||
self.last_action = LAST_ACTION
|
||||
|
||||
def run(self):
|
||||
if self.targetFile:
|
||||
if self.target_file:
|
||||
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 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()
|
||||
|
||||
def send(self):
|
||||
@ -152,13 +124,15 @@ class SendActionThread(threading.Thread):
|
||||
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
||||
cmd = [
|
||||
API_CLIENT,
|
||||
'--file', self.targetFile,
|
||||
'--file', self.target_file,
|
||||
'--time', str('%f' % self.timestamp),
|
||||
'--plugin', ua,
|
||||
'--key', str(bytes.decode(self.api_key.encode('utf8'))),
|
||||
]
|
||||
if self.isWrite:
|
||||
if self.is_write:
|
||||
cmd.append('--write')
|
||||
for pattern in self.ignore:
|
||||
cmd.extend(['--ignore', pattern])
|
||||
if self.debug:
|
||||
cmd.append('--verbose')
|
||||
if HAS_SSL:
|
||||
@ -168,18 +142,23 @@ class SendActionThread(threading.Thread):
|
||||
if code != 0:
|
||||
print('Error: Response code %d from wakatime package' % code)
|
||||
else:
|
||||
cmd.insert(0, python_binary())
|
||||
if self.debug:
|
||||
print(cmd)
|
||||
if platform.system() == 'Windows':
|
||||
Popen(cmd, shell=False)
|
||||
python = python_binary()
|
||||
if python:
|
||||
cmd.insert(0, python)
|
||||
if self.debug:
|
||||
print(cmd)
|
||||
if platform.system() == 'Windows':
|
||||
Popen(cmd, shell=False)
|
||||
else:
|
||||
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
||||
Popen(cmd, stderr=stderr)
|
||||
else:
|
||||
with open(join(expanduser('~'), '.wakatime.log'), 'a') as stderr:
|
||||
Popen(cmd, stderr=stderr)
|
||||
print('Error: Unable to find python binary.')
|
||||
|
||||
|
||||
def plugin_loaded():
|
||||
setup_settings_file()
|
||||
global SETTINGS
|
||||
SETTINGS = sublime.load_settings(SETTINGS_FILE)
|
||||
after_loaded()
|
||||
|
||||
|
||||
@ -196,10 +175,10 @@ if ST_VERSION < 3000:
|
||||
class WakatimeListener(sublime_plugin.EventListener):
|
||||
|
||||
def on_post_save(self, view):
|
||||
handle_write_action(view)
|
||||
handle_action(view, is_write=True)
|
||||
|
||||
def on_activated(self, view):
|
||||
handle_normal_action(view)
|
||||
handle_action(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.
|
||||
"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": false
|
||||
}
|
||||
|
@ -3,6 +3,31 @@ 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)
|
||||
++++++++++++++++++
|
||||
|
||||
- New config for ignoring files from regular expressions
|
||||
- Parse more options from config file (verbose, logfile, ignore)
|
||||
|
||||
|
||||
0.4.8 (2013-10-13)
|
||||
++++++++++++++++++
|
||||
|
||||
- Read git HEAD file to find current branch instead of running git command line
|
||||
|
||||
|
||||
0.4.7 (2013-09-30)
|
||||
++++++++++++++++++
|
||||
|
||||
- Sending local olson timezone string in api request
|
||||
|
||||
|
||||
0.4.6 (2013-09-22)
|
||||
++++++++++++++++++
|
||||
|
||||
|
@ -1,4 +1,3 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
wakatime-cli
|
||||
|
@ -12,7 +12,7 @@
|
||||
from __future__ import print_function
|
||||
|
||||
__title__ = 'wakatime'
|
||||
__version__ = '0.4.7'
|
||||
__version__ = '0.4.10'
|
||||
__author__ = 'Alan Hamlett'
|
||||
__license__ = 'BSD'
|
||||
__copyright__ = 'Copyright 2013 Alan Hamlett'
|
||||
@ -52,6 +52,41 @@ class FileAction(argparse.Action):
|
||||
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,
|
||||
}
|
||||
|
||||
if not os.path.isfile(configFile):
|
||||
return configs
|
||||
|
||||
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):
|
||||
try:
|
||||
sys.argv
|
||||
@ -75,6 +110,8 @@ def parseArguments(argv):
|
||||
parser.add_argument('--key', dest='key',
|
||||
help='your wakati.me api key; uses api_key from '+
|
||||
'~/.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',
|
||||
help='defaults to ~/.wakatime.log')
|
||||
parser.add_argument('--config', dest='config',
|
||||
@ -85,29 +122,38 @@ def parseArguments(argv):
|
||||
args = parser.parse_args(args=argv[1:])
|
||||
if not args.timestamp:
|
||||
args.timestamp = time.time()
|
||||
|
||||
# set arguments from config file
|
||||
configs = parseConfigFile(args.config)
|
||||
if not args.key:
|
||||
default_key = get_api_key(args.config)
|
||||
default_key = configs.get('api_key')
|
||||
if default_key:
|
||||
args.key = default_key
|
||||
else:
|
||||
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
|
||||
|
||||
|
||||
def get_api_key(configFile):
|
||||
if not configFile:
|
||||
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
||||
api_key = None
|
||||
def should_ignore(fileName, patterns):
|
||||
try:
|
||||
cf = open(configFile)
|
||||
for line in cf:
|
||||
line = line.split('=', 1)
|
||||
if line[0] == 'api_key':
|
||||
api_key = line[1].strip()
|
||||
cf.close()
|
||||
except IOError:
|
||||
print('Error: Could not read from config file.')
|
||||
return api_key
|
||||
for pattern in patterns:
|
||||
try:
|
||||
compiled = re.compile(pattern, re.IGNORECASE)
|
||||
if compiled.search(fileName):
|
||||
return pattern
|
||||
except re.error as ex:
|
||||
log.warning('Regex error (%s) for ignore pattern: %s' % (str(ex), pattern))
|
||||
except TypeError:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def get_user_agent(plugin):
|
||||
@ -189,6 +235,10 @@ def main(argv=None):
|
||||
argv = sys.argv
|
||||
args = parseArguments(argv)
|
||||
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):
|
||||
branch = None
|
||||
name = None
|
||||
@ -208,4 +258,3 @@ def main(argv=None):
|
||||
else:
|
||||
log.debug('File does not exist; ignoring this action.')
|
||||
return 101
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from .packages import simplejson as json
|
||||
try:
|
||||
@ -25,7 +26,13 @@ class CustomEncoder(json.JSONEncoder):
|
||||
if isinstance(obj, bytes):
|
||||
obj = bytes.decode(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):
|
||||
|
@ -12,6 +12,7 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
from .projects.wakatime import WakaTime
|
||||
from .projects.git import Git
|
||||
from .projects.mercurial import Mercurial
|
||||
from .projects.subversion import Subversion
|
||||
@ -20,6 +21,7 @@ from .projects.subversion import Subversion
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
PLUGINS = [
|
||||
WakaTime,
|
||||
Git,
|
||||
Mercurial,
|
||||
Subversion,
|
||||
|
@ -11,7 +11,6 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from .base import BaseProject
|
||||
try:
|
||||
@ -38,23 +37,16 @@ class Git(BaseProject):
|
||||
return None
|
||||
|
||||
def branch(self):
|
||||
stdout = None
|
||||
try:
|
||||
stdout, stderr = Popen([
|
||||
'git', 'branch', '--no-color'
|
||||
], stdout=PIPE, stderr=PIPE, cwd=self._project_base()
|
||||
).communicate()
|
||||
except OSError:
|
||||
pass
|
||||
if stdout:
|
||||
for line in stdout.splitlines():
|
||||
if isinstance(line, bytes):
|
||||
line = bytes.decode(line)
|
||||
line = line.split(' ', 1)
|
||||
if line[0] == '*':
|
||||
return line[1]
|
||||
return None
|
||||
|
||||
branch = None
|
||||
base = self._project_base()
|
||||
if base:
|
||||
head = os.path.join(self._project_base(), '.git', 'HEAD')
|
||||
try:
|
||||
with open(head) as f:
|
||||
branch = f.readline().strip().rsplit('/', 1)[-1]
|
||||
except IOError:
|
||||
pass
|
||||
return branch
|
||||
|
||||
def _project_base(self):
|
||||
if self.config:
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
from .base import BaseProject
|
||||
@ -62,6 +63,8 @@ class Subversion(BaseProject):
|
||||
return info
|
||||
|
||||
def _find_project_base(self, path, found=False):
|
||||
if platform.system() == 'Windows':
|
||||
return False
|
||||
path = os.path.realpath(path)
|
||||
if os.path.isfile(path):
|
||||
path = os.path.split(path)[0]
|
||||
|
51
packages/wakatime/wakatime/projects/wakatime.py
Normal file
51
packages/wakatime/wakatime/projects/wakatime.py
Normal 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])
|
@ -18,25 +18,61 @@ if sys.version_info[0] == 2:
|
||||
else:
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages', 'pygments3'))
|
||||
from pygments.lexers import guess_lexer_for_filename
|
||||
from pygments.util import ClassNotFound
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# force file name extensions to be recognized as a certain language
|
||||
EXTENSIONS = {
|
||||
'j2': 'HTML',
|
||||
'markdown': 'Markdown',
|
||||
'md': 'Markdown',
|
||||
}
|
||||
TRANSLATIONS = {
|
||||
'CSS+Genshi Text': 'CSS',
|
||||
'CSS+Lasso': 'CSS',
|
||||
'HTML+Django/Jinja': 'HTML',
|
||||
'HTML+Lasso': 'HTML',
|
||||
'JavaScript+Genshi Text': 'JavaScript',
|
||||
'JavaScript+Lasso': 'JavaScript',
|
||||
'Perl6': 'Perl',
|
||||
'RHTML': 'HTML',
|
||||
}
|
||||
|
||||
|
||||
def guess_language(file_name):
|
||||
if file_name:
|
||||
language = guess_language_from_extension(file_name.rsplit('.', 1)[-1])
|
||||
if language:
|
||||
return language
|
||||
lexer = None
|
||||
try:
|
||||
with open(file_name) as f:
|
||||
lexer = guess_lexer_for_filename(file_name, f.read(512000))
|
||||
except (ClassNotFound, IOError):
|
||||
except:
|
||||
pass
|
||||
if lexer:
|
||||
return str(lexer.name)
|
||||
return translate_language(str(lexer.name))
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def guess_language_from_extension(extension):
|
||||
if extension:
|
||||
if extension in EXTENSIONS:
|
||||
return EXTENSIONS[extension]
|
||||
if extension.lower() in EXTENSIONS:
|
||||
return mapping[EXTENSIONS.lower()]
|
||||
return None
|
||||
|
||||
|
||||
def translate_language(language):
|
||||
if language in TRANSLATIONS:
|
||||
language = TRANSLATIONS[language]
|
||||
return language
|
||||
|
||||
|
||||
def number_lines_in_file(file_name):
|
||||
lines = 0
|
||||
try:
|
||||
|
Reference in New Issue
Block a user