mirror of
https://github.com/hydrosquall/tiingo-python.git
synced 2025-12-17 20:04:19 +01:00
issue #86 addressing pull comments
This commit is contained in:
@@ -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
|
||||
|
||||
54
tools/api_key_tool.py
Normal file → Executable file
54
tools/api_key_tool.py
Normal file → Executable file
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user