new command line argument --notfile for logging time towards things other than files

This commit is contained in:
Alan Hamlett 2015-02-12 17:27:21 -08:00
parent 902ba42623
commit a4f7c6bc32
2 changed files with 57 additions and 41 deletions

View file

@ -143,21 +143,25 @@ def parseArguments(argv):
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 heartbeat') help='absolute path to file for current heartbeat')
parser.add_argument('--key', dest='key',
help='your wakatime api key; uses api_key from '+
'~/.wakatime.conf by default')
parser.add_argument('--write', dest='isWrite',
action='store_true',
help='when set, tells api this heartbeat was triggered from '+
'writing to a file')
parser.add_argument('--plugin', dest='plugin',
help='optional text editor plugin name and version '+
'for User-Agent header')
parser.add_argument('--time', dest='timestamp', metavar='time', parser.add_argument('--time', dest='timestamp', metavar='time',
type=float, type=float,
help='optional floating-point unix epoch timestamp; '+ help='optional floating-point unix epoch timestamp; '+
'uses current time by default') 'uses current time by default')
parser.add_argument('--write', dest='isWrite', parser.add_argument('--notfile', dest='notfile', action='store_true',
action='store_true', help='when set, will accept any value for the file. for example, '+
help='note heartbeat was triggered from writing to a file') 'a domain name or other item you want to log time towards.')
parser.add_argument('--plugin', dest='plugin',
help='optional text editor plugin name and version '+
'for User-Agent header')
parser.add_argument('--project', dest='project_name', parser.add_argument('--project', dest='project_name',
help='optional project name; will auto-discover by default') help='optional project name; will auto-discover by default')
parser.add_argument('--key', dest='key',
help='your wakatime api key; uses api_key from '+
'~/.wakatime.conf by default')
parser.add_argument('--disableoffline', dest='offline', parser.add_argument('--disableoffline', dest='offline',
action='store_false', action='store_false',
help='disables offline time logging instead of queuing logged time') help='disables offline time logging instead of queuing logged time')
@ -165,7 +169,8 @@ def parseArguments(argv):
action='store_true', action='store_true',
help='obfuscate file names; will not send file names to api') help='obfuscate file names; will not send file names to api')
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',
help='defaults to ~/.wakatime.log') help='defaults to ~/.wakatime.log')
parser.add_argument('--config', dest='config', parser.add_argument('--config', dest='config',
@ -221,6 +226,7 @@ def parseArguments(argv):
def should_ignore(fileName, patterns): def should_ignore(fileName, patterns):
if fileName is not None and fileName.strip() != '':
try: try:
for pattern in patterns: for pattern in patterns:
try: try:
@ -255,14 +261,14 @@ def get_user_agent(plugin):
def send_heartbeat(project=None, branch=None, stats={}, key=None, targetFile=None, def send_heartbeat(project=None, branch=None, stats={}, key=None, targetFile=None,
timestamp=None, isWrite=None, plugin=None, offline=None, timestamp=None, isWrite=None, plugin=None, offline=None,
hidefilenames=None, **kwargs): hidefilenames=None, notfile=False, **kwargs):
url = 'https://wakatime.com/api/v1/heartbeats' url = 'https://wakatime.com/api/v1/heartbeats'
log.debug('Sending heartbeat to api at %s' % url) log.debug('Sending heartbeat to api at %s' % url)
data = { data = {
'time': timestamp, 'time': timestamp,
'file': targetFile, 'file': targetFile,
} }
if hidefilenames and targetFile is not None: if hidefilenames and targetFile is not None and not notfile:
data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1] data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1]
if len(data['file'].strip('.').split('.', 1)) > 1: if len(data['file'].strip('.').split('.', 1)) > 1:
data['file'] = u('HIDDEN.{ext}').format(ext=u(data['file'].strip('.').rsplit('.', 1)[-1])) data['file'] = u('HIDDEN.{ext}').format(ext=u(data['file'].strip('.').rsplit('.', 1)[-1]))
@ -392,10 +398,12 @@ def main(argv=None):
)) ))
return 0 return 0
if os.path.isfile(args.targetFile): if os.path.isfile(args.targetFile) or args.notfile:
stats = get_file_stats(args.targetFile) stats = get_file_stats(args.targetFile, notfile=args.notfile)
project = None
if not args.notfile:
project = find_project(args.targetFile, configs=configs) project = find_project(args.targetFile, configs=configs)
branch = None branch = None
project_name = args.project_name project_name = args.project_name
@ -423,7 +431,8 @@ def main(argv=None):
isWrite=heartbeat['is_write'], isWrite=heartbeat['is_write'],
plugin=heartbeat['plugin'], plugin=heartbeat['plugin'],
offline=args.offline, offline=args.offline,
hidefilenames=args.hidefilenames) hidefilenames=args.hidefilenames,
notfile=args.notfile)
if not sent: if not sent:
break break
return 0 # success return 0 # success

View file

@ -86,7 +86,14 @@ def number_lines_in_file(file_name):
return lines return lines
def get_file_stats(file_name): def get_file_stats(file_name, notfile=False):
if notfile:
stats = {
'language': None,
'dependencies': [],
'lines': None,
}
else:
language, lexer = guess_language(file_name) language, lexer = guess_language(file_name)
parser = DependencyParser(file_name, lexer) parser = DependencyParser(file_name, lexer)
dependencies = parser.parse() dependencies = parser.parse()