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
|
import shutil, tempfile
|
||||||
from os import path
|
from os import path
|
||||||
from unittest import TestCase
|
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):
|
class TestAPIKeyTools(TestCase):
|
||||||
@@ -44,8 +44,8 @@ class TestAPIKeyTools(TestCase):
|
|||||||
shutil.rmtree(self.test_dir)
|
shutil.rmtree(self.test_dir)
|
||||||
|
|
||||||
def test_key_detector(self):
|
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):
|
def test_key_remover(self):
|
||||||
api_key_remover(path.join(self.test_dir, 'test.yaml'))
|
remove_api_key(path.join(self.test_dir, 'test.yaml'))
|
||||||
assert api_key_detector(path.join(self.test_dir, 'test.yaml')) is False
|
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
|
from __future__ import print_function
|
||||||
import glob
|
import glob
|
||||||
import re
|
import re
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
tokenString = '[Token '
|
fixtures_directory = 'tests/fixtures/'
|
||||||
fixturesDirectory = 'tests/fixtures/'
|
zero_api_regex = r'(\[Token )0{40}(\])'
|
||||||
zeroapiregex = r'(\[Token )0{40}(\])'
|
real_api_regex = r'(\[Token ).{40}(\])'
|
||||||
anyapiregex = r'(\[Token ).{40}(\])'
|
zero_token_string = '[Token ' + 40 * '0' + ']'
|
||||||
zeroapistring = '[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'.
|
Detect whether the file contains an api key in the Token object that is not 40*'0'.
|
||||||
See issue #86.
|
See issue #86.
|
||||||
:param file: path-to-file to check
|
:param file: path-to-file to check
|
||||||
:return: boolean
|
:return: boolean
|
||||||
'''
|
"""
|
||||||
f = open(file, 'r')
|
f = open(file, 'r')
|
||||||
text = f.read()
|
text = f.read()
|
||||||
if re.search(anyapiregex, text) is not None and \
|
if re.search(real_api_regex, text) is not None and \
|
||||||
re.search(zeroapiregex, text) is None:
|
re.search(zero_api_regex, text) is None:
|
||||||
return True
|
return True
|
||||||
return False
|
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.
|
Change the api key in the Token object to 40*'0'. See issue #86.
|
||||||
:param file: path-to-file to change
|
:param file: path-to-file to change
|
||||||
'''
|
"""
|
||||||
f = open(file, 'r')
|
with open(file, 'r') as fp:
|
||||||
text = f.read()
|
text = fp.read()
|
||||||
f.close()
|
text = re.sub(real_api_regex, zero_token_string, text)
|
||||||
text = re.sub(anyapiregex, zeroapistring, text)
|
with open(file, 'w') as fp:
|
||||||
f = open(file, 'w')
|
fp.write(text)
|
||||||
f.write(text)
|
|
||||||
f.close()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def main(path):
|
def main(path):
|
||||||
if path[-1] != '/':
|
if path[-1] != '/':
|
||||||
raise ValueError('Final character in path must be /.')
|
raise ValueError('Final character in path must be /.')
|
||||||
nFilesChanged = 0
|
n_files_changed = 0
|
||||||
for file in glob.glob(path+'*.yaml'):
|
for filename in glob.glob(path+'*.yaml'):
|
||||||
if api_key_detector(file):
|
if has_api_key(filename):
|
||||||
api_key_remover(file)
|
remove_api_key(filename)
|
||||||
nFilesChanged += 1
|
n_files_changed += 1
|
||||||
print("Changed {} files.".format(nFilesChanged))
|
print("Changed {} files.".format(n_files_changed))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("path", help="path to test fixtures",
|
parser.add_argument("path", help="path to test fixtures",
|
||||||
nargs='?', default=fixturesDirectory)
|
nargs='?', default=fixtures_directory)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
main(args.path)
|
main(args.path)
|
||||||
|
|||||||
Reference in New Issue
Block a user