From 7f198639aad499d9f06f8ee9d9686a03ed960ad1 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Wed, 1 Feb 2017 23:31:30 -0800 Subject: [PATCH] allow list of regex patterns for hiding file names --- tests/samples/configs/hide_file_names.cfg | 6 ++ tests/test_main.py | 94 ++++++++++++++++++++++- wakatime/main.py | 77 +++++++++++-------- 3 files changed, 146 insertions(+), 31 deletions(-) create mode 100644 tests/samples/configs/hide_file_names.cfg diff --git a/tests/samples/configs/hide_file_names.cfg b/tests/samples/configs/hide_file_names.cfg new file mode 100644 index 0000000..6d7f0df --- /dev/null +++ b/tests/samples/configs/hide_file_names.cfg @@ -0,0 +1,6 @@ +[settings] +debug = false +api_key = 1234 +hidefilenames = + missingfile + twolinefile\.txt$ diff --git a/tests/test_main.py b/tests/test_main.py index 5a4874a..10ad4c3 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -325,7 +325,7 @@ class MainTestCase(utils.TestCase): self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() - def test_hidden_filename(self): + def test_hide_all_filenames(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response @@ -371,6 +371,98 @@ class MainTestCase(utils.TestCase): self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + def test_hide_matching_filenames(self): + response = Response() + response.status_code = 0 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) + + now = u(int(time.time())) + config = 'tests/samples/configs/hide_file_names.cfg' + + args = ['--file', entity, '--key', '123', '--config', config, '--time', now] + + retval = execute(args) + self.assertEquals(retval, API_ERROR) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') + + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + + heartbeat = { + 'language': 'Text only', + 'lines': 2, + 'entity': 'HIDDEN.txt', + 'project': os.path.basename(os.path.abspath('.')), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): [], + u('language'): u('Text only'), + u('lineno'): None, + u('lines'): 2, + } + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + + def test_does_not_hide_unmatching_filenames(self): + response = Response() + response.status_code = 0 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/emptyfile.txt' + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) + + now = u(int(time.time())) + config = 'tests/samples/configs/hide_file_names.cfg' + + args = ['--file', entity, '--key', '123', '--config', config, '--time', now] + + retval = execute(args) + self.assertEquals(retval, API_ERROR) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') + + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + + heartbeat = { + 'language': 'Text only', + 'lines': 0, + 'entity': entity, + 'project': os.path.basename(os.path.abspath('.')), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): [], + u('language'): u('Text only'), + u('lineno'): None, + u('lines'): 0, + } + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + def test_invalid_timeout_passed_via_command_line(self): response = Response() response.status_code = 201 diff --git a/wakatime/main.py b/wakatime/main.py index 83b772a..237560c 100644 --- a/wakatime/main.py +++ b/wakatime/main.py @@ -238,10 +238,23 @@ def parseArguments(): args.include.append(pattern) except TypeError: # pragma: nocover pass + if args.hidefilenames: + args.hidefilenames = ['.*'] + else: + args.hidefilenames = [] + if configs.has_option('settings', 'hidefilenames'): + option = configs.get('settings', 'hidefilenames').strip() + if option.lower() == 'true': + args.hidefilenames = ['.*'] + elif option.lower() != 'false': + try: + for pattern in option.split("\n"): + if pattern.strip() != '': + args.hidefilenames.append(pattern) + except TypeError: + pass if args.offline and configs.has_option('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.proxy and configs.has_option('settings', 'proxy'): args.proxy = configs.get('settings', 'proxy') if not args.verbose and configs.has_option('settings', 'verbose'): @@ -266,32 +279,26 @@ def parseArguments(): def should_exclude(entity, include, exclude): if entity is not None and entity.strip() != '': - try: - for pattern in include: - try: - compiled = re.compile(pattern, re.IGNORECASE) - if compiled.search(entity): - return False - except re.error as ex: - log.warning(u('Regex error ({msg}) for include pattern: {pattern}').format( - msg=u(ex), - pattern=u(pattern), - )) - except TypeError: # pragma: nocover - pass - try: - for pattern in exclude: - try: - compiled = re.compile(pattern, re.IGNORECASE) - if compiled.search(entity): - return pattern - except re.error as ex: - log.warning(u('Regex error ({msg}) for exclude pattern: {pattern}').format( - msg=u(ex), - pattern=u(pattern), - )) - except TypeError: # pragma: nocover - pass + for pattern in include: + try: + compiled = re.compile(pattern, re.IGNORECASE) + if compiled.search(entity): + return False + except re.error as ex: + log.warning(u('Regex error ({msg}) for include pattern: {pattern}').format( + msg=u(ex), + pattern=u(pattern), + )) + for pattern in exclude: + try: + compiled = re.compile(pattern, re.IGNORECASE) + if compiled.search(entity): + return pattern + except re.error as ex: + log.warning(u('Regex error ({msg}) for exclude pattern: {pattern}').format( + msg=u(ex), + pattern=u(pattern), + )) return False @@ -336,8 +343,18 @@ def send_heartbeat(project=None, branch=None, hostname=None, stats={}, key=None, 'type': entity_type, } if hidefilenames and entity is not None and entity_type == 'file': - extension = u(os.path.splitext(data['entity'])[1]) - data['entity'] = u('HIDDEN{0}').format(extension) + for pattern in hidefilenames: + try: + compiled = re.compile(pattern, re.IGNORECASE) + if compiled.search(entity): + extension = u(os.path.splitext(data['entity'])[1]) + data['entity'] = u('HIDDEN{0}').format(extension) + break + except re.error as ex: + log.warning(u('Regex error ({msg}) for include pattern: {pattern}').format( + msg=u(ex), + pattern=u(pattern), + )) if stats.get('lines'): data['lines'] = stats['lines'] if stats.get('language'):