Skip to content

Commit

Permalink
spiderfoot: Move cachePath() from sflib to SpiderFootHelpers (smicall…
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoles committed Sep 11, 2021
1 parent d62fe91 commit 4729d18
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
19 changes: 3 additions & 16 deletions sflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import urllib.request
from copy import deepcopy
from datetime import datetime
from pathlib import Path

import cryptography
import dns.resolver
Expand All @@ -40,6 +39,7 @@
import urllib3
from bs4 import BeautifulSoup, SoupStrainer
from publicsuffixlist import PublicSuffixList
from spiderfoot import SpiderFootHelpers

# For hiding the SSL warnings coming from the requests lib
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # noqa: DUO131
Expand Down Expand Up @@ -299,19 +299,6 @@ def hashstring(self, string: str) -> str:
s = str(string)
return hashlib.sha256(s.encode('raw_unicode_escape')).hexdigest()

def cachePath(self) -> str:
"""Returns the file system location of the cacha data files.
Returns:
str: SpiderFoot cache file system path
"""
path = os.environ.get('SPIDERFOOT_CACHE')
if not path:
path = f"{Path.home()}/.spiderfoot/cache"
if not os.path.isdir(path):
os.mkdir(path)
return path

def cachePut(self, label: str, data: str) -> None:
"""Store data to the cache
Expand All @@ -320,7 +307,7 @@ def cachePut(self, label: str, data: str) -> None:
data (str): Data to cache
"""
pathLabel = hashlib.sha224(label.encode('utf-8')).hexdigest()
cacheFile = self.cachePath() + "/" + pathLabel
cacheFile = SpiderFootHelpers.cachePath() + "/" + pathLabel
with io.open(cacheFile, "w", encoding="utf-8", errors="ignore") as fp:
if isinstance(data, list):
for line in data:
Expand Down Expand Up @@ -349,7 +336,7 @@ def cacheGet(self, label: str, timeoutHrs: int) -> str:
return None

pathLabel = hashlib.sha224(label.encode('utf-8')).hexdigest()
cacheFile = self.cachePath() + "/" + pathLabel
cacheFile = SpiderFootHelpers.cachePath() + "/" + pathLabel
try:
(m, i, d, n, u, g, sz, atime, mtime, ctime) = os.stat(cacheFile)

Expand Down
16 changes: 15 additions & 1 deletion spiderfoot/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ def dataPath() -> str:
if not path:
path = f"{Path.home()}/.spiderfoot/"
if not os.path.isdir(path):
os.mkdir(path)
os.makedirs(path, exist_ok=True)
return path

@staticmethod
def cachePath() -> str:
"""Returns the file system location of the cacha data files.
Returns:
str: SpiderFoot cache file system path
"""
path = os.environ.get('SPIDERFOOT_CACHE')
if not path:
path = f"{Path.home()}/.spiderfoot/cache"
if not os.path.isdir(path):
os.makedirs(path, exist_ok=True)
return path

@staticmethod
Expand Down
9 changes: 0 additions & 9 deletions test/unit/test_spiderfoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,6 @@ def test_hash_string_should_return_a_string(self):
self.assertIsInstance(hash_string, str)
self.assertEqual("aedfb92b3053a21a114f4f301a02a3c6ad5dff504d124dc2cee6117623eec706", hash_string)

def test_cache_path_should_return_a_string(self):
"""
Test cachePath(self)
"""
sf = SpiderFoot(dict())

cache_path = sf.cachePath()
self.assertIsInstance(cache_path, str)

def test_cache_get_should_return_a_string(self):
"""
Test cachePut(self, label, data)
Expand Down
14 changes: 14 additions & 0 deletions test/unit/test_spiderfoothelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ class TestSpiderFootHelpers(unittest.TestCase):
Test SpiderFootHelpers
"""

def test_data_path_should_return_a_string(self):
"""
Test dataPath()
"""
data_path = SpiderFootHelpers.dataPath()
self.assertIsInstance(data_path, str)

def test_cache_path_should_return_a_string(self):
"""
Test cachePath()
"""
cache_path = SpiderFootHelpers.cachePath()
self.assertIsInstance(cache_path, str)

def test_target_type(self):
"""
Test targetType(target)
Expand Down

0 comments on commit 4729d18

Please sign in to comment.