option for obfuscating file names called --hidefilenames
This commit is contained in:
parent
3759127e9a
commit
1055a25aff
2 changed files with 56 additions and 16 deletions
|
@ -54,6 +54,11 @@ except:
|
||||||
|
|
||||||
log = logging.getLogger('WakaTime')
|
log = logging.getLogger('WakaTime')
|
||||||
|
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
class FileAction(argparse.Action):
|
class FileAction(argparse.Action):
|
||||||
|
|
||||||
|
@ -161,6 +166,9 @@ def parseArguments(argv):
|
||||||
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')
|
||||||
|
parser.add_argument('--hidefilenames', dest='hidefilenames',
|
||||||
|
action='store_true',
|
||||||
|
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',
|
||||||
|
@ -203,6 +211,8 @@ def parseArguments(argv):
|
||||||
pass
|
pass
|
||||||
if args.offline and configs.has_option('settings', 'offline'):
|
if args.offline and configs.has_option('settings', 'offline'):
|
||||||
args.offline = configs.getboolean('settings', 'offline')
|
args.offline = configs.getboolean('settings', 'offline')
|
||||||
|
if not args.hidefilenames and configs.has_option('settings', 'hidefilenames'):
|
||||||
|
args.hidefilenames = configs.getboolean('settings', 'hidefilenames')
|
||||||
if not args.verbose and configs.has_option('settings', 'verbose'):
|
if not args.verbose and configs.has_option('settings', 'verbose'):
|
||||||
args.verbose = configs.getboolean('settings', 'verbose')
|
args.verbose = configs.getboolean('settings', 'verbose')
|
||||||
if not args.verbose and configs.has_option('settings', 'debug'):
|
if not args.verbose and configs.has_option('settings', 'debug'):
|
||||||
|
@ -221,7 +231,10 @@ def should_ignore(fileName, patterns):
|
||||||
if compiled.search(fileName):
|
if compiled.search(fileName):
|
||||||
return pattern
|
return pattern
|
||||||
except re.error as ex:
|
except re.error as ex:
|
||||||
log.warning('Regex error (%s) for ignore pattern: %s' % (str(ex), pattern))
|
log.warning(unicode('Regex error ({msg}) for ignore pattern: {pattern}').format(
|
||||||
|
msg=str(ex),
|
||||||
|
pattern=pattern,
|
||||||
|
))
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
return False
|
return False
|
||||||
|
@ -230,21 +243,34 @@ def should_ignore(fileName, patterns):
|
||||||
def get_user_agent(plugin):
|
def get_user_agent(plugin):
|
||||||
ver = sys.version_info
|
ver = sys.version_info
|
||||||
python_version = '%d.%d.%d.%s.%d' % (ver[0], ver[1], ver[2], ver[3], ver[4])
|
python_version = '%d.%d.%d.%s.%d' % (ver[0], ver[1], ver[2], ver[3], ver[4])
|
||||||
user_agent = 'wakatime/%s (%s) Python%s' % (__version__,
|
user_agent = unicode('wakatime/{ver} ({platform}) Python{py_ver}').format(
|
||||||
platform.platform(), python_version)
|
ver=__version__,
|
||||||
|
platform=platform.platform(),
|
||||||
|
py_ver=python_version,
|
||||||
|
)
|
||||||
if plugin:
|
if plugin:
|
||||||
user_agent = user_agent+' '+plugin
|
user_agent = unicode('{user_agent} {plugin}').format(
|
||||||
|
user_agent=user_agent,
|
||||||
|
plugin=plugin,
|
||||||
|
)
|
||||||
return user_agent
|
return user_agent
|
||||||
|
|
||||||
|
|
||||||
def send_action(project=None, branch=None, stats=None, key=None, targetFile=None,
|
def send_action(project=None, branch=None, stats=None, key=None, targetFile=None,
|
||||||
timestamp=None, isWrite=None, plugin=None, offline=None, **kwargs):
|
timestamp=None, isWrite=None, plugin=None, offline=None,
|
||||||
|
hidefilenames=None, **kwargs):
|
||||||
url = 'https://wakatime.com/api/v1/actions'
|
url = 'https://wakatime.com/api/v1/actions'
|
||||||
log.debug('Sending action 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:
|
||||||
|
data['file'] = data['file'].rsplit('/', 1)[-1].rsplit('\\', 1)[-1]
|
||||||
|
if len(data['file'].strip('.').split('.', 1)) > 1:
|
||||||
|
data['file'] = unicode('HIDDEN.{ext}').format(ext=data['file'].strip('.').rsplit('.', 1)[-1])
|
||||||
|
else:
|
||||||
|
data['file'] = unicode('HIDDEN')
|
||||||
if stats.get('lines'):
|
if stats.get('lines'):
|
||||||
data['lines'] = stats['lines']
|
data['lines'] = stats['lines']
|
||||||
if stats.get('language'):
|
if stats.get('language'):
|
||||||
|
@ -258,16 +284,16 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
|
||||||
log.debug(data)
|
log.debug(data)
|
||||||
|
|
||||||
# setup api request
|
# setup api request
|
||||||
request = Request(url=url, data=str.encode(json.dumps(data)))
|
request = Request(url=url, data=json.dumps(data))
|
||||||
request.add_header('User-Agent', get_user_agent(plugin))
|
request.add_header('User-Agent', get_user_agent(plugin))
|
||||||
request.add_header('Content-Type', 'application/json')
|
request.add_header('Content-Type', 'application/json')
|
||||||
auth = 'Basic %s' % bytes.decode(base64.b64encode(str.encode(key)))
|
auth = unicode('Basic {key}').format(key=bytes.decode(base64.b64encode(str.encode(key))))
|
||||||
request.add_header('Authorization', auth)
|
request.add_header('Authorization', auth)
|
||||||
|
|
||||||
# add Olson timezone to request
|
# add Olson timezone to request
|
||||||
tz = tzlocal.get_localzone()
|
tz = tzlocal.get_localzone()
|
||||||
if tz:
|
if tz:
|
||||||
request.add_header('TimeZone', str(tz.zone))
|
request.add_header('TimeZone', unicode(tz.zone))
|
||||||
|
|
||||||
# log time to api
|
# log time to api
|
||||||
response = None
|
response = None
|
||||||
|
@ -276,7 +302,7 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
|
||||||
except HTTPError as exc:
|
except HTTPError as exc:
|
||||||
exception_data = {
|
exception_data = {
|
||||||
'response_code': exc.getcode(),
|
'response_code': exc.getcode(),
|
||||||
sys.exc_info()[0].__name__: str(sys.exc_info()[1]),
|
sys.exc_info()[0].__name__: unicode(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()
|
||||||
|
@ -289,7 +315,7 @@ def send_action(project=None, branch=None, stats=None, key=None, targetFile=None
|
||||||
log.error(exception_data)
|
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__: unicode(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()
|
||||||
|
@ -339,7 +365,9 @@ def main(argv=None):
|
||||||
|
|
||||||
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(unicode('File ignored because matches pattern: {pattern}').format(
|
||||||
|
pattern=ignore,
|
||||||
|
))
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if os.path.isfile(args.targetFile):
|
if os.path.isfile(args.targetFile):
|
||||||
|
@ -364,9 +392,16 @@ def main(argv=None):
|
||||||
action = queue.pop()
|
action = queue.pop()
|
||||||
if action is None:
|
if action is None:
|
||||||
break
|
break
|
||||||
if not send_action(project=action['project'], targetFile=action['file'], timestamp=action['time'],
|
sent = send_action(project=action['project'],
|
||||||
branch=action['branch'], stats={'language': action['language'], 'lines': action['lines']},
|
targetFile=action['file'],
|
||||||
key=args.key, isWrite=action['is_write'], plugin=action['plugin'], offline=args.offline):
|
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,
|
||||||
|
hidefilenames=args.hidefilenames)
|
||||||
|
if not sent:
|
||||||
break
|
break
|
||||||
return 0 # success
|
return 0 # success
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,11 @@ from pygments.lexers import guess_lexer_for_filename
|
||||||
|
|
||||||
log = logging.getLogger('WakaTime')
|
log = logging.getLogger('WakaTime')
|
||||||
|
|
||||||
|
try:
|
||||||
|
unicode
|
||||||
|
except NameError:
|
||||||
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
# force file name extensions to be recognized as a certain language
|
# force file name extensions to be recognized as a certain language
|
||||||
EXTENSIONS = {
|
EXTENSIONS = {
|
||||||
|
@ -54,7 +59,7 @@ def guess_language(file_name):
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
if lexer:
|
if lexer:
|
||||||
return translate_language(str(lexer.name))
|
return translate_language(unicode(lexer.name))
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue