Skip to content

Commit 2d04db2

Browse files
mediaministerdagwieers
authored andcommitted
Make exceptions Python 2/3 compatible (#623)
* Return to previous situation * Restore comment and use very selective try-block * Early exit and flat logic
1 parent e52d6f5 commit 2d04db2

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

resources/lib/kodiutils.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -696,26 +696,23 @@ def get_cache(path, ttl=None): # pylint: disable=redefined-outer-name
696696
from time import localtime, mktime
697697
mtime = stat_file(fullpath).st_mtime()
698698
now = mktime(localtime())
699-
if ttl is None or now - mtime < ttl:
700-
try: # Python 3
701-
from json import loads, JSONDecodeError
702-
ValueError = BaseException # pylint: disable=invalid-name, redefined-builtin
703-
except ImportError: # Python 2
704-
from json import loads
705-
JSONDecodeError = BaseException
706-
if ttl is None:
707-
log(3, "Cache '{path}' is forced from cache.", path=path)
708-
else:
709-
log(3, "Cache '{path}' is fresh, expires in {time}.", path=path, time=human_delta(mtime + ttl - now))
710-
cache_data = None
711-
with open_file(fullpath, 'r') as fdesc:
712-
try:
713-
cache_data = to_unicode(fdesc.read())
714-
if cache_data:
715-
return loads(cache_data)
716-
except (JSONDecodeError, TypeError, ValueError): # pylint: disable=broad-except
717-
return None
718-
return None
699+
if ttl and now >= mtime + ttl:
700+
return None
701+
702+
if ttl is None:
703+
log(3, "Cache '{path}' is forced from cache.", path=path)
704+
else:
705+
log(3, "Cache '{path}' is fresh, expires in {time}.", path=path, time=human_delta(mtime + ttl - now))
706+
with open_file(fullpath, 'r') as fdesc:
707+
cache_data = to_unicode(fdesc.read())
708+
if not cache_data:
709+
return None
710+
711+
from json import loads
712+
try:
713+
return loads(cache_data)
714+
except (TypeError, ValueError): # No JSON object could be decoded
715+
return None
719716

720717

721718
def update_cache(path, data):

0 commit comments

Comments
 (0)