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('--time', dest='timestamp', metavar='time',
type=float,
help='optional floating-point unix epoch timestamp; '+
'uses current time by default')
parser.add_argument('--write', dest='isWrite',
action='store_true',
help='note 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('--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 wakatime 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('--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',
type=float,
help='optional floating-point unix epoch timestamp; '+
'uses current time by default')
parser.add_argument('--notfile', dest='notfile', action='store_true',
help='when set, will accept any value for the file. for example, '+
'a domain name or other item you want to log time towards.')
parser.add_argument('--project', dest='project_name',
help='optional project name; will auto-discover 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,19 +226,20 @@ def parseArguments(argv):
def should_ignore(fileName, patterns): def should_ignore(fileName, patterns):
try: if fileName is not None and fileName.strip() != '':
for pattern in patterns: try:
try: for pattern in patterns:
compiled = re.compile(pattern, re.IGNORECASE) try:
if compiled.search(fileName): compiled = re.compile(pattern, re.IGNORECASE)
return pattern if compiled.search(fileName):
except re.error as ex: return pattern
log.warning(u('Regex error ({msg}) for ignore pattern: {pattern}').format( except re.error as ex:
msg=u(ex), log.warning(u('Regex error ({msg}) for ignore pattern: {pattern}').format(
pattern=u(pattern), msg=u(ex),
)) pattern=u(pattern),
except TypeError: ))
pass except TypeError:
pass
return False return False
@ -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,11 +398,13 @@ 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 = find_project(args.targetFile, configs=configs) project = None
if not args.notfile:
project = find_project(args.targetFile, configs=configs)
branch = None branch = None
project_name = args.project_name project_name = args.project_name
if project: if project:
@ -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,13 +86,20 @@ 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):
language, lexer = guess_language(file_name) if notfile:
parser = DependencyParser(file_name, lexer) stats = {
dependencies = parser.parse() 'language': None,
stats = { 'dependencies': [],
'language': language, 'lines': None,
'dependencies': dependencies, }
'lines': number_lines_in_file(file_name), else:
} language, lexer = guess_language(file_name)
parser = DependencyParser(file_name, lexer)
dependencies = parser.parse()
stats = {
'language': language,
'dependencies': dependencies,
'lines': number_lines_in_file(file_name),
}
return stats return stats