From 9a1b52d25a88f0f0b0717afc77e212747c307f19 Mon Sep 17 00:00:00 2001
From: Davis Thames
Date: Mon, 30 Apr 2018 10:33:14 -0500
Subject: [PATCH] issue #86 addressing pull comments
---
tests/test_api_key_tools.py | 8 +++---
tools/api_key_tool.py | 54 ++++++++++++++++++-------------------
2 files changed, 31 insertions(+), 31 deletions(-)
mode change 100644 => 100755 tools/api_key_tool.py
diff --git a/tests/test_api_key_tools.py b/tests/test_api_key_tools.py
index 2fb2514..43223f9 100644
--- a/tests/test_api_key_tools.py
+++ b/tests/test_api_key_tools.py
@@ -5,7 +5,7 @@
import shutil, tempfile
from os import path
from unittest import TestCase
-from tools.api_key_tool import api_key_remover, api_key_detector
+from tools.api_key_tool import remove_api_key, has_api_key
class TestAPIKeyTools(TestCase):
@@ -44,8 +44,8 @@ class TestAPIKeyTools(TestCase):
shutil.rmtree(self.test_dir)
def test_key_detector(self):
- assert api_key_detector(path.join(self.test_dir, 'test.yaml')) is True
+ assert has_api_key(path.join(self.test_dir, 'test.yaml')) is True
def test_key_remover(self):
- api_key_remover(path.join(self.test_dir, 'test.yaml'))
- assert api_key_detector(path.join(self.test_dir, 'test.yaml')) is False
+ remove_api_key(path.join(self.test_dir, 'test.yaml'))
+ assert has_api_key(path.join(self.test_dir, 'test.yaml')) is False
diff --git a/tools/api_key_tool.py b/tools/api_key_tool.py
old mode 100644
new mode 100755
index 1d42d09..3d92246
--- a/tools/api_key_tool.py
+++ b/tools/api_key_tool.py
@@ -1,58 +1,58 @@
+#!/usr/bin/env python
+
from __future__ import print_function
import glob
import re
import argparse
-tokenString = '[Token '
-fixturesDirectory = 'tests/fixtures/'
-zeroapiregex = r'(\[Token )0{40}(\])'
-anyapiregex = r'(\[Token ).{40}(\])'
-zeroapistring = '[Token '+40*'0'+']'
+fixtures_directory = 'tests/fixtures/'
+zero_api_regex = r'(\[Token )0{40}(\])'
+real_api_regex = r'(\[Token ).{40}(\])'
+zero_token_string = '[Token ' + 40 * '0' + ']'
-def api_key_detector(file):
- '''
+def has_api_key(file):
+ """
Detect whether the file contains an api key in the Token object that is not 40*'0'.
See issue #86.
:param file: path-to-file to check
:return: boolean
- '''
+ """
f = open(file, 'r')
text = f.read()
- if re.search(anyapiregex, text) is not None and \
- re.search(zeroapiregex, text) is None:
+ if re.search(real_api_regex, text) is not None and \
+ re.search(zero_api_regex, text) is None:
return True
return False
-def api_key_remover(file):
- '''
+def remove_api_key(file):
+ """
Change the api key in the Token object to 40*'0'. See issue #86.
:param file: path-to-file to change
- '''
- f = open(file, 'r')
- text = f.read()
- f.close()
- text = re.sub(anyapiregex, zeroapistring, text)
- f = open(file, 'w')
- f.write(text)
- f.close()
+ """
+ with open(file, 'r') as fp:
+ text = fp.read()
+ text = re.sub(real_api_regex, zero_token_string, text)
+ with open(file, 'w') as fp:
+ fp.write(text)
return
def main(path):
if path[-1] != '/':
raise ValueError('Final character in path must be /.')
- nFilesChanged = 0
- for file in glob.glob(path+'*.yaml'):
- if api_key_detector(file):
- api_key_remover(file)
- nFilesChanged += 1
- print("Changed {} files.".format(nFilesChanged))
+ n_files_changed = 0
+ for filename in glob.glob(path+'*.yaml'):
+ if has_api_key(filename):
+ remove_api_key(filename)
+ n_files_changed += 1
+ print("Changed {} files.".format(n_files_changed))
+
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("path", help="path to test fixtures",
- nargs='?', default=fixturesDirectory)
+ nargs='?', default=fixtures_directory)
args = parser.parse_args()
main(args.path)