mirror of
https://github.com/wakatime/sublime-wakatime.git
synced 2023-08-10 21:13:02 +03:00
Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
66fddc07b9 | |||
e56a07e909 | |||
64ea40b3f5 | |||
17fd6ef8e1 | |||
e5e399dfbe | |||
bcf037e8a4 | |||
7e678a38bd | |||
533aaac313 | |||
7f4f70cc85 | |||
4adb8a8796 | |||
48e1993b24 | |||
8a3375bb23 | |||
8bd54a7427 | |||
fcbbf05933 | |||
9733087094 | |||
da4e02199a | |||
09a16dea1e | |||
4c7adf0943 | |||
216a8eaa0a | |||
81f838489d | |||
d6228b8dce | |||
7a2c2b9750 | |||
d9cc911595 | |||
805e2fe222 | |||
bbcb39b2cf | |||
9f9b97c69f | |||
908ff98613 | |||
37f74b4b56 | |||
a1c0d7e489 |
78
HISTORY.rst
78
HISTORY.rst
@ -3,6 +3,84 @@ History
|
|||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
||||||
|
2.0.3 (2014-05-26)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade external wakatime package to v2.0.1
|
||||||
|
- fix bug in queue preventing completed tasks from being purged
|
||||||
|
|
||||||
|
|
||||||
|
2.0.2 (2014-05-26)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- disable syncing offline time until bug fixed
|
||||||
|
|
||||||
|
|
||||||
|
2.0.1 (2014-05-25)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade external wakatime package to v2.0.0
|
||||||
|
- offline time logging using sqlite3 to queue editor events
|
||||||
|
|
||||||
|
|
||||||
|
1.6.5 (2014-03-05)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade external wakatime package to v1.0.1
|
||||||
|
- use new domain wakatime.com
|
||||||
|
|
||||||
|
|
||||||
|
1.6.4 (2014-02-05)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade external wakatime package to v1.0.0
|
||||||
|
- support for mercurial revision control
|
||||||
|
|
||||||
|
|
||||||
|
1.6.3 (2014-01-15)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade common wakatime package to v0.5.3
|
||||||
|
|
||||||
|
|
||||||
|
1.6.2 (2014-01-14)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade common wakatime package to v0.5.2
|
||||||
|
|
||||||
|
|
||||||
|
1.6.1 (2013-12-13)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade common wakatime package to v0.5.1
|
||||||
|
- second line in .wakatime-project now sets branch name
|
||||||
|
|
||||||
|
|
||||||
|
1.6.0 (2013-12-13)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- upgrade common wakatime package to v0.5.0
|
||||||
|
|
||||||
|
|
||||||
|
1.5.2 (2013-12-03)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- use non-localized datetime in log
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
1.4.12 (2013-11-21)
|
||||||
+++++++++++++++++++
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
29
LICENSE
Normal file
29
LICENSE
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
Copyright (c) 2014 Alan Hamlett https://wakatime.com
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer
|
||||||
|
in the documentation and/or other materials provided
|
||||||
|
with the distribution.
|
||||||
|
|
||||||
|
* Neither the names of Wakatime or WakaTime, nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived
|
||||||
|
from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||||
|
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
|
||||||
|
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
|
||||||
|
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@ -8,7 +8,7 @@ Installation
|
|||||||
|
|
||||||
Heads Up! For Sublime Text 2 on Windows & Linux, WakaTime depends on [Python](http://www.python.org/getit/) being installed to work correctly.
|
Heads Up! For Sublime Text 2 on Windows & Linux, WakaTime depends on [Python](http://www.python.org/getit/) being installed to work correctly.
|
||||||
|
|
||||||
1. Get an api key from: https://wakati.me
|
1. Get an api key from: https://wakatime.com/#apikey
|
||||||
|
|
||||||
2. Using [Sublime Package Control](http://wbond.net/sublime_packages/package_control):
|
2. Using [Sublime Package Control](http://wbond.net/sublime_packages/package_control):
|
||||||
|
|
||||||
@ -18,16 +18,16 @@ Heads Up! For Sublime Text 2 on Windows & Linux, WakaTime depends on [Python](ht
|
|||||||
|
|
||||||
c) Type `wakatime`, then press `enter` with the `WakaTime` plugin selected.
|
c) Type `wakatime`, then press `enter` with the `WakaTime` plugin selected.
|
||||||
|
|
||||||
3. You will see a prompt at the bottom asking for your [api key](https://www.wakati.me/#apikey). Enter your api key, then press `enter`.
|
3. You will see a prompt at the bottom asking for your [api key](https://wakatime.com/#apikey). Enter your api key, then press `enter`.
|
||||||
|
|
||||||
4. Use Sublime and your time will automatically be tracked for you.
|
4. Use Sublime and your time will automatically be tracked for you.
|
||||||
|
|
||||||
5. Visit https://wakati.me to see your logged time.
|
5. Visit https://wakatime.com to see your logged time.
|
||||||
|
|
||||||
6. Consider installing [BIND9](https://help.ubuntu.com/community/BIND9ServerHowto#Caching_Server_configuration) to cache your repeated DNS requests: `sudo apt-get install bind9`
|
6. Consider installing [BIND9](https://help.ubuntu.com/community/BIND9ServerHowto#Caching_Server_configuration) to cache your repeated DNS requests: `sudo apt-get install bind9`
|
||||||
|
|
||||||
Screen Shots
|
Screen Shots
|
||||||
------------
|
------------
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
48
WakaTime.py
48
WakaTime.py
@ -1,11 +1,12 @@
|
|||||||
""" ==========================================================
|
""" ==========================================================
|
||||||
File: WakaTime.py
|
File: WakaTime.py
|
||||||
Description: Automatic time tracking for Sublime Text 2 and 3.
|
Description: Automatic time tracking for Sublime Text 2 and 3.
|
||||||
Maintainer: WakaTi.me <support@wakatime.com>
|
Maintainer: WakaTime <support@wakatime.com>
|
||||||
Website: https://www.wakati.me/
|
License: BSD, see LICENSE for more details.
|
||||||
|
Website: https://wakatime.com/
|
||||||
==========================================================="""
|
==========================================================="""
|
||||||
|
|
||||||
__version__ = '1.4.12'
|
__version__ = '2.0.3'
|
||||||
|
|
||||||
import sublime
|
import sublime
|
||||||
import sublime_plugin
|
import sublime_plugin
|
||||||
@ -21,14 +22,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()
|
||||||
|
|
||||||
@ -56,10 +60,10 @@ def prompt_api_key():
|
|||||||
sublime.save_settings(SETTINGS_FILE)
|
sublime.save_settings(SETTINGS_FILE)
|
||||||
window = sublime.active_window()
|
window = sublime.active_window()
|
||||||
if window:
|
if window:
|
||||||
window.show_input_panel('Enter your WakaTime api key:', '', got_key, None, None)
|
window.show_input_panel('[WakaTime] Enter your wakatime.com api key:', '', got_key, None, None)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print('Error: Could not prompt for api key because no window found.')
|
print('[WakaTime] Error: Could not prompt for api key because no window found.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -76,20 +80,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,17 +110,17 @@ 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):
|
||||||
if not self.api_key:
|
if not self.api_key:
|
||||||
print('missing api key')
|
print('[WakaTime] Error: missing api key.')
|
||||||
return
|
return
|
||||||
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
ua = 'sublime/%d sublime-wakatime/%s' % (ST_VERSION, __version__)
|
||||||
cmd = [
|
cmd = [
|
||||||
@ -133,7 +141,7 @@ class SendActionThread(threading.Thread):
|
|||||||
print(cmd)
|
print(cmd)
|
||||||
code = wakatime.main(cmd)
|
code = wakatime.main(cmd)
|
||||||
if code != 0:
|
if code != 0:
|
||||||
print('Error: Response code %d from wakatime package' % code)
|
print('[WakaTime] Error: Response code %d from wakatime package.' % code)
|
||||||
else:
|
else:
|
||||||
python = python_binary()
|
python = python_binary()
|
||||||
if python:
|
if python:
|
||||||
@ -146,7 +154,7 @@ class SendActionThread(threading.Thread):
|
|||||||
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)
|
||||||
else:
|
else:
|
||||||
print('Error: Unable to find python binary.')
|
print('[WakaTime] Error: Unable to find python binary.')
|
||||||
|
|
||||||
|
|
||||||
def plugin_loaded():
|
def plugin_loaded():
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
// This settings file will be overwritten when upgrading.
|
// This settings file will be overwritten when upgrading.
|
||||||
|
|
||||||
{
|
{
|
||||||
// Your api key from https://www.wakati.me/#apikey
|
// Your api key from https://wakatime.com/#apikey
|
||||||
// Set this in your User specific WakaTime.sublime-settings file.
|
// Set this in your User specific WakaTime.sublime-settings file.
|
||||||
"api_key": "",
|
"api_key": "",
|
||||||
|
|
||||||
|
15
packages/wakatime/AUTHORS
Normal file
15
packages/wakatime/AUTHORS
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
WakaTime is written and maintained by Alan Hamlett and
|
||||||
|
various contributors:
|
||||||
|
|
||||||
|
|
||||||
|
Development Lead
|
||||||
|
----------------
|
||||||
|
|
||||||
|
- Alan Hamlett <alan.hamlett@gmail.com>
|
||||||
|
|
||||||
|
|
||||||
|
Patches and Suggestions
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
- 3onyc <3onyc@x3tech.com>
|
||||||
|
- userid <xixico@ymail.com>
|
@ -3,6 +3,61 @@ History
|
|||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
||||||
|
2.0.1 (2014-05-26)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- fix bug in queue preventing actions with NULL values from being purged
|
||||||
|
|
||||||
|
|
||||||
|
2.0.0 (2014-05-25)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- offline time logging using sqlite3 to queue editor events
|
||||||
|
|
||||||
|
|
||||||
|
1.0.2 (2014-05-06)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- ability to set project from command line argument
|
||||||
|
|
||||||
|
|
||||||
|
1.0.1 (2014-03-05)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- use new domain name wakatime.com
|
||||||
|
|
||||||
|
|
||||||
|
1.0.0 (2014-02-05)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- detect project name and branch name from mercurial revision control
|
||||||
|
|
||||||
|
|
||||||
|
0.5.3 (2014-01-15)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- bug fix for unicode in Python3
|
||||||
|
|
||||||
|
|
||||||
|
0.5.2 (2014-01-14)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- minor bug fix for Subversion on non-English systems
|
||||||
|
|
||||||
|
|
||||||
|
0.5.1 (2013-12-13)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- second line in .wakatime-project file now sets branch name
|
||||||
|
|
||||||
|
|
||||||
|
0.5.0 (2013-12-13)
|
||||||
|
++++++++++++++++++
|
||||||
|
|
||||||
|
- Convert ~/.wakatime.conf to ~/.wakatime.cfg and use configparser format
|
||||||
|
- new [projectmap] section in cfg file for naming projects based on folders
|
||||||
|
|
||||||
|
|
||||||
0.4.10 (2013-11-13)
|
0.4.10 (2013-11-13)
|
||||||
+++++++++++++++++++
|
+++++++++++++++++++
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2013 Alan Hamlett https://wakati.me
|
Copyright (c) 2013 Alan Hamlett https://wakatime.com
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
@ -12,7 +12,7 @@ modification, are permitted provided that the following conditions are met:
|
|||||||
in the documentation and/or other materials provided
|
in the documentation and/or other materials provided
|
||||||
with the distribution.
|
with the distribution.
|
||||||
|
|
||||||
* Neither the names of Wakatime or Wakati.Me, nor the names of its
|
* Neither the names of Wakatime or WakaTime, nor the names of its
|
||||||
contributors may be used to endorse or promote products derived
|
contributors may be used to endorse or promote products derived
|
||||||
from this software without specific prior written permission.
|
from this software without specific prior written permission.
|
||||||
|
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
WakaTime
|
WakaTime
|
||||||
========
|
========
|
||||||
|
|
||||||
Automatic time tracking for your text editor. This is the command line
|
Fully automatic time tracking for programmers.
|
||||||
event appender for the WakaTime api. You shouldn't need to directly
|
|
||||||
use this outside of a text editor plugin.
|
This is the common interface for the WakaTime api. You shouldn't need to directly use this package unless you are creating a new plugin or your text editor's plugin asks you to install the wakatime-cli interface.
|
||||||
|
|
||||||
|
Go to http://wakatime.com to install the plugin for your text editor.
|
||||||
|
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
https://www.wakati.me/help/plugins/installing-plugins
|
pip install wakatime
|
||||||
|
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
|
https://wakatime.com/
|
||||||
|
@ -11,13 +11,10 @@ setup(
|
|||||||
name='wakatime',
|
name='wakatime',
|
||||||
version=VERSION,
|
version=VERSION,
|
||||||
license='BSD 3 Clause',
|
license='BSD 3 Clause',
|
||||||
description=' '.join([
|
description='Interface to the WakaTime api.',
|
||||||
'Action event appender for Wakati.Me, a time',
|
|
||||||
'tracking api for text editors.',
|
|
||||||
]),
|
|
||||||
long_description=open('README.rst').read(),
|
long_description=open('README.rst').read(),
|
||||||
author='Alan Hamlett',
|
author='Alan Hamlett',
|
||||||
author_email='alan.hamlett@gmail.com',
|
author_email='alan@wakatime.com',
|
||||||
url='https://github.com/wakatime/wakatime',
|
url='https://github.com/wakatime/wakatime',
|
||||||
packages=packages,
|
packages=packages,
|
||||||
package_dir={'wakatime': 'wakatime'},
|
package_dir={'wakatime': 'wakatime'},
|
||||||
@ -28,7 +25,7 @@ setup(
|
|||||||
'console_scripts': ['wakatime = wakatime.__init__:main'],
|
'console_scripts': ['wakatime = wakatime.__init__:main'],
|
||||||
},
|
},
|
||||||
classifiers=(
|
classifiers=(
|
||||||
'Development Status :: 3 - Alpha',
|
'Development Status :: 5 - Production/Stable',
|
||||||
'Environment :: Console',
|
'Environment :: Console',
|
||||||
'Intended Audience :: Developers',
|
'Intended Audience :: Developers',
|
||||||
'License :: OSI Approved :: BSD License',
|
'License :: OSI Approved :: BSD License',
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
wakatime-cli
|
wakatime-cli
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
Action event appender for Wakati.Me, auto time tracking for text editors.
|
Command-line entry point.
|
||||||
|
|
||||||
:copyright: (c) 2013 Alan Hamlett.
|
:copyright: (c) 2013 Alan Hamlett.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
wakatime
|
wakatime
|
||||||
~~~~~~~~
|
~~~~~~~~
|
||||||
|
|
||||||
Action event appender for Wakati.Me, auto time tracking for text editors.
|
Common interface to the WakaTime api.
|
||||||
|
http://wakatime.com
|
||||||
|
|
||||||
:copyright: (c) 2013 Alan Hamlett.
|
:copyright: (c) 2013 Alan Hamlett.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
@ -12,10 +13,10 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
__title__ = 'wakatime'
|
__title__ = 'wakatime'
|
||||||
__version__ = '0.4.10'
|
__version__ = '2.0.1'
|
||||||
__author__ = 'Alan Hamlett'
|
__author__ = 'Alan Hamlett'
|
||||||
__license__ = 'BSD'
|
__license__ = 'BSD'
|
||||||
__copyright__ = 'Copyright 2013 Alan Hamlett'
|
__copyright__ = 'Copyright 2014 Alan Hamlett'
|
||||||
|
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
@ -26,20 +27,25 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
try:
|
||||||
|
import ConfigParser as configparser
|
||||||
|
except ImportError:
|
||||||
|
import configparser
|
||||||
|
try:
|
||||||
|
from urllib2 import HTTPError, Request, urlopen
|
||||||
|
except ImportError:
|
||||||
|
from urllib.error import HTTPError
|
||||||
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages'))
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages'))
|
||||||
|
from .queue import Queue
|
||||||
from .log import setup_logging
|
from .log import setup_logging
|
||||||
from .project import find_project
|
from .project import find_project
|
||||||
from .stats import get_file_stats
|
from .stats import get_file_stats
|
||||||
from .packages import argparse
|
from .packages import argparse
|
||||||
from .packages import simplejson as json
|
from .packages import simplejson as json
|
||||||
from .packages import tzlocal
|
from .packages import tzlocal
|
||||||
try:
|
|
||||||
from urllib2 import HTTPError, Request, urlopen
|
|
||||||
except ImportError:
|
|
||||||
from urllib.error import HTTPError
|
|
||||||
from urllib.request import Request, urlopen
|
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -52,48 +58,84 @@ class FileAction(argparse.Action):
|
|||||||
setattr(namespace, self.dest, values)
|
setattr(namespace, self.dest, values)
|
||||||
|
|
||||||
|
|
||||||
def parseConfigFile(configFile):
|
def upgradeConfigFile(configFile):
|
||||||
if not configFile:
|
"""For backwards-compatibility, upgrade the existing config file
|
||||||
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
to work with configparser and rename from .wakatime.conf to .wakatime.cfg.
|
||||||
|
"""
|
||||||
|
|
||||||
# define default config values
|
if os.path.isfile(configFile):
|
||||||
configs = {
|
# if upgraded cfg file already exists, don't overwrite it
|
||||||
'api_key': None,
|
return
|
||||||
'ignore': [],
|
|
||||||
'verbose': False,
|
|
||||||
}
|
|
||||||
|
|
||||||
if not os.path.isfile(configFile):
|
|
||||||
return configs
|
|
||||||
|
|
||||||
|
oldConfig = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
||||||
try:
|
try:
|
||||||
with open(configFile) as fh:
|
configs = {
|
||||||
for line in fh:
|
'ignore': [],
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(oldConfig) as fh:
|
||||||
|
for line in fh.readlines():
|
||||||
line = line.split('=', 1)
|
line = line.split('=', 1)
|
||||||
if len(line) == 2 and line[0].strip() and line[1].strip():
|
if len(line) == 2 and line[0].strip() and line[1].strip():
|
||||||
line[0] = line[0].strip()
|
if line[0].strip() == 'ignore':
|
||||||
line[1] = line[1].strip()
|
configs['ignore'].append(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:
|
else:
|
||||||
configs[line[0]] = line[1]
|
configs[line[0].strip()] = line[1].strip()
|
||||||
|
|
||||||
|
with open(configFile, 'w') as fh:
|
||||||
|
fh.write("[settings]\n")
|
||||||
|
for name, value in configs.items():
|
||||||
|
if isinstance(value, list):
|
||||||
|
fh.write("%s=\n" % name)
|
||||||
|
for item in value:
|
||||||
|
fh.write(" %s\n" % item)
|
||||||
|
else:
|
||||||
|
fh.write("%s = %s\n" % (name, value))
|
||||||
|
|
||||||
|
os.remove(oldConfig)
|
||||||
except IOError:
|
except IOError:
|
||||||
print('Error: Could not read from config file ~/.wakatime.conf')
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def parseConfigFile(configFile):
|
||||||
|
"""Returns a configparser.SafeConfigParser instance with configs
|
||||||
|
read from the config file. Default location of the config file is
|
||||||
|
at ~/.wakatime.cfg.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not configFile:
|
||||||
|
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
|
||||||
|
|
||||||
|
upgradeConfigFile(configFile)
|
||||||
|
|
||||||
|
configs = configparser.SafeConfigParser()
|
||||||
|
try:
|
||||||
|
with open(configFile) as fh:
|
||||||
|
try:
|
||||||
|
configs.readfp(fh)
|
||||||
|
except configparser.Error:
|
||||||
|
print(traceback.format_exc())
|
||||||
|
return None
|
||||||
|
except IOError:
|
||||||
|
if not os.path.isfile(configFile):
|
||||||
|
print('Error: Could not read from config file ~/.wakatime.conf')
|
||||||
return configs
|
return configs
|
||||||
|
|
||||||
|
|
||||||
def parseArguments(argv):
|
def parseArguments(argv):
|
||||||
|
"""Parse command line arguments and configs from ~/.wakatime.cfg.
|
||||||
|
Command line arguments take precedence over config file settings.
|
||||||
|
Returns instances of ArgumentParser and SafeConfigParser.
|
||||||
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sys.argv
|
sys.argv
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
sys.argv = argv
|
sys.argv = argv
|
||||||
|
|
||||||
|
# define supported command line arguments
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='Wakati.Me event api appender')
|
description='Common interface for the WakaTime api.')
|
||||||
parser.add_argument('--file', dest='targetFile', metavar='file',
|
parser.add_argument('--file', dest='targetFile', metavar='file',
|
||||||
action=FileAction, required=True,
|
action=FileAction, required=True,
|
||||||
help='absolute path to file for current action')
|
help='absolute path to file for current action')
|
||||||
@ -107,9 +149,14 @@ def parseArguments(argv):
|
|||||||
parser.add_argument('--plugin', dest='plugin',
|
parser.add_argument('--plugin', dest='plugin',
|
||||||
help='optional text editor plugin name and version '+
|
help='optional text editor plugin name and version '+
|
||||||
'for User-Agent header')
|
'for User-Agent header')
|
||||||
|
parser.add_argument('--project', dest='project_name',
|
||||||
|
help='optional project name; will auto-discover by default')
|
||||||
parser.add_argument('--key', dest='key',
|
parser.add_argument('--key', dest='key',
|
||||||
help='your wakati.me api key; uses api_key from '+
|
help='your wakatime api key; uses api_key from '+
|
||||||
'~/.wakatime.conf by default')
|
'~/.wakatime.conf by default')
|
||||||
|
parser.add_argument('--disableoffline', dest='offline',
|
||||||
|
action='store_false',
|
||||||
|
help='disables offline time logging instead of queuing logged time')
|
||||||
parser.add_argument('--ignore', dest='ignore', action='append',
|
parser.add_argument('--ignore', dest='ignore', action='append',
|
||||||
help='filename patterns to ignore; POSIX regex syntax; can be used more than once')
|
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',
|
||||||
@ -119,27 +166,47 @@ def parseArguments(argv):
|
|||||||
parser.add_argument('--verbose', dest='verbose', action='store_true',
|
parser.add_argument('--verbose', dest='verbose', action='store_true',
|
||||||
help='turns on debug messages in log file')
|
help='turns on debug messages in log file')
|
||||||
parser.add_argument('--version', action='version', version=__version__)
|
parser.add_argument('--version', action='version', version=__version__)
|
||||||
|
|
||||||
|
# parse command line arguments
|
||||||
args = parser.parse_args(args=argv[1:])
|
args = parser.parse_args(args=argv[1:])
|
||||||
|
|
||||||
|
# use current unix epoch timestamp by default
|
||||||
if not args.timestamp:
|
if not args.timestamp:
|
||||||
args.timestamp = time.time()
|
args.timestamp = time.time()
|
||||||
|
|
||||||
# set arguments from config file
|
# parse ~/.wakatime.cfg file
|
||||||
configs = parseConfigFile(args.config)
|
configs = parseConfigFile(args.config)
|
||||||
|
if configs is None:
|
||||||
|
return args, configs
|
||||||
|
|
||||||
|
# update args from configs
|
||||||
if not args.key:
|
if not args.key:
|
||||||
default_key = configs.get('api_key')
|
default_key = None
|
||||||
|
if configs.has_option('settings', 'api_key'):
|
||||||
|
default_key = configs.get('settings', '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:
|
||||||
if not args.ignore:
|
args.ignore = []
|
||||||
args.ignore = []
|
if configs.has_option('settings', 'ignore'):
|
||||||
args.ignore.append(pattern)
|
try:
|
||||||
if not args.verbose and 'verbose' in configs:
|
for pattern in configs.get('settings', 'ignore').split("\n"):
|
||||||
args.verbose = configs['verbose']
|
if pattern.strip() != '':
|
||||||
if not args.logfile and 'logfile' in configs:
|
args.ignore.append(pattern)
|
||||||
args.logfile = configs['logfile']
|
except TypeError:
|
||||||
return args
|
pass
|
||||||
|
if args.offline and configs.has_option('settings', 'offline'):
|
||||||
|
args.offline = configs.getboolean('settings', 'offline')
|
||||||
|
if not args.verbose and configs.has_option('settings', 'verbose'):
|
||||||
|
args.verbose = configs.getboolean('settings', 'verbose')
|
||||||
|
if not args.verbose and configs.has_option('settings', 'debug'):
|
||||||
|
args.verbose = configs.getboolean('settings', 'debug')
|
||||||
|
if not args.logfile and configs.has_option('settings', 'logfile'):
|
||||||
|
args.logfile = configs.get('settings', 'logfile')
|
||||||
|
|
||||||
|
return args, configs
|
||||||
|
|
||||||
|
|
||||||
def should_ignore(fileName, patterns):
|
def should_ignore(fileName, patterns):
|
||||||
@ -166,9 +233,9 @@ def get_user_agent(plugin):
|
|||||||
return user_agent
|
return user_agent
|
||||||
|
|
||||||
|
|
||||||
def send_action(project=None, branch=None, stats={}, key=None, targetFile=None,
|
def send_action(project=None, branch=None, stats=None, key=None, targetFile=None,
|
||||||
timestamp=None, isWrite=None, plugin=None, **kwargs):
|
timestamp=None, isWrite=None, plugin=None, offline=None, **kwargs):
|
||||||
url = 'https://www.wakati.me/api/v1/actions'
|
url = 'https://wakatime.com/api/v1/actions'
|
||||||
log.debug('Sending action to api at %s' % url)
|
log.debug('Sending action to api at %s' % url)
|
||||||
data = {
|
data = {
|
||||||
'time': timestamp,
|
'time': timestamp,
|
||||||
@ -209,52 +276,93 @@ def send_action(project=None, branch=None, stats={}, key=None, targetFile=None,
|
|||||||
}
|
}
|
||||||
if log.isEnabledFor(logging.DEBUG):
|
if log.isEnabledFor(logging.DEBUG):
|
||||||
exception_data['traceback'] = traceback.format_exc()
|
exception_data['traceback'] = traceback.format_exc()
|
||||||
log.error(exception_data)
|
if offline:
|
||||||
|
queue = Queue()
|
||||||
|
queue.push(data, plugin)
|
||||||
|
if log.isEnabledFor(logging.DEBUG):
|
||||||
|
log.warn(exception_data)
|
||||||
|
else:
|
||||||
|
log.error(exception_data)
|
||||||
except:
|
except:
|
||||||
exception_data = {
|
exception_data = {
|
||||||
sys.exc_info()[0].__name__: str(sys.exc_info()[1]),
|
sys.exc_info()[0].__name__: str(sys.exc_info()[1]),
|
||||||
}
|
}
|
||||||
if log.isEnabledFor(logging.DEBUG):
|
if log.isEnabledFor(logging.DEBUG):
|
||||||
exception_data['traceback'] = traceback.format_exc()
|
exception_data['traceback'] = traceback.format_exc()
|
||||||
log.error(exception_data)
|
if offline:
|
||||||
|
queue = Queue()
|
||||||
|
queue.push(data, plugin)
|
||||||
|
if log.isEnabledFor(logging.DEBUG):
|
||||||
|
log.warn(exception_data)
|
||||||
|
else:
|
||||||
|
log.error(exception_data)
|
||||||
else:
|
else:
|
||||||
if response.getcode() == 201:
|
if response.getcode() == 201:
|
||||||
log.debug({
|
log.debug({
|
||||||
'response_code': response.getcode(),
|
'response_code': response.getcode(),
|
||||||
})
|
})
|
||||||
return True
|
return True
|
||||||
log.error({
|
if offline:
|
||||||
'response_code': response.getcode(),
|
queue = Queue()
|
||||||
'response_content': response.read(),
|
queue.push(data, plugin)
|
||||||
})
|
if log.isEnabledFor(logging.DEBUG):
|
||||||
|
log.warn({
|
||||||
|
'response_code': response.getcode(),
|
||||||
|
'response_content': response.read(),
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
log.error({
|
||||||
|
'response_code': response.getcode(),
|
||||||
|
'response_content': response.read(),
|
||||||
|
})
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
if not argv:
|
if not argv:
|
||||||
argv = sys.argv
|
argv = sys.argv
|
||||||
args = parseArguments(argv)
|
|
||||||
|
args, configs = parseArguments(argv)
|
||||||
|
if configs is None:
|
||||||
|
return 103 # config file parsing error
|
||||||
|
|
||||||
setup_logging(args, __version__)
|
setup_logging(args, __version__)
|
||||||
|
|
||||||
ignore = should_ignore(args.targetFile, args.ignore)
|
ignore = should_ignore(args.targetFile, args.ignore)
|
||||||
if ignore is not False:
|
if ignore is not False:
|
||||||
log.debug('File ignored because matches pattern: %s' % ignore)
|
log.debug('File ignored because matches pattern: %s' % ignore)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if os.path.isfile(args.targetFile):
|
if os.path.isfile(args.targetFile):
|
||||||
branch = None
|
|
||||||
name = None
|
|
||||||
stats = get_file_stats(args.targetFile)
|
stats = get_file_stats(args.targetFile)
|
||||||
project = find_project(args.targetFile)
|
|
||||||
|
project = find_project(args.targetFile, configs=configs)
|
||||||
|
branch = None
|
||||||
|
project_name = None
|
||||||
if project:
|
if project:
|
||||||
branch = project.branch()
|
branch = project.branch()
|
||||||
name = project.name()
|
project_name = args.project_name or project.name()
|
||||||
|
|
||||||
if send_action(
|
if send_action(
|
||||||
project=name,
|
project=project_name,
|
||||||
branch=branch,
|
branch=branch,
|
||||||
stats=stats,
|
stats=stats,
|
||||||
**vars(args)
|
**vars(args)
|
||||||
):
|
):
|
||||||
return 0
|
queue = Queue()
|
||||||
return 102
|
while True:
|
||||||
|
action = queue.pop()
|
||||||
|
if action is None:
|
||||||
|
break
|
||||||
|
if not send_action(project=action['project'], targetFile=action['file'], timestamp=action['time'],
|
||||||
|
branch=action['branch'], stats={'language': action['language'], 'lines': action['lines']},
|
||||||
|
key=args.key, isWrite=action['is_write'], plugin=action['plugin'], offline=args.offline):
|
||||||
|
break
|
||||||
|
return 0 # success
|
||||||
|
|
||||||
|
return 102 # api error
|
||||||
|
|
||||||
else:
|
else:
|
||||||
log.debug('File does not exist; ignoring this action.')
|
log.debug('File does not exist; ignoring this action.')
|
||||||
return 101
|
return 0
|
||||||
|
@ -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:
|
||||||
@ -28,7 +29,8 @@ class CustomEncoder(json.JSONEncoder):
|
|||||||
try:
|
try:
|
||||||
encoded = super(CustomEncoder, self).default(obj)
|
encoded = super(CustomEncoder, self).default(obj)
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
obj = obj.decode('utf-8', 'ignore')
|
encoding = sys.getfilesystemencoding()
|
||||||
|
obj = obj.decode(encoding, 'ignore').encode('utf-8')
|
||||||
encoded = super(CustomEncoder, self).default(obj)
|
encoded = super(CustomEncoder, self).default(obj)
|
||||||
return encoded
|
return encoded
|
||||||
|
|
||||||
@ -74,7 +76,7 @@ def setup_logging(args, version):
|
|||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
set_log_level(logger, args)
|
set_log_level(logger, args)
|
||||||
if len(logger.handlers) > 0:
|
if len(logger.handlers) > 0:
|
||||||
formatter = JsonFormatter(datefmt='%a %b %d %H:%M:%S %Z %Y')
|
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
||||||
formatter.setup(
|
formatter.setup(
|
||||||
timestamp=args.timestamp,
|
timestamp=args.timestamp,
|
||||||
isWrite=args.isWrite,
|
isWrite=args.isWrite,
|
||||||
@ -88,7 +90,7 @@ def setup_logging(args, version):
|
|||||||
if not logfile:
|
if not logfile:
|
||||||
logfile = '~/.wakatime.log'
|
logfile = '~/.wakatime.log'
|
||||||
handler = logging.FileHandler(os.path.expanduser(logfile))
|
handler = logging.FileHandler(os.path.expanduser(logfile))
|
||||||
formatter = JsonFormatter(datefmt='%a %b %d %H:%M:%S %Z %Y')
|
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
||||||
formatter.setup(
|
formatter.setup(
|
||||||
timestamp=args.timestamp,
|
timestamp=args.timestamp,
|
||||||
isWrite=args.isWrite,
|
isWrite=args.isWrite,
|
||||||
|
@ -12,25 +12,34 @@
|
|||||||
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.projectmap import ProjectMap
|
||||||
from .projects.subversion import Subversion
|
from .projects.subversion import Subversion
|
||||||
|
from .projects.wakatime import WakaTime
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# List of plugin classes to find a project for the current file path.
|
||||||
|
# Project plugins will be processed with priority in the order below.
|
||||||
PLUGINS = [
|
PLUGINS = [
|
||||||
WakaTime,
|
WakaTime,
|
||||||
|
ProjectMap,
|
||||||
Git,
|
Git,
|
||||||
Mercurial,
|
Mercurial,
|
||||||
Subversion,
|
Subversion,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def find_project(path):
|
def find_project(path, configs=None):
|
||||||
for plugin in PLUGINS:
|
for plugin in PLUGINS:
|
||||||
project = plugin(path)
|
plugin_name = plugin.__name__.lower()
|
||||||
|
plugin_configs = None
|
||||||
|
if configs and configs.has_section(plugin_name):
|
||||||
|
plugin_configs = dict(configs.items(plugin_name))
|
||||||
|
project = plugin(path, configs=plugin_configs)
|
||||||
if project.process():
|
if project.process():
|
||||||
return project
|
return project
|
||||||
return None
|
return None
|
||||||
|
@ -22,18 +22,19 @@ class BaseProject(object):
|
|||||||
be found for the current path.
|
be found for the current path.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, path, configs=None):
|
||||||
self.path = path
|
self.path = path
|
||||||
|
self._configs = configs
|
||||||
|
|
||||||
def type(self):
|
def project_type(self):
|
||||||
""" Returns None if this is the base class.
|
""" Returns None if this is the base class.
|
||||||
Returns the type of project if this is a
|
Returns the type of project if this is a
|
||||||
valid project.
|
valid project.
|
||||||
"""
|
"""
|
||||||
type = self.__class__.__name__.lower()
|
project_type = self.__class__.__name__.lower()
|
||||||
if type == 'baseproject':
|
if project_type == 'baseproject':
|
||||||
type = None
|
project_type = None
|
||||||
return type
|
return project_type
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
""" Processes self.path into a project and
|
""" Processes self.path into a project and
|
||||||
|
@ -13,47 +13,47 @@ import logging
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from .base import BaseProject
|
from .base import BaseProject
|
||||||
try:
|
|
||||||
from collections import OrderedDict
|
|
||||||
except ImportError:
|
|
||||||
from ..packages.ordereddict import OrderedDict
|
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# str is unicode in Python3
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
class Git(BaseProject):
|
class Git(BaseProject):
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
self.config = self._find_config(self.path)
|
self.configFile = self._find_git_config_file(self.path)
|
||||||
if self.config:
|
return self.configFile is not None
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
base = self._project_base()
|
base = self._project_base()
|
||||||
if base:
|
if base:
|
||||||
return os.path.basename(base)
|
return unicode(os.path.basename(base))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def branch(self):
|
def branch(self):
|
||||||
branch = None
|
|
||||||
base = self._project_base()
|
base = self._project_base()
|
||||||
if base:
|
if base:
|
||||||
head = os.path.join(self._project_base(), '.git', 'HEAD')
|
head = os.path.join(self._project_base(), '.git', 'HEAD')
|
||||||
try:
|
try:
|
||||||
with open(head) as f:
|
with open(head) as fh:
|
||||||
branch = f.readline().strip().rsplit('/', 1)[-1]
|
return unicode(fh.readline().strip().rsplit('/', 1)[-1])
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
return branch
|
|
||||||
|
|
||||||
def _project_base(self):
|
|
||||||
if self.config:
|
|
||||||
return os.path.dirname(os.path.dirname(self.config))
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _find_config(self, path):
|
def _project_base(self):
|
||||||
|
if self.configFile:
|
||||||
|
return os.path.dirname(os.path.dirname(self.configFile))
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _find_git_config_file(self, path):
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
path = os.path.split(path)[0]
|
path = os.path.split(path)[0]
|
||||||
@ -62,34 +62,4 @@ class Git(BaseProject):
|
|||||||
split_path = os.path.split(path)
|
split_path = os.path.split(path)
|
||||||
if split_path[1] == '':
|
if split_path[1] == '':
|
||||||
return None
|
return None
|
||||||
return self._find_config(split_path[0])
|
return self._find_git_config_file(split_path[0])
|
||||||
|
|
||||||
def _parse_config(self):
|
|
||||||
sections = {}
|
|
||||||
try:
|
|
||||||
f = open(self.config, 'r')
|
|
||||||
except IOError as e:
|
|
||||||
log.exception("Exception:")
|
|
||||||
else:
|
|
||||||
with f:
|
|
||||||
section = None
|
|
||||||
for line in f.readlines():
|
|
||||||
line = line.lstrip()
|
|
||||||
if len(line) > 0 and line[0] == '[':
|
|
||||||
section = line[1:].split(']', 1)[0]
|
|
||||||
temp = section.split(' ', 1)
|
|
||||||
section = temp[0].lower()
|
|
||||||
if len(temp) > 1:
|
|
||||||
section = ' '.join([section, temp[1]])
|
|
||||||
sections[section] = {}
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
(setting, value) = line.split('=', 1)
|
|
||||||
except ValueError:
|
|
||||||
setting = line.split('#', 1)[0].split(';', 1)[0]
|
|
||||||
value = 'true'
|
|
||||||
setting = setting.strip().lower()
|
|
||||||
value = value.split('#', 1)[0].split(';', 1)[0].strip()
|
|
||||||
sections[section][setting] = value
|
|
||||||
f.close()
|
|
||||||
return sections
|
|
||||||
|
@ -18,13 +18,41 @@ from .base import BaseProject
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# str is unicode in Python3
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
class Mercurial(BaseProject):
|
class Mercurial(BaseProject):
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
return False
|
self.configDir = self._find_hg_config_dir(self.path)
|
||||||
|
return self.configDir is not None
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
|
if self.configDir:
|
||||||
|
return unicode(os.path.basename(os.path.dirname(self.configDir)))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def branch(self):
|
def branch(self):
|
||||||
return None
|
if self.configDir:
|
||||||
|
branch_file = os.path.join(self.configDir, 'branch')
|
||||||
|
try:
|
||||||
|
with open(branch_file) as fh:
|
||||||
|
return unicode(fh.readline().strip().rsplit('/', 1)[-1])
|
||||||
|
except IOError:
|
||||||
|
pass
|
||||||
|
return unicode('default')
|
||||||
|
|
||||||
|
def _find_hg_config_dir(self, path):
|
||||||
|
path = os.path.realpath(path)
|
||||||
|
if os.path.isfile(path):
|
||||||
|
path = os.path.split(path)[0]
|
||||||
|
if os.path.isdir(os.path.join(path, '.hg')):
|
||||||
|
return os.path.join(path, '.hg')
|
||||||
|
split_path = os.path.split(path)
|
||||||
|
if split_path[1] == '':
|
||||||
|
return None
|
||||||
|
return self._find_hg_config_dir(split_path[0])
|
||||||
|
72
packages/wakatime/wakatime/projects/projectmap.py
Normal file
72
packages/wakatime/wakatime/projects/projectmap.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
wakatime.projects.projectmap
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Use the ~/.wakatime.cfg file to set custom project names by
|
||||||
|
recursively matching folder paths.
|
||||||
|
Project maps go under the [projectmap] config section.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[projectmap]
|
||||||
|
/home/user/projects/foo = new project name
|
||||||
|
/home/user/projects/bar = project2
|
||||||
|
|
||||||
|
Will result in file `/home/user/projects/foo/src/main.c` to have
|
||||||
|
project name `new project name`.
|
||||||
|
|
||||||
|
:copyright: (c) 2013 Alan Hamlett.
|
||||||
|
:license: BSD, see LICENSE for more details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from .base import BaseProject
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# str is unicode in Python3
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
|
class ProjectMap(BaseProject):
|
||||||
|
|
||||||
|
def process(self):
|
||||||
|
if not self._configs:
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.project = self._find_project(self.path)
|
||||||
|
|
||||||
|
return self.project is not None
|
||||||
|
|
||||||
|
def _find_project(self, path):
|
||||||
|
path = os.path.realpath(path)
|
||||||
|
if os.path.isfile(path):
|
||||||
|
path = os.path.split(path)[0]
|
||||||
|
|
||||||
|
if self._configs.get(path.lower()):
|
||||||
|
return self._configs.get(path.lower())
|
||||||
|
if self._configs.get('%s/' % path.lower()):
|
||||||
|
return self._configs.get('%s/' % path.lower())
|
||||||
|
if self._configs.get('%s\\' % path.lower()):
|
||||||
|
return self._configs.get('%s\\' % path.lower())
|
||||||
|
|
||||||
|
split_path = os.path.split(path)
|
||||||
|
if split_path[1] == '':
|
||||||
|
return None
|
||||||
|
return self._find_project(split_path[0])
|
||||||
|
|
||||||
|
def branch(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def name(self):
|
||||||
|
if self.project:
|
||||||
|
return unicode(self.project)
|
||||||
|
return None
|
@ -24,24 +24,31 @@ except ImportError:
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# str is unicode in Python3
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
class Subversion(BaseProject):
|
class Subversion(BaseProject):
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
return self._find_project_base(self.path)
|
return self._find_project_base(self.path)
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
return self.info['Repository Root'].split('/')[-1]
|
return unicode(self.info['Repository Root'].split('/')[-1])
|
||||||
|
|
||||||
def branch(self):
|
def branch(self):
|
||||||
branch = None
|
|
||||||
if self.base:
|
if self.base:
|
||||||
branch = os.path.basename(self.base)
|
unicode(os.path.basename(self.base))
|
||||||
return branch
|
return None
|
||||||
|
|
||||||
def _get_info(self, path):
|
def _get_info(self, path):
|
||||||
info = OrderedDict()
|
info = OrderedDict()
|
||||||
stdout = None
|
stdout = None
|
||||||
try:
|
try:
|
||||||
|
os.environ['LANG'] = 'en_US'
|
||||||
stdout, stderr = Popen([
|
stdout, stderr = Popen([
|
||||||
'svn', 'info', os.path.realpath(path)
|
'svn', 'info', os.path.realpath(path)
|
||||||
], stdout=PIPE, stderr=PIPE).communicate()
|
], stdout=PIPE, stderr=PIPE).communicate()
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
Information from a .wakatime-project file about the project for
|
Information from a .wakatime-project file about the project for
|
||||||
a given file.
|
a given file. First line of .wakatime-project sets the project
|
||||||
|
name. Second line sets the current branch name.
|
||||||
|
|
||||||
:copyright: (c) 2013 Alan Hamlett.
|
:copyright: (c) 2013 Alan Hamlett.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
@ -19,25 +20,37 @@ from .base import BaseProject
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
# str is unicode in Python3
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
class WakaTime(BaseProject):
|
class WakaTime(BaseProject):
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
self.config = self._find_config(self.path)
|
self.config = self._find_config(self.path)
|
||||||
|
self._project_name = None
|
||||||
|
self._project_branch = None
|
||||||
|
|
||||||
if self.config:
|
if self.config:
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(self.config) as fh:
|
||||||
|
self._project_name = unicode(fh.readline().strip())
|
||||||
|
self._project_branch = unicode(fh.readline().strip())
|
||||||
|
except IOError as e:
|
||||||
|
log.exception("Exception:")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
project_name = None
|
return self._project_name
|
||||||
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):
|
def branch(self):
|
||||||
return None
|
return self._project_branch
|
||||||
|
|
||||||
def _find_config(self, path):
|
def _find_config(self, path):
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
|
114
packages/wakatime/wakatime/queue.py
Normal file
114
packages/wakatime/wakatime/queue.py
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
wakatime.queue
|
||||||
|
~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Queue for offline time logging.
|
||||||
|
http://wakatime.com
|
||||||
|
|
||||||
|
:copyright: (c) 2014 Alan Hamlett.
|
||||||
|
:license: BSD, see LICENSE for more details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sqlite3
|
||||||
|
import traceback
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Queue(object):
|
||||||
|
DB_FILE = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
conn = sqlite3.connect(self.DB_FILE)
|
||||||
|
c = conn.cursor()
|
||||||
|
c.execute('''CREATE TABLE IF NOT EXISTS action (
|
||||||
|
file text,
|
||||||
|
time real,
|
||||||
|
project text,
|
||||||
|
language text,
|
||||||
|
lines integer,
|
||||||
|
branch text,
|
||||||
|
is_write integer,
|
||||||
|
plugin text)
|
||||||
|
''')
|
||||||
|
return (conn, c)
|
||||||
|
|
||||||
|
|
||||||
|
def push(self, data, plugin):
|
||||||
|
try:
|
||||||
|
conn, c = self.connect()
|
||||||
|
action = {
|
||||||
|
'file': data.get('file'),
|
||||||
|
'time': data.get('time'),
|
||||||
|
'project': data.get('project'),
|
||||||
|
'language': data.get('language'),
|
||||||
|
'lines': data.get('lines'),
|
||||||
|
'branch': data.get('branch'),
|
||||||
|
'is_write': 1 if data.get('is_write') else 0,
|
||||||
|
'plugin': plugin,
|
||||||
|
}
|
||||||
|
c.execute('INSERT INTO action VALUES (:file,:time,:project,:language,:lines,:branch,:is_write,:plugin)', action)
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
except sqlite3.Error, e:
|
||||||
|
log.error(str(e))
|
||||||
|
|
||||||
|
|
||||||
|
def pop(self):
|
||||||
|
tries = 3
|
||||||
|
wait = 0.1
|
||||||
|
action = None
|
||||||
|
try:
|
||||||
|
conn, c = self.connect()
|
||||||
|
except sqlite3.Error, e:
|
||||||
|
log.debug(traceback.format_exc())
|
||||||
|
return None
|
||||||
|
loop = True
|
||||||
|
while loop and tries > -1:
|
||||||
|
try:
|
||||||
|
c.execute('BEGIN IMMEDIATE')
|
||||||
|
c.execute('SELECT * FROM action LIMIT 1')
|
||||||
|
row = c.fetchone()
|
||||||
|
if row is not None:
|
||||||
|
values = []
|
||||||
|
clauses = []
|
||||||
|
index = 0
|
||||||
|
for row_name in ['file', 'time', 'project', 'language', 'lines', 'branch', 'is_write']:
|
||||||
|
if row[index] is not None:
|
||||||
|
clauses.append('{0}=?'.format(row_name))
|
||||||
|
values.append(row[index])
|
||||||
|
else:
|
||||||
|
clauses.append('{0} IS NULL'.format(row_name))
|
||||||
|
index += 1
|
||||||
|
if len(values) > 0:
|
||||||
|
c.execute('DELETE FROM action WHERE {0}'.format(u' AND '.join(clauses)), values)
|
||||||
|
else:
|
||||||
|
c.execute('DELETE FROM action WHERE {0}'.format(u' AND '.join(clauses)))
|
||||||
|
conn.commit()
|
||||||
|
if row is not None:
|
||||||
|
action = {
|
||||||
|
'file': row[0],
|
||||||
|
'time': row[1],
|
||||||
|
'project': row[2],
|
||||||
|
'language': row[3],
|
||||||
|
'lines': row[4],
|
||||||
|
'branch': row[5],
|
||||||
|
'is_write': True if row[6] is 1 else False,
|
||||||
|
'plugin': row[7],
|
||||||
|
}
|
||||||
|
loop = False
|
||||||
|
except sqlite3.Error, e:
|
||||||
|
log.debug(traceback.format_exc())
|
||||||
|
sleep(wait)
|
||||||
|
tries -= 1
|
||||||
|
try:
|
||||||
|
conn.close()
|
||||||
|
except sqlite3.Error, e:
|
||||||
|
log.debug(traceback.format_exc())
|
||||||
|
return action
|
@ -28,6 +28,7 @@ EXTENSIONS = {
|
|||||||
'j2': 'HTML',
|
'j2': 'HTML',
|
||||||
'markdown': 'Markdown',
|
'markdown': 'Markdown',
|
||||||
'md': 'Markdown',
|
'md': 'Markdown',
|
||||||
|
'twig': 'Twig',
|
||||||
}
|
}
|
||||||
TRANSLATIONS = {
|
TRANSLATIONS = {
|
||||||
'CSS+Genshi Text': 'CSS',
|
'CSS+Genshi Text': 'CSS',
|
||||||
|
Reference in New Issue
Block a user