Skip to content

Commit

Permalink
refactor: Jeff refactor suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpulver committed Jul 4, 2024
1 parent 8856151 commit 890fcd0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
4 changes: 0 additions & 4 deletions safety/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
from typing_extensions import Annotated





LOG = logging.getLogger(__name__)

def preprocess_args():
Expand All @@ -59,7 +56,6 @@ def preprocess_args():
if len(sys.argv) > index + 1:
next_arg = sys.argv[index + 1]
if next_arg in ('1', 'true'):
sys.argv[index] = '--debug' # Keep --debug
sys.argv.pop(index + 1) # Remove the next argument (1 or true)

preprocess_args()
Expand Down
37 changes: 17 additions & 20 deletions safety/safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,30 @@
def get_from_cache(db_name: str, cache_valid_seconds: int = 0, skip_time_verification: bool = False) -> Optional[Dict[str, Any]]:
cache_file_lock = f"{DB_CACHE_FILE}.lock"
os.makedirs(os.path.dirname(cache_file_lock), exist_ok=True)
lock = FileLock(cache_file_lock, timeout=10)
with lock:
with FileLock(cache_file_lock, timeout=10) as lock:
if os.path.exists(DB_CACHE_FILE):
with open(DB_CACHE_FILE) as f:
try:
data = json.loads(f.read())
if db_name in data:

if "cached_at" in data[db_name]:
if data[db_name]["cached_at"] + cache_valid_seconds > time.time() or skip_time_verification:
LOG.debug('Getting the database from cache at %s, cache setting: %s',
data[db_name]["cached_at"], cache_valid_seconds)
except json.JSONDecodeError:
LOG.debug('JSONDecodeError trying to get the cached database.')
if db_name in data:

try:
data[db_name]["db"]["meta"]["base_domain"] = "https://data.safetycli.com"
except KeyError as e:
pass
if "cached_at" in data[db_name]:
if data[db_name]["cached_at"] + cache_valid_seconds > time.time() or skip_time_verification:
LOG.debug('Getting the database from cache at %s, cache setting: %s',
data[db_name]["cached_at"], cache_valid_seconds)

return data[db_name]["db"]
try:
data[db_name]["db"]["meta"]["base_domain"] = "https://data.safetycli.com"
except KeyError as e:
pass

LOG.debug('Cached file is too old, it was cached at %s', data[db_name]["cached_at"])
else:
LOG.debug('There is not the cached_at key in %s database', data[db_name])
return data[db_name]["db"]

except json.JSONDecodeError:
LOG.debug('JSONDecodeError trying to get the cached database.')
LOG.debug('Cached file is too old, it was cached at %s', data[db_name]["cached_at"])
else:
LOG.debug('There is not the cached_at key in %s database', data[db_name])
else:
LOG.debug("Cache file doesn't exist...")
return None
Expand Down Expand Up @@ -101,8 +99,7 @@ def write_to_cache(db_name, data):
raise

cache_file_lock = f"{DB_CACHE_FILE}.lock"
lock = FileLock(cache_file_lock, timeout=10)
with lock:
with FileLock(cache_file_lock, timeout=10) as lock:
if os.path.exists(DB_CACHE_FILE):
with open(DB_CACHE_FILE, "r") as f:
try:
Expand Down

0 comments on commit 890fcd0

Please sign in to comment.