Skip to content

Commit

Permalink
Fix invalid date entering cache DB
Browse files Browse the repository at this point in the history
  • Loading branch information
ValueRaider committed Dec 22, 2023
1 parent f753e60 commit de30304
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions yfinance/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,6 @@ def get_tz_cache():
return _TzCacheManager.get_tz_cache()


def set_tz_cache_location(cache_dir: str):
"""
Sets the path to create the "py-yfinance" cache folder in.
Useful if the default folder returned by "appdir.user_cache_dir()" is not writable.
Must be called before cache is used (that is, before fetching tickers).
:param cache_dir: Path to use for caches
:return: None
"""
_TzDBManager.set_location(cache_dir)



# --------------
# Cookie cache
Expand Down Expand Up @@ -300,9 +289,21 @@ def get_location(cls):


Cookie_db_proxy = _peewee.Proxy()
class ISODateTimeField(_peewee.DateTimeField):
# Ensure Python datetime is read & written correctly for sqlite,
# because user discovered peewee allowed an invalid datetime
# to get written.
def db_value(self, value):
if value and isinstance(value, _datetime.datetime):
return value.isoformat()
return super().db_value(value)
def python_value(self, value):
if value and isinstance(value, str) and 'T' in value:
return _datetime.datetime.fromisoformat(value)
return super().python_value(value)
class _CookieSchema(_peewee.Model):
strategy = _peewee.CharField(primary_key=True)
fetch_date = _peewee.DateTimeField(default=_datetime.datetime.now)
fetch_date = ISODateTimeField(default=_datetime.datetime.now)

# Which cookie type depends on strategy
cookie_bytes = _peewee.BlobField()
Expand Down Expand Up @@ -398,3 +399,19 @@ def store(self, strategy, cookie):
def get_cookie_cache():
return _CookieCacheManager.get_cookie_cache()



def set_cache_location(cache_dir: str):
"""
Sets the path to create the "py-yfinance" cache folder in.
Useful if the default folder returned by "appdir.user_cache_dir()" is not writable.
Must be called before cache is used (that is, before fetching tickers).
:param cache_dir: Path to use for caches
:return: None
"""
_TzDBManager.set_location(cache_dir)
_CookieDBManager.set_location(cache_dir)

def set_tz_cache_location(cache_dir: str):
set_cache_location(cache_dir)

0 comments on commit de30304

Please sign in to comment.