new --extra-heartbeats argument
This commit is contained in:
parent
984a8ab62a
commit
67a2403070
5 changed files with 107 additions and 45 deletions
|
@ -4,7 +4,7 @@ usage: wakatime [-h] [--entity FILE] [--key KEY] [--write] [--plugin PLUGIN]
|
||||||
[--alternate-project ALTERNATE_PROJECT]
|
[--alternate-project ALTERNATE_PROJECT]
|
||||||
[--alternate-language ALTERNATE_LANGUAGE]
|
[--alternate-language ALTERNATE_LANGUAGE]
|
||||||
[--hostname HOSTNAME] [--disableoffline] [--hidefilenames]
|
[--hostname HOSTNAME] [--disableoffline] [--hidefilenames]
|
||||||
[--exclude EXCLUDE] [--include INCLUDE] [--logfile LOGFILE]
|
[--exclude EXCLUDE] [--include INCLUDE] [--extra-heartbeats]
|
||||||
[--apiurl API_URL] [--timeout TIMEOUT] [--config CONFIG]
|
[--logfile LOGFILE] [--apiurl API_URL] [--timeout TIMEOUT]
|
||||||
[--verbose] [--version]
|
[--config CONFIG] [--verbose] [--version]
|
||||||
wakatime: error: argument --timeout: invalid int value: 'abc'
|
wakatime: error: argument --timeout: invalid int value: 'abc'
|
||||||
|
|
|
@ -4,9 +4,9 @@ usage: wakatime [-h] [--entity FILE] [--key KEY] [--write] [--plugin PLUGIN]
|
||||||
[--alternate-project ALTERNATE_PROJECT]
|
[--alternate-project ALTERNATE_PROJECT]
|
||||||
[--alternate-language ALTERNATE_LANGUAGE]
|
[--alternate-language ALTERNATE_LANGUAGE]
|
||||||
[--hostname HOSTNAME] [--disableoffline] [--hidefilenames]
|
[--hostname HOSTNAME] [--disableoffline] [--hidefilenames]
|
||||||
[--exclude EXCLUDE] [--include INCLUDE] [--logfile LOGFILE]
|
[--exclude EXCLUDE] [--include INCLUDE] [--extra-heartbeats]
|
||||||
[--apiurl API_URL] [--timeout TIMEOUT] [--config CONFIG]
|
[--logfile LOGFILE] [--apiurl API_URL] [--timeout TIMEOUT]
|
||||||
[--verbose] [--version]
|
[--config CONFIG] [--verbose] [--version]
|
||||||
|
|
||||||
Common interface for the WakaTime api.
|
Common interface for the WakaTime api.
|
||||||
|
|
||||||
|
@ -46,6 +46,8 @@ optional arguments:
|
||||||
--include INCLUDE filename patterns to log; when used in combination
|
--include INCLUDE filename patterns to log; when used in combination
|
||||||
with --exclude, files matching include will still be
|
with --exclude, files matching include will still be
|
||||||
logged; POSIX regex syntax; can be used more than once
|
logged; POSIX regex syntax; can be used more than once
|
||||||
|
--extra-heartbeats reads extra heartbeats from STDIN as a JSON array
|
||||||
|
until EOF
|
||||||
--logfile LOGFILE defaults to ~/.wakatime.log
|
--logfile LOGFILE defaults to ~/.wakatime.log
|
||||||
--apiurl API_URL heartbeats api url; for debugging with a local server
|
--apiurl API_URL heartbeats api url; for debugging with a local server
|
||||||
--timeout TIMEOUT number of seconds to wait when sending heartbeats to
|
--timeout TIMEOUT number of seconds to wait when sending heartbeats to
|
||||||
|
|
|
@ -4,7 +4,7 @@ usage: wakatime [-h] [--entity FILE] [--key KEY] [--write] [--plugin PLUGIN]
|
||||||
[--alternate-project ALTERNATE_PROJECT]
|
[--alternate-project ALTERNATE_PROJECT]
|
||||||
[--alternate-language ALTERNATE_LANGUAGE]
|
[--alternate-language ALTERNATE_LANGUAGE]
|
||||||
[--hostname HOSTNAME] [--disableoffline] [--hidefilenames]
|
[--hostname HOSTNAME] [--disableoffline] [--hidefilenames]
|
||||||
[--exclude EXCLUDE] [--include INCLUDE] [--logfile LOGFILE]
|
[--exclude EXCLUDE] [--include INCLUDE] [--extra-heartbeats]
|
||||||
[--apiurl API_URL] [--timeout TIMEOUT] [--config CONFIG]
|
[--logfile LOGFILE] [--apiurl API_URL] [--timeout TIMEOUT]
|
||||||
[--verbose] [--version]
|
[--config CONFIG] [--verbose] [--version]
|
||||||
wakatime: error: Missing api key
|
wakatime: error: Missing api key
|
||||||
|
|
|
@ -23,9 +23,9 @@ try:
|
||||||
except (ImportError, SyntaxError):
|
except (ImportError, SyntaxError):
|
||||||
import json
|
import json
|
||||||
try:
|
try:
|
||||||
from mock import ANY
|
from mock import ANY, call
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from unittest.mock import ANY
|
from unittest.mock import ANY, call
|
||||||
from wakatime.packages import tzlocal
|
from wakatime.packages import tzlocal
|
||||||
|
|
||||||
|
|
||||||
|
@ -647,3 +647,35 @@ class BaseTestCase(utils.TestCase):
|
||||||
timezone = tzlocal.get_localzone()
|
timezone = tzlocal.get_localzone()
|
||||||
headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers
|
headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers
|
||||||
self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone)
|
self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone)
|
||||||
|
|
||||||
|
def test_extra_heartbeats_argument(self):
|
||||||
|
response = Response()
|
||||||
|
response.status_code = 201
|
||||||
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
||||||
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
args = ['--file', entity, '--config', config, '--extra-heartbeats']
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.main.sys.stdin') as mock_stdin:
|
||||||
|
now = int(time.time())
|
||||||
|
heartbeats = json.dumps([{
|
||||||
|
'timestamp': now,
|
||||||
|
'entity': entity,
|
||||||
|
'entity_type': 'file',
|
||||||
|
'is_write': True,
|
||||||
|
}])
|
||||||
|
mock_stdin.read.return_value = heartbeats
|
||||||
|
|
||||||
|
retval = execute(args)
|
||||||
|
|
||||||
|
self.assertEquals(retval, SUCCESS)
|
||||||
|
self.assertEquals(sys.stdout.getvalue(), '')
|
||||||
|
self.assertEquals(sys.stderr.getvalue(), '')
|
||||||
|
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_has_calls([call(), call()])
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
|
||||||
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_has_calls([call(ANY), call(ANY)])
|
||||||
|
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
||||||
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_has_calls([call(), call()])
|
||||||
|
|
|
@ -153,6 +153,9 @@ def parseArguments():
|
||||||
'POSIX regex syntax; can be used more than once')
|
'POSIX regex syntax; can be used more than once')
|
||||||
parser.add_argument('--ignore', dest='ignore', action='append',
|
parser.add_argument('--ignore', dest='ignore', action='append',
|
||||||
help=argparse.SUPPRESS)
|
help=argparse.SUPPRESS)
|
||||||
|
parser.add_argument('--extra-heartbeats', dest='extra_heartbeats',
|
||||||
|
action='store_true',
|
||||||
|
help='reads extra heartbeats from STDIN as a JSON array until EOF')
|
||||||
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('--apiurl', dest='api_url',
|
parser.add_argument('--apiurl', dest='api_url',
|
||||||
|
@ -452,17 +455,7 @@ def sync_offline_heartbeats(args, hostname):
|
||||||
return SUCCESS
|
return SUCCESS
|
||||||
|
|
||||||
|
|
||||||
def execute(argv=None):
|
def process_heartbeat(args, configs, heartbeat):
|
||||||
if argv:
|
|
||||||
sys.argv = ['wakatime'] + argv
|
|
||||||
|
|
||||||
args, configs = parseArguments()
|
|
||||||
if configs is None:
|
|
||||||
return CONFIG_FILE_PARSE_ERROR
|
|
||||||
|
|
||||||
setup_logging(args, __version__)
|
|
||||||
|
|
||||||
try:
|
|
||||||
exclude = should_exclude(args.entity, args.include, args.exclude)
|
exclude = should_exclude(args.entity, args.include, args.exclude)
|
||||||
if exclude is not False:
|
if exclude is not False:
|
||||||
log.debug(u('Skipping because matches exclude pattern: {pattern}').format(
|
log.debug(u('Skipping because matches exclude pattern: {pattern}').format(
|
||||||
|
@ -501,6 +494,41 @@ def execute(argv=None):
|
||||||
else:
|
else:
|
||||||
log.debug('File does not exist; ignoring this heartbeat.')
|
log.debug('File does not exist; ignoring this heartbeat.')
|
||||||
return SUCCESS
|
return SUCCESS
|
||||||
|
|
||||||
|
|
||||||
|
def execute(argv=None):
|
||||||
|
if argv:
|
||||||
|
sys.argv = ['wakatime'] + argv
|
||||||
|
|
||||||
|
args, configs = parseArguments()
|
||||||
|
if configs is None:
|
||||||
|
return CONFIG_FILE_PARSE_ERROR
|
||||||
|
|
||||||
|
setup_logging(args, __version__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
heartbeat = {
|
||||||
|
'timestamp': args.timestamp,
|
||||||
|
'entity': args.entity,
|
||||||
|
'entity_type': args.entity_type,
|
||||||
|
'project': args.project,
|
||||||
|
'is_write': args.isWrite,
|
||||||
|
'lineno': args.lineno,
|
||||||
|
'cursorpos': args.cursorpos,
|
||||||
|
'alternate_project': args.alternate_project,
|
||||||
|
'alternate_language': args.alternate_language,
|
||||||
|
}
|
||||||
|
retval = process_heartbeat(args, configs, heartbeat)
|
||||||
|
|
||||||
|
if args.extra_heartbeats:
|
||||||
|
heartbeats = json.loads(sys.stdin.read())
|
||||||
|
for heartbeat in heartbeats:
|
||||||
|
retval = process_heartbeat(args, configs, heartbeat)
|
||||||
|
|
||||||
|
return retval
|
||||||
|
|
||||||
except:
|
except:
|
||||||
log.traceback()
|
log.traceback()
|
||||||
|
print(traceback.format_exc())
|
||||||
return UNKNOWN_ERROR
|
return UNKNOWN_ERROR
|
||||||
|
|
Loading…
Reference in a new issue